File: init.sh

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (1048 lines) | stat: -rwxr-xr-x 27,927 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
#!/bin/sh
#############################################################################
#
# MODULE:   	GRASS Initialization
# AUTHOR(S):	Original author unknown - probably CERL
#               Andreas Lange - Germany - andreas.lange@rhein-main.de
#   	    	Huidae Cho - Korea - grass4u@gmail.com
#   	    	Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
#   	    	Markus Neteler - Germany/Italy - neteler@itc.it
#		Hamish Bowman - New Zealand - hamish_b at yahoo,com
# PURPOSE:  	The source file for this shell script is in
#   	    	src/general/init/init.sh. It sets up some environment
#   	    	variables and the lock file. It also parses any remaining
#   	    	command line options for setting the GISDBASE, LOCATION, and/or
#   	    	MAPSET. Finally it starts GRASS with the appropriate user
#   	    	interface and cleans up after it is finished.
# COPYRIGHT:    (C) 2000, 2010 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.
#
#############################################################################

trap "echo 'User break!' ; exit" 2 3 15

# Set default GUI
DEFAULT_GUI="wxpython"

# the following is only meant to be an internal variable for debugging this script.
#  use 'g.gisenv set="DEBUG=[0-5]"' to turn GRASS debug mode on properly.
if [ -z "$GRASS_DEBUG" ] ; then
   GRASS_DEBUG=0
fi

# Set the GRASS_PERL variable
GRASS_PERL=PERL_COMMAND
export GRASS_PERL

# GRASS_SH is normally just for Windows when not started from a bourne 
# shell. But when starting from Init.sh is still needed for Tcl/Tk.
GRASS_SH=/bin/sh
export GRASS_SH

# Set GRASS version number for R interface etc (must be an env_var for MS-Windows)
GRASS_VERSION="GRASS_VERSION_NUMBER"
export GRASS_VERSION

# Get the command name
CMD_NAME=START_UP

# Get the system name
SYSTEM=`uname -s`
case $SYSTEM in
MINGW* | MSYS*)
	MINGW=1
	;;
CYGWIN*)
	CYGWIN=1
	;;
Darwin*)
	MACOSX=1
	;;
esac


# 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)
	    echo "GRASS GIS $GRASS_VERSION"
	    echo
	    cat "$GISBASE/etc/license"
	    exit
	    ;;

    	# Check if the user asked for help
	help|-h|-help|--help)
	    echo "Usage:"
	    echo "  $CMD_NAME [-h | -help | --help] [-v | --version] [-c]"
	    echo "          [-text | -gui | -tcltk | -oldtcltk | -wxpython | -wx]"
	    echo "          [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]"
	    echo
            echo "Flags:"
            echo "  -h or -help or --help          print this help message"
	    echo "  -v or --version                show version information and exit"
	    echo "  -c                             create given mapset if it doesn't exist"
            echo "  -text                          use text based interface"
            echo "                                   and set as default"
            echo "  -gui                           use graphical user interface ($DEFAULT_GUI by default)"
            echo "                                   and set as default"
            echo "  -tcltk                         use Tcl/Tk based graphical user interface"
            echo "                                   and set as default"
	    echo "  -oldtcltk                      use old Tcl/Tk based graphical user interface"
            echo "                                   and set as default"
            echo "  -wxpython or -wx               use wxPython based graphical user interface"
            echo "                                   and set as default"
            echo
            echo "Parameters:"
            echo "  GISDBASE                       initial database (path to GIS data)"
            echo "  LOCATION_NAME                  initial location"
            echo "  MAPSET                         initial mapset"
            echo
            echo "  GISDBASE/LOCATION_NAME/MAPSET  fully qualified initial mapset directory"
            echo
            echo "Environment variables relevant for startup:"
            echo "  GRASS_GUI                      select GUI (text, gui, tcltk, oldtcltk, wxpython)"
            echo "  GRASS_TCLSH                    set tclsh shell name to override 'tclsh'"
            echo "  GRASS_WISH                     set wish shell name to override 'wish'"
            echo "  GRASS_HTML_BROWSER             set html web browser for help pages"
            echo "  GRASS_ADDON_PATH               set additional path(s) to local GRASS modules"
            echo "  GRASS_BATCH_JOB                shell script to be processed as batch job"
            echo "  GRASS_PYTHON                   set python shell name to override 'python'"
	    exit
	    ;;

	# Check if the -text flag was given
	-text | --text)
	    GRASS_GUI="text"
	    shift
	    ;;

	# Check if the -gui flag was given
	-gui | --gui)
	    GRASS_GUI="$DEFAULT_GUI"
	    shift
	    ;;

	# Check if the -tcltk flag was given
	-tcltk | --tcltk)
	    GRASS_GUI="tcltk"
	    shift
	    ;;

	# Check if the -oldtcltk flag was given
	-oldtcltk | --oldtcltk)
	    GRASS_GUI="oldtcltk"
	    shift
	    ;;

	# Check if the -wxpython flag was given
	-wxpython | -wx | --wxpython | --wx)
	    GRASS_GUI="wxpython"
	    shift
	    ;;

    	# Check if the user wants to create a new mapset
	-c | --create)
	    CREATE_NEW=1
	    shift
	    ;;
    esac
