#!/bin/sh
# @(#)gis.head
#
# updates for SWAT 2000 version.  This is a BETA script
# and may contain bugs between the model ID's.  This script
# also currently works only with GRASS 4.3 since much of the 
# binary code is written in non-fp.  I'm working on migrating
# the code to GRASS 5.0 (fp) as time allows.  So far the only
# major binary bug is with the format of the watershed input.
#
# Send questions to me at:  Bruce_Byars@baylor.edu
#
# -------------------------------- RLG, SCS ---------------
umask 002
if [ "$TERM" = "at386" ]
then stty kill \025
fi
LOOP=true
while ($LOOP)
do
# ---------------------------------------------------------

: ${SWATGRSBASE?}
: ${GISBASE?}

# get home directory, name of lockfile and gisrc file

home=`cd;pwd`
lockfile=$home/.gislock
#SWATGRSRC=$home/.swatgrsrc
#export SWATGRSRC

# set PATH to GRASS bin, ETC to GRASS etc

BINARY=$SWATGRSBASE/bin
ETC=$GISBASE/etc
PATH=$SWATGRSBASE/bin:$PATH
export PATH

# check for concurrent use

$ETC/lock $lockfile $$
case $? in
 0) 
    echo GRASS is not running, start GRASS before running the SWAT/GRASS
    echo interface.
    exit;;
 1)
    echo `whoami` is currently running GRASS. 
   ;;
 *)
    echo Unable to properly access $lockfile
    echo Please notify system personel.
    exit ;;
esac

# run tset to set terminal and TERM variable
# must tell it to output sh
eval `SHELL=/bin/sh tset -s -Q`
stty -tabs

# user selects all the database locations

$SWATGRSBASE/bin/swatgrs_set $SWATGRSBASE
case $? in
 0) ;;
 *) exit ;;
esac

LOCATION=${GISDBASE?}/${LOCATION_NAME?}/${MAPSET?}
export LOCATION

##############################################################

trap "" 2 3
shell=sh
shellname="Bourne Shell"

if test "$SHELL"
then
    if [ "$SHELL" = "/bin/csh" ]
    then
	shell=csh
	shellname="C Shell"
    fi
fi

# Display the advertising
clear
cat << EOF
Welcome to SWAT/GRASS Project (Spring 2001)

COPYRIGHT: 
  CAGSR, Baylor University                         Waco, Texas.
  Texas Agricultural Experiment Station            Temple, Texas.
  USDA, Soil Conservation Service                  Washington DC.
  USDA, Agricultural Research Service              Temple, Texas.
  NOAA                                             Washington DC.

This version running thru the $shellname ($shell)
When ready to quit enter:           exit
EOF

if [ $shell = csh ]
then
	
	if [ ! -d $LOCATION/swat ]
	then
	mkdir $LOCATION/swat
	fi

    HOME=$LOCATION/swat
    export HOME
    cshrc=$HOME/.cshrc
    rm -f $cshrc
    echo "set home = $HOME" > $cshrc

    if [ -r $HOME/.cshrc ]
    then
	grep '^ *set  *mail *= *' $HOME/.cshrc >> $cshrc
    fi

    if [ -r $HOME/.login ]
    then
	grep '^ *set  *mail *= *' $HOME/.login >> $cshrc
    fi

    echo "set history=30 noclobber ignoreeof" >> $cshrc
    echo "set prompt='\\" >> $cshrc
    echo "SWAT/GRASS session 2000>'" >> $cshrc
#   cd $LOCATION
    $ETC/run csh

    HOME=$home
    export HOME

else

# left in for NRCS applications (BB)

PS1="
SWAT/GRASS session 94.2>"

    export PS1
    cd $LOCATION                  # added RL Glenn, SCS
    $ETC/run sh
fi

LOOP=false
trap 2 3

done
echo ""
echo "                  GOOD BYE from the SWAT/GRASS Project"
sleep 2
clear
