#! /bin/sh
#############################################################################
#
# $Id: grass.src,v 1.13 2002/01/22 04:51:04 glynn Exp $
#
# MODULE:   	Grass Initialization
# AUTHOR(S):	Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
# PURPOSE:  	The source file for this shell script is in
#   	    	src/general/init/grass.src and is the grass startup script. It
#   	    	requires a source file because the definition of GISBASE
#   	    	is not known until compile time and is substituted from the
#   	    	Gmakefile. The script also sets the default user interface and
#   	    	parses the command line options for interface and help flags.
#   	    	Any remaining options are passed to Init.sh.
# COPYRIGHT:    (C) 2000 by the GRASS Development Team
#
#               This program is free software under the GNU General Public
#   	    	License (>=v2). Read the file COPYING that comes with GRASS
#   	    	for details.
#
#############################################################################

# Set the GISBASE variable
GISBASE=/home/guest/stankovic@itc.it/babyGRASS_src/dist.armv4l-unknown-linux-gnu
export GISBASE

# Set the GRASS_PERL variable
GRASS_PERL=/usr/bin/perl
export GRASS_PERL

# Clear the variable that will hold the command line options
CMD_LINE_ARGS=""

# Get the command name
CMD_NAME=`basename $0`

# Go through the command line options
for i in "$@" ; do
    
    # Use a case to check the command line options
    case $i in
    
    	# Check if the user asked for the version
	-v|--version)
	    $GISBASE/bin/g.version
	    exit
	    ;;

    	# Check if the user asked for help
	help|-h|-help|--help)
	    echo "Usage:"
	    echo "  $CMD_NAME [-h | -help | --help] [-text | -tcltk] [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]"
	    echo
            echo "Flags:"
            echo "  -h or -help or --help          print this help message"
            echo "  -text                          use text based interface"
            echo "  -tcltk                         use Tcl/Tk based graphical interface"
            echo
            echo "Parameters:"
            echo "  GISDBASE                       initial database"
            echo "  LOCATION_NAME                  initial location"
            echo "  MAPSET                         initial mapset"
            echo
            echo "  GISDBASE/LOCATION_NAME/MAPSET  fully qualified initial LOCATION directory"
            echo
            echo "Environment variables:"
            echo "  GRASS_TCLSH                    set tclsh shell name to override 'tclsh'"
            echo "  GRASS_WISH                     set wish shell name to override 'wish'"
            echo "  GRASS_ADDON_PATH               set additional path(s) to local GRASS modules"
	    exit
	    ;;
	
	# Check if the -text flag was given
	-text)
	    GRASS_GUI="text"
	    ;;
	
	# Check if the -tcltk flag was given
	-tcltk)
	    GRASS_GUI="tcltk"
	    ;;
	    
	# Check for command line options with spaces
	*' '*)
	    i="'$i'"
	    CMD_LINE_ARGS="$CMD_LINE_ARGS $i "
	    ;;
	    
	# Keep any other command line options
	*)
	    CMD_LINE_ARGS="$CMD_LINE_ARGS $i "
	    ;;
    esac
done

# Set the GRASSRC file
GISRC=$HOME/.grassrc5
export GISRC

# At this point the grass user interface variable has been set from the
# command line, been set from an external environment variable, or is 
# not set. So we check if it is not set
if [ ! "$GRASS_GUI" ] ; then
    
    # Check for a reference to the grass user interface in the grassrc file
    if [ -f $GISRC ] ; then
    	GRASS_GUI=`awk '/GRASS_GUI/ {print $2}' $GISRC`
    fi
    
    # Set the grass user interface to the default if needed - currently tcltk
    if [ ! "$GRASS_GUI" ] ; then
    	GRASS_GUI="tcltk"
    fi
fi

# Export the user interface variable
export GRASS_GUI

# Need to call /bin/sh in order to pass command line options properly
/bin/sh -c "$GISBASE/etc/Init.sh $CMD_LINE_ARGS"