done

# Set the GIS_LOCK variable to current process id
GIS_LOCK=$$
export GIS_LOCK

# Set the global grassrc file
if [ -n "$GRASS_BATCH_JOB" ] ; then
	GISRCRC="$HOME/.grassrc6.`uname -n`"
	if [ ! -f "$GISRCRC" ] ; then
		GISRCRC="$HOME/.grassrc6"
	fi
else
	GISRCRC="$HOME/.grassrc6"
fi

# Set the session grassrc file
if [ "$MINGW" ] ; then
	PWD=`pwd -W`
	USER="$USERNAME"
	if [ ! "$USER" ] ; then
		USER="user_name"
	fi
	if [ ! -f "$GISBASE/etc/monitorcap" ] ; then
		# create an empty monitorcap
		touch "$GISBASE/etc/monitorcap"
	fi
else
	PWD=`pwd`
	USER="`whoami`"
fi


# all exits after setting up $tmp should also tidy it up
cleanup_tmp()
{
  # remove session files from tmpdir
  rm -rf "$tmp"
}

## use TMPDIR if it exists, otherwise /tmp
#tmp=${TMPDIR-/tmp}
#tmp="$tmp/grass6-$USER-$GIS_LOCK"
tmp=/tmp/grass6-$USER-$GIS_LOCK
(umask 077 && mkdir "$tmp") || {
    echo "Cannot create temporary directory! Exiting." 1>&2
    exit 1
}
GISRC="$tmp/gisrc"
export GISRC

# remove invalid GISRC file to avoid disturbing error messages:
cat "$GISRCRC" 2>/dev/null| grep UNKNOWN >/dev/null
if [ $? -eq 0 ] ; then
   rm -f "$GISRCRC"
fi

# Copy the global grassrc file to the session grassrc file
if [ -f "$GISRCRC" ] ; then
    cp "$GISRCRC" "$GISRC"
    if [ $? -eq 1 ] ; then
    	echo "Cannot copy '$GISRCRC' to '$GISRC'"
	cleanup_tmp
    	exit 1
    fi
fi

# Copy global grassrc file to session grassrc

# 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

    # Check for a reference to the language in the grassrc file
    if [ -f "$GISRC" ] ; then
	if [ `grep -c 'LANG' "$GISRC"` -ge 1 ] ; then
    	    LANG=`awk '/LANG/ {print $2}' "$GISRC"`
	fi
    fi

    # Set the GRASS user interface to the default if needed
    if [ ! "$GRASS_GUI" ] ; then
	GRASS_GUI="$DEFAULT_GUI"
    fi
else
    if [ "$GRASS_GUI" = "gui" ] ; then
    	GRASS_GUI="$DEFAULT_GUI"
    elif [ "$GRASS_GUI" = "wx" ] ; then
	GRASS_GUI="wxpython"
    fi
fi

# in case of fire, break glass
dos2unix_path()
{
   echo "$1" | sed -e 's|^\([A-Za-z]\):|/\1|' -e 's|\\|/|g'
}

# Set PATH to GRASS bin, ETC to GRASS etc
ETC="$GISBASE/etc"

if [ $LANG ] ; then
    LCL=$LANG
    export LANG
    export LANGUAGE=$LANG
