#!/bin/bash
#
#	$Id: gmtswitch 9554 2011-07-28 02:32:32Z remko $
#
#	gmtswitch - switch between several installed GMT versions
#
function checkinput() 
{ # Make sure argument is an integer
	echo $1 | grep -Eq '^(\+|-)?[0-9]+$'
	echo $?
}

help=0
if [ $# -gt 1 ]; then
	help=1
fi
if [ $# -eq 1 ] && [ "X$1" = "X-help" ]; then
	help=1
fi
if [ $help -eq 1 ]; then
	cat << EOF >&2
usage: gmtswitch [version]

version, if present, is a unique text pattern identifying a GMT version,
e.g., GMT4.5.7.  If none are given then we list available versions and
let the user choose one.  If the version is given as "init" OR it is the
very first time gmtswitch is run we will initialize the list of versions.
EOF
	exit
fi
#--------------------------------------------------------------------------------
# Get the functionality of echo -n
#--------------------------------------------------------------------------------
if [ x`echo -n` = x ]; then	# echo -n works
	echon()
	{
		echo -n "$*" 
	}
elif [ x`echo -e` = x ]; then	# echo -e works
	echon()
	{
		echo -e "$*"'\c'
	}
else				# echo with escapes better work
	echon()
	{
		echo "$*"'\c'
	}
fi

here=`pwd`
cd ~
home=`pwd`
if [ ! -f $home/.gmtversions ]; then	# No .gmtversions exists yet, first do that part
	cat << EOF >&2

	GMTSWITCH $Revision: 9554 $
	
gmtswitch helps you modify your environment to allow for the switching back
and forth between several GMT versions, in particular GMT 5.x and previous GMT
versions such as GMT 4.5.7.  First, we need to determine all the GMT versions
currently installed on this platform.  If you have not already installed the latest GMT 5
please CTRL-C at the prompt and do that install first.  Otherwise, hit RETURN
and please be patient while we search your system.

EOF
	echon "==> Press RETURN to continue: " >&2
	read s

	echon "--> Searching..." >&2
	( find / -type d -name 'GMT[34].*' -print > $home/.gmtversions ) 2> /dev/null
	n=`cat $home/.gmtversions | wc -l | awk '{print $1}'`
	cat << EOF >&2
	
There are $n GMT versions currently installed on your system.
Before we can allow for switching you must prepare your environment.

1. CSH OR TCSH USERS
   In your .cshrc or .tcshrc file:

   Change your path statement. Make sure it includes $home/this_gmt/bin
  
2. BASH USERS
   (re)Place these lines in your .profile file:

   Change your PATH statement. Make sure it includes $home/this_gmt/bin
   
Now, please make these edits and logout and back in again.  The next time
you run gmtswitch we will be able to do the switching.
CVS USERS: You will have to manually add the path to the GMT directory since
it is not considered an official release.
EOF
	exit
fi

if [ "X$1" = "XD" ]; then	# First entry is default
	line=`head -1 $home/.gmtversions`
	rm -f $home/this_gmt
	ln -sf $line $home/this_gmt
elif [ $# -eq 1 ]; then
	line=`grep $1 $home/.gmtversions`
	nl=`grep $1 $home/.gmtversions | wc -l`
	if [ $nl -eq 0 ]; then
		echo "Version $1 is not listed among recognized GMT versions" >&2
		echo "Run gmtswitch -help for more information" >&2
		exit
	fi
	if [ $nl -ne 1 ]; then
		echo "Version $1 is not unique among recognized GMT versions" >&2
		echo "Run gmtswitch without argument and choose from a menu" >&2
		exit
	fi
	rm -f $home/this_gmt
	ln -sf $line $home/this_gmt
else
	n=`cat $home/.gmtversions | wc -l | awk '{print $1}'`
	i=0
	while [ $i -lt $n ]; do
		i=`expr $i + 1`
		line=`sed -n ${i}p $home/.gmtversions`
		version=`basename $line`
		echo "$i. Version $version" >&2
	done
	echo "" >&2
	v=-1
	while [ $v -lt 0 ] || [ $v -gt $n ]; do
		echon "Please choose a version (1-$n) [1]: " >&2
		read v
		if [ "X$v" = "X" ]; then
			v=1
		fi
		result=`checkinput $v`
		if [ $result -eq 1 ]; then
			echon "ERROR: You must give an integer from 1-$n" >&2
			v=-1
		fi
	done
	line=`sed -n ${v}p $home/.gmtversions`
	rm -f $home/this_gmt
	ln -sf $line $home/this_gmt
fi