else
    if [ "$LC_ALL" ] ; then
        LCL=`echo "$LC_ALL" | sed 's/\(..\)\(.*\)/\1/'`
    elif [ "$LC_MESSAGES" ] ; then
        LCL=`echo "$LC_MESSAGES" | sed 's/\(..\)\(.*\)/\1/'`
    else
        LCL=`echo "$LANG" | sed 's/\(..\)\(.*\)/\1/'`
    fi
fi

# if it doesn't exist set it to something so that g.extension's default is reasonable
if [ -z "$GRASS_ADDON_PATH" ] ; then
    if [ "$MINGW" ] ; then
	APPDATA_UNIX=`dos2unix_path "$APPDATA"`
	GRASS_ADDON_PATH="$APPDATA_UNIX/GRASS6/addons"
    else
	GRASS_ADDON_PATH="$HOME/.grass6/addons"
    fi
    export GRASS_ADDON_PATH
fi
PATH="$GISBASE/bin:$GISBASE/scripts:$GRASS_ADDON_PATH:$PATH"
export PATH

# Set LD_LIBRARY_PATH to find GRASS shared libraries
if [ ! "$LD_LIBRARY_PATH_VAR" ] ; then
  LD_LIBRARY_PATH_VAR="$GISBASE/lib"
else
  LD_LIBRARY_PATH_VAR="$GISBASE/lib:$LD_LIBRARY_PATH_VAR"
fi
export LD_LIBRARY_PATH_VAR
# Additional copy of variable to use with grass-run.sh
GRASS_LD_LIBRARY_PATH="$LD_LIBRARY_PATH_VAR"
export GRASS_LD_LIBRARY_PATH

# Set some environment variables if they are not set
if [ ! "$GRASS_PAGER" ] ; then
    if [ -x /usr/bin/pager ] ; then
      	GRASS_PAGER=pager
    elif [ -x /bin/less ] || [ -x /usr/bin/less ] ; then
        GRASS_PAGER=less
    elif [ -x /bin/more ] || [ -x /usr/bin/more ] ; then
         GRASS_PAGER=more
    elif [ "$MINGW" ] ; then
        GRASS_PAGER=more
    else
        GRASS_PAGER=cat
    fi
    export GRASS_PAGER
fi

# Set up tcltk and wish environment 

if [ ! "$GRASS_TCLSH" ] ; then
   GRASS_TCLSH=tclsh
   export GRASS_TCLSH
fi   

#WISH_OS=`echo 'puts $tcl_platform(platform) ; exit 0' | wish`

if [ ! "$GRASS_WISH" ] ; then
   GRASS_WISH=wish
   export GRASS_WISH
fi

if [ ! "$GRASS_PYTHON" ] ; then
    if [ "$MACOSX" ] ; then
        GRASS_PYTHON=pythonw
    else
        GRASS_PYTHON=python
    fi
    export GRASS_PYTHON
fi

# Set PYTHONPATH to find GRASS Python modules
if [ ! "$PYTHONPATH" ] ; then
    PYTHONPATH="$GISBASE/etc/python"
else
    PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
fi
export PYTHONPATH
if [ "$MINGW" ] ; then
    PATHEXT="${PATHEXT};.PY"
    export PATHEXT
fi

# try and find a web browser if one isn't already specified
if [ ! "$GRASS_HTML_BROWSER" ] ; then
	if [ -x /usr/bin/xdg-open ] ; then
		GRASS_HTML_BROWSER=xdg-open
	elif [ -x /usr/bin/x-www-browser ] ; then
		GRASS_HTML_BROWSER=x-www-browser
	else 
		GRASS_HTML_BROWSER=true
	fi
fi
export GRASS_HTML_BROWSER


#predefine monitor size for certain architectures
if [ "$HOSTTYPE" = "arm" ] ; then
   #small monitor on ARM (iPAQ, zaurus... etc)
   GRASS_HEIGHT=320
   GRASS_WIDTH=240
   export GRASS_HEIGHT GRASS_WIDTH
fi


if [ ! "$GRASS_GNUPLOT" ] ; then
    GRASS_GNUPLOT="gnuplot -persist"
    export GRASS_GNUPLOT
fi

if [ ! "$GRASS_PROJSHARE" ] ; then
    GRASS_PROJSHARE=CONFIG_PROJSHARE
    export GRASS_PROJSHARE
fi

# First time user - GISRC is defined in the GRASS script
if [ ! -f "$GISRC" ] ; then

    if [ ! -f "$GISBASE/locale/$LCL/etc/grass_intro" ] ; then
	cat "$ETC/grass_intro"
    else
	cat "$GISBASE/locale/$LCL/etc/grass_intro"
    fi

    echo
    echo "Hit RETURN to continue"
    read ans

    #for convenience, define pwd as GISDBASE:
    echo "GISDBASE: $HOME" > "$GISRC"
    echo 'LOCATION_NAME: <UNKNOWN>' >> "$GISRC"
    echo 'MAPSET: <UNKNOWN>' >> "$GISRC"

    # This is a hack for not having a good initial gui - should be removed
    # with next version of initialization gui
    #GRASS_GUI="text"
    
else
    echo "Cleaning up temporary files ..."
    ("$ETC/clean_temp" > /dev/null &)
fi


echo "Starting GRASS ..."

# Check if we are running X windows by checking the DISPLAY variable
if [ "$DISPLAY" -o "$MINGW" ] ; then

    # Check if python is working properly
    if [ "$GRASS_GUI" = "wxpython" ]; then
        echo 'variable=True' | "$GRASS_PYTHON" >/dev/null 2>&1
    fi
    # Check if we need to find wish
    if [ "$GRASS_GUI" = "tcltk" ] || \
        [ "$GRASS_GUI" = "gis.m" ] || \
	[ "$GRASS_GUI" = "oldtcltk" ] || \
        [ "$GRASS_GUI" = "d.m" ] ; then

	# Check if wish is working properly
	echo 'exit 0' | "$GRASS_WISH" >/dev/null 2>&1
    fi

    # ok
    if [ "$?" = 0 ] ; then
        # Set the tcltkgrass base directory
        TCLTKGRASSBASE="$ETC"
        # Set the wxpython base directory
        WXPYTHONGRASSBASE="$ETC/wxpython"
    else

        # Wish was not found - switch to text interface mode
        echo
        echo "WARNING: The wish command does not work as expected!"
        echo "Please check your GRASS_WISH environment variable."
        echo "Use the -help option for details."
        echo "Switching to text based interface mode."
        echo
        echo "Hit RETURN to continue."
        read ans

        GRASS_GUI="text"
    fi
else

    # Display a message if a graphical interface was expected
    if [ "$GRASS_GUI" != "text" ] ; then
        # Set the interface mode to text
    	echo
	echo "WARNING: It appears that the X Windows system is not active."
	echo "A graphical based user interface is not supported."
	echo "Switching to text based interface mode."
	echo
	echo "Hit RETURN to continue"
	read ans

        GRASS_GUI="text"
    fi
fi

# Save the user interface variable in the grassrc file - choose a temporary
# file name that should not match another file
if [ -f "$GISRC" ] ; then
    awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
    echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
    mv -f "$GISRC.$$" "$GISRC"
fi

# Parsing argument to get LOCATION
if [ ! "$1" ] ; then

    # Try interactive startup
    LOCATION=
else

    # Try non-interactive startup
    L=
    
    if [ "$1" = "-" ] ; then
    
    	if [ "$LOCATION" ] ; then
    	    L="$LOCATION"
    	fi
    else
    	L="$1"
    fi

    if [ "$L" ] ; then
	if [ "$L" = "." ] ; then
	    L=$PWD
	elif [ `echo "$L" | cut -c 1` != "/" ] ; then
    	    L="$PWD/$L"
    	fi

    	MAPSET=`basename "$L"`
    	L=`dirname "$L"`
    
    	if [ "$L" != "." ] ; then
    	    LOCATION_NAME=`basename "$L"`
    	    L=`dirname "$L"`
    
    	    if [ "$L" != "." ] ; then
    	    	GISDBASE="$L"
    	    fi
    	fi
    fi

    #strip off white space from LOCATION_NAME and MAPSET: only supported for $GISDBASE
    MAPSET=`echo $MAPSET | sed 's+ ++g'`
    LOCATION_NAME=`echo $LOCATION_NAME | sed 's+ ++g'`

    if [ "$GISDBASE" -a "$LOCATION_NAME" -a "$MAPSET" ] ; then
    	LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"

    	if [ ! -r "$LOCATION/WIND" ] ; then
		if [ "$LOCATION_NAME" = "PERMANENT" ] ; then
		   echo "$LOCATION: Not a valid GRASS location"
		   cleanup_tmp
		   exit 1
		else
		   # the user wants to create mapset on the fly
		   if [ -n "$CREATE_NEW" ] && [ "$CREATE_NEW" -eq 1 ] ; then
		     if [ ! -f "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" ] ; then
			echo "The LOCATION \"$LOCATION_NAME\" does not exist. Please create it first"
			cleanup_tmp
			exit 1
		     else
			mkdir -p "$LOCATION"
			cp "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" "$LOCATION/WIND"
			echo "Missing WIND file fixed"
		     fi
		   else
			echo "$LOCATION: Not a valid GRASS location"
			cleanup_tmp
			exit 1
		   fi
		fi
    	fi

    	if [ -s "$GISRC" ] ; then
    	    sed -e "s|^GISDBASE:.*$|GISDBASE: $GISDBASE|; \
    	    	s|^LOCATION_NAME:.*$|LOCATION_NAME: $LOCATION_NAME|; \
    	    	s|^MAPSET:.*$|MAPSET: $MAPSET|" "$GISRC" > "$GISRC.$$"
    
    	    if [ $? -eq 0 ] ; then
    	    	mv -f "$GISRC.$$" "$GISRC"
    	    else
    	    	rm -f "$GISRC.$$"
    	    	echo "Failed to create new $GISRC"
    	    	LOCATION=
    	    fi
    	else
    	    echo "GISDBASE: $GISDBASE" > "$GISRC"
    	    echo "LOCATION_NAME: $LOCATION_NAME" >> "$GISRC"
    	    echo "MAPSET: $MAPSET" >> "$GISRC"
    	fi
    else
    	echo "GISDBASE, LOCATION_NAME and MAPSET variables not set properly."
    	echo "Interactive startup needed."
	cleanup_tmp
    	exit 1
    fi
fi

# User selects LOCATION and MAPSET if not set
if [ ! "$LOCATION" ] ; then

    case "$GRASS_GUI" in

	# Check for text interface
	text)
	    "$ETC/set_data"

	    case $? in
     	    	0) ;;
     	    	*) 
		    # Check for an invalid GISRC file
		    if [ -f "$GISRC" ] ; then
			VALUE=`grep "GISDBASE" "$GISRC"`
			if [ "$VALUE" = "" ] ; then
    			    echo "Invalid resource file, removing $GISRC"
			    rm -f "$GISRC"
			fi
		    fi
		    
		    cleanup_tmp
		    exit
		    ;;
    	    esac
	    ;;

	# Check that GUI support software is functional
	tcltk | gis.m | oldtcltk | d.m | wxpython)

	    if [ "$GRASS_GUI" = "tcltk" ] || \
		[ "$GRASS_GUI" = "gis.m" ] || \
		[ "$GRASS_GUI" = "oldtcltk" ] || \
		[ "$GRASS_GUI" = "d.m" ] ; then

		    # eval `foo` will return subshell return code and not app foo return code!!!
		    eval '"$GRASS_WISH" -file "$TCLTKGRASSBASE/gis_set.tcl"'
		    thetest=$?
	    else
		    eval '"$GRASS_PYTHON" "$WXPYTHONGRASSBASE/gis_set.py"'
		    thetest=$?
	    fi


	    case $thetest in
     	    	1)

         	    # The gis_set script printed an error message so wait
		    # for user to read it
		    echo "Error in GUI startup. If necessary, please"
		    echo "report this error to the GRASS developers."
		    echo "Switching to text mode now."
		    echo "Hit RETURN to continue..."
		    read ans
		    
		    GRASS_GUI="text"

                    if [ -f "$GISRC" ] ; then
                        awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
                        echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
                        mv -f "$GISRC.$$" "$GISRC"
                    fi

		    "$ETC/set_data"

		    case $? in
     	    		0) ;;
     	    		*) 
			    # Check for an invalid GISRC file
			    if [ -f "$GISRC" ] ; then
				VALUE=`grep "GISDBASE" "$GISRC"`
				if [ "$VALUE" = "" ] ; then
    				    echo "Invalid resource file, removing $GISRC" 
				    rm -f "$GISRC"
				fi
			    fi

			    cleanup_tmp
			    exit
			    ;;
    		    esac
		    ;;
	    
     	    	0)
		    # These checks should not be necessary with real init stuff
		    if [ "$LOCATION_NAME" = "##NONE##" ] ; then
    	    		"$ETC/set_data"
    	    		if [ $? != 0 ]; then
    	    		    echo "GISDBASE: $OLD_DB" > "$GISRC"
    	    		    echo "LOCATION_NAME: $OLD_LOC" >> "$GISRC"
    	    		    echo "MAPSET: $OLD_MAP" >> "$GISRC"
    	    		    cleanup_tmp
			    exit
    	    		fi
    		    fi

		    if [ "$LOCATION_NAME" = "##ERROR##" ] ; then
    	    		echo "The selected location is not a valid GRASS location"
    	    		cleanup_tmp
			exit 1
		    fi

		    ;;
		    
		2)
			# User wants to exit from GRASS
			echo "Received EXIT message from GUI."
			echo "GRASS is not started. Bye."
			cleanup_tmp
			exit 0
			;;
		*)
		    echo "ERROR: Invalid return code from gis_set.tcl."
		    echo "Please advise GRASS developers of this error."
		    ;;
    	    esac
	    
	    ;;
	*)
	    # Shouldn't need this but you never know
	    echo "ERROR: Invalid user interface specified - <$GRASS_GUI>."
	    echo "Use the --help option to see valid interface names."
	    cleanup_tmp
	    exit 1
	    ;;
    esac
fi

GISDBASE=`g.gisenv GISDBASE`
LOCATION_NAME=`g.gisenv LOCATION_NAME`
MAPSET=`g.gisenv MAPSET`

if [ -z "$GISDBASE" ] || [ -z "$LOCATION_NAME" ] || [ -z "$MAPSET" ] ; then
    echo "ERROR: Reading data path information from g.gisenv."
    echo "GISDBASE=[$GISDBASE]"
    echo "LOCATION_NAME=[$LOCATION_NAME]"
    echo "MAPSET=[$MAPSET]"
    echo
    echo "Check the <$GISRCRC> file."
    cleanup_tmp
    exit 1
fi

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

# Check for concurrent use
lockfile="$LOCATION/.gislock"
"$ETC/lock" "$lockfile" $$
case $? in
    0) ;;
    2)
    	echo "$USER is currently running GRASS in selected mapset (file $lockfile found). Concurrent use not allowed."
    	cleanup_tmp
    	exit 1 ;;
    *)
    	echo Unable to properly access "$lockfile"
    	echo Please notify system personel.
	cleanup_tmp
    	exit 1 ;;
esac

# build user fontcap if specified but not present
if [ "$GRASS_FONT_CAP" ] && [ ! -f "$GRASS_FONT_CAP" ] ; then
	echo "Building user fontcap ..."
	g.mkfontcap
fi

# predefine default driver if DB connection not defined
#  is this really needed?? Modules should call this when/if required.
if [ ! -e "$LOCATION/VAR" ] ; then
   db.connect -c --quiet
fi

trap "" 2 3 15

# cygwin has many problems with the shell setup
# below, so i hardcoded everything here.
if [ "$CYGWIN" ] ; then
    sh="cygwin"
    shellname="GNU Bash (Cygwin)"
    export SHELL=/usr/bin/bash.exe
    export OSTYPE=cygwin
else 
    sh=`basename "$SHELL"`
    case "$sh" in
        ksh)  shellname="Korn Shell";;
        csh)  shellname="C Shell" ;;
        tcsh) shellname="TC Shell" ;;
        bash) shellname="Bash Shell" ;;
        sh)   shellname="Bourne Shell";;
        *)    shellname=shell;;
    esac
fi

# check for SHELL
if [ ! -x "$SHELL" ] ; then
    echo "ERROR: The SHELL variable is not set" 1>&2
    rm -f "$lockfile"
    cleanup_tmp
    exit 1
fi

# hack to process batch jobs:
if [ -n "$GRASS_BATCH_JOB" ] ; then
   # defined, but ...
   if [ ! -f "$GRASS_BATCH_JOB" ] ; then
      # wrong file
      echo "Job file '$GRASS_BATCH_JOB' has been defined in"
      echo "the 'GRASS_BATCH_JOB' variable but not found. Exiting."
      echo
      echo "Use 'unset GRASS_BATCH_JOB' to disable batch job processing."
      cleanup_tmp
      exit 1
   else
      # right file, but ...
      if [ ! -x "$GRASS_BATCH_JOB" ] ; then
	 echo "ERROR: change file permission to 'executable' for '$GRASS_BATCH_JOB'"
	 cleanup_tmp
	 exit 1
      else
         echo "Executing '$GRASS_BATCH_JOB' ..."
         GRASS_GUI="text"
         SHELL="$GRASS_BATCH_JOB"
      fi
   fi
fi

# Start the chosen GUI but ignore text
if [ "$GRASS_DEBUG" -ne 0 ] ; then
   echo "GRASS GUI should be $GRASS_GUI"
fi

case "$GRASS_GUI" in
    
    # Check for tcltk interface
    tcltk | gis.m)
	if [ "$sh" != "bash" ] && [ "$sh" != "msh" ] && [ "$sh" != "cygwin" ]; then
		# trap is not supported by csh/tcsh and rc
		"$GISBASE/scripts/gis.m"
	fi;
	;;
    oldtcltk | d.m)
	"$GISBASE/scripts/d.m"
	;;
    wxpython)
        "$GRASS_PYTHON" "$GISBASE/etc/wxpython/wxgui.py" &
	;;

    # Ignore others
    *)
    	;;
esac

# Display the version and license info
if [ "$MINGW" ] ; then
	:
# TODO: uncomment when PDCurses works.
#	cls
else
	if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
	   tput clear
	fi
fi


say_hello()
{
    if [ -f "$GISBASE/locale/$LCL/etc/welcome" ] ; then
	cat "$GISBASE/locale/$LCL/etc/welcome"
    else
	cat "$ETC/welcome"
    fi

}

if [ -n "$GRASS_BATCH_JOB" ] ; then
  say_hello
else
  cat <<EOF
          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\\\_  \\   / / __ / / \\__ \\ 
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ / 
       \____/_/ |_/_/  |_/____/____/   \____/___//____/  

EOF

  say_hello
  echo "GRASS homepage:                          http://grass.osgeo.org/"
  echo "This version running thru:               $shellname ($SHELL)"
  echo "Help is available with the command:      g.manual -i"
  echo "See the licence terms with:              g.version -c"

  case "$GRASS_GUI" in
     tcltk | gis.m)
        echo "If required, restart the GUI with:       g.gui tcltk"
        ;;
     oldtcltk | d.m)
        echo "If required, restart the GUI with:       g.gui oldtcltk"
        ;;
     wxpython)
        echo "If required, restart the GUI with:       g.gui wxpython"
        ;;
     *)
        echo "Start the GUI with:                      g.gui $DEFAULT_GUI"
        ;;
  esac

  echo "When ready to quit enter:                exit"
  echo
fi


case "$sh" in

csh|tcsh)
    USERHOME="$HOME"      # save original home
    HOME="$LOCATION"
    export HOME
    cshrc="$HOME/.cshrc"
    tcshrc="$HOME/.tcshrc"
    rm -f "$cshrc" "$tcshrc"
    echo "set home = $USERHOME" > "$cshrc"
    echo "set history = 3000 savehist = 3000  noclobber ignoreeof" >> "$cshrc"
    echo "set histfile = $HOME/.history" >> "$cshrc"

    echo "set prompt = '\\" >> "$cshrc"
    echo "Mapset <${MAPSET}> in Location <${LOCATION_NAME}> \\" >> "$cshrc"
    echo "GRASS GRASS_VERSION_NUMBER > '" >> "$cshrc"
    echo 'set BOGUS=``;unset BOGUS' >> "$cshrc"

    if [ -r "$USERHOME/.grass.cshrc" ]
    then
	cat "$USERHOME/.grass.cshrc" >> "$cshrc"
    fi

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

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

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

    echo "set path = ( $PATH ) " | sed 's/:/ /g' >> "$cshrc"

    cp "$cshrc" "$tcshrc"
    "$ETC/run" "$SHELL"
    EXIT_VAL=$?

    HOME="$USERHOME"
    export HOME
    ;;

bash|msh|cygwin)
    # save command history in mapset dir and remember more
    export HISTFILE="$LOCATION/.bash_history"
    if [ -z "$HISTSIZE" ] && [ -z "$HISTFILESIZE" ] ; then 
	export HISTSIZE=3000
    fi

    # instead of changing $HOME, start bash with: --rcfile "$LOCATION/.bashrc" ?
    #   if so, must care be taken to explicity call .grass.bashrc et al for
    #   non-interactive bash batch jobs?
    USERHOME="$HOME"      # save original home
    HOME="$LOCATION"      # save .bashrc in $LOCATION
    export HOME
    bashrc="$HOME/.bashrc"
    rm -f "$bashrc"
    echo "test -r ~/.alias && . ~/.alias" >> "$bashrc"
    echo "PS1='GRASS GRASS_VERSION_NUMBER ($LOCATION_NAME):\w > '" >> "$bashrc"
    # Use \"' to allow spaces in $GISBASE
    echo "PROMPT_COMMAND=\"'$GISBASE/etc/prompt.sh'\"" >> "$bashrc"

    if [ -r "$USERHOME/.grass.bashrc" ]
    then
        cat "$USERHOME/.grass.bashrc" >> "$bashrc"
    fi

    echo "export PATH=\"$PATH\"" >> "$bashrc"
    echo "export HOME=\"$USERHOME\"" >> "$bashrc" # restore user home path
    echo 'export GRASS_SHELL_PID=$$' >> "$bashrc" # can be used to terminate GRASS session from GUI
    if [ "$GRASS_GUI" = tcltk ] || [ "$GRASS_GUI" = gis.m ]; then
    	echo '$GISBASE/scripts/gis.m' >> "$bashrc" # Start gis.m
    fi;
    echo 'trap "echo \"GUI issued an exit\"; exit" SIGQUIT' >> "$bashrc"

    "$ETC/run" "$SHELL"
    EXIT_VAL=$?

    HOME="$USERHOME"
    export HOME
    ;;

*)
    if [ "$MINGW" ] ; then
	GRASS_BASE_VERSION=`echo "$GRASS_VERSION" | cut -f1,2 -d.`
	PS1="GRASS $GRASS_BASE_VERSION> "
	export PS1

	# "$ETC/run" doesn't work at all???
	"$SHELL"
	rm -rf "$LOCATION/.tmp"/*  # remove gis.m session files from .tmp
    else
	PS1="GRASS $GRASS_VERSION ($LOCATION_NAME):\w > "
	export PS1

	"$ETC/run" "$SHELL"
	EXIT_VAL=$?
    fi
    ;;
esac

trap 2 3 15

# GRASS session finished
if [ "$MINGW" ] ; then
	:
# TODO: uncomment when PDCurses works.
#	cls
else
	if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
	   tput clear
	fi
fi

echo "Closing monitors ..."
for MON  in `d.mon -L | grep running | grep -v "not running" | sed 's/ .*//'`
do
    if [ "$GRASS_DEBUG" -ne 0 ] ; then
       echo "d.mon stop=$MON"
    fi
    d.mon stop="$MON"
done

# Attempt to close any open gis.m instances.
if [ -n "$TCLTKGRASSBASE" ] && [ `ps -a | grep -c "$GRASS_WISH"` -ge 1 ] ; then
	echo "Closing open gis.m sessions....."
	echo 'foreach gwin [lsearch -all -inline [winfo interps] gm_tcl*] {
		catch {send -async $gwin Gm::remoteExit $env(GIS_LOCK)}
	}
	exit' | "$GRASS_WISH" #>/dev/null 2>&1
fi

echo "Cleaning up temporary files ..."

"$ETC/clean_temp" > /dev/null
rm -f "$lockfile"

# Save GISRC
cp "$GISRC" "$GISRCRC"


cleanup_tmp
#### after this point no more grass modules may be called ####

if [ -x "$GRASS_BATCH_JOB" ] ; then
   echo "Batch job '$GRASS_BATCH_JOB' (defined in GRASS_BATCH_JOB variable) was executed."
   echo "Goodbye from GRASS GIS"
   exit $EXIT_VAL
else
   echo "Done."
   echo
   echo
   echo
   echo "Goodbye from GRASS GIS"
   echo
fi