~ubuntu-branches/ubuntu/precise/grass/precise

« back to all changes in this revision

Viewing changes to .pc/www-browser/lib/init/init.sh

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-04-13 17:08:41 UTC
  • mfrom: (8.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110413170841-ss1t9bic0d0uq0gz
Tags: 6.4.1-1
* New upstream version.
* Now build-dep on libjpeg-dev and current libreadline6-dev.
* Removed patch swig: obsolete.
* Policy bumped to 3.9.2, without changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#############################################################################
 
3
#
 
4
# MODULE:       GRASS Initialization
 
5
# AUTHOR(S):    Original author unknown - probably CERL
 
6
#               Andreas Lange - Germany - andreas.lange@rhein-main.de
 
7
#               Huidae Cho - Korea - grass4u@gmail.com
 
8
#               Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
 
9
#               Markus Neteler - Germany/Italy - neteler@itc.it
 
10
#               Hamish Bowman - New Zealand - hamish_nospam at yahoo,com
 
11
# PURPOSE:      The source file for this shell script is in
 
12
#               src/general/init/init.sh. It sets up some environment
 
13
#               variables and the lock file. It also parses any remaining
 
14
#               command line options for setting the GISDBASE, LOCATION, and/or
 
15
#               MAPSET. Finally it starts GRASS with the appropriate user
 
16
#               interface and cleans up after it is finished.
 
17
# COPYRIGHT:    (C) 2000 by the GRASS Development Team
 
18
#
 
19
#               This program is free software under the GNU General Public
 
20
#               License (>=v2). Read the file COPYING that comes with GRASS
 
21
#               for details.
 
22
#
 
23
#############################################################################
 
24
 
 
25
trap "echo 'User break!' ; exit" 2 3 15
 
26
 
 
27
# Set default GUI
 
28
DEFAULT_GUI="wxpython"
 
29
 
 
30
# the following is only meant to be an internal variable for debugging this script.
 
31
#  use 'g.gisenv set="DEBUG=[0-5]"' to turn GRASS debug mode on properly.
 
32
if [ -z "$GRASS_DEBUG" ] ; then
 
33
   GRASS_DEBUG=0
 
34
fi
 
35
 
 
36
# Set the GRASS_PERL variable
 
37
GRASS_PERL=PERL_COMMAND
 
38
export GRASS_PERL
 
39
 
 
40
# GRASS_SH is normally just for Windows when not started from a bourne 
 
41
# shell. But when starting from Init.sh is still needed for Tcl/Tk.
 
42
GRASS_SH=/bin/sh
 
43
export GRASS_SH
 
44
 
 
45
# Set GRASS version number for R interface etc (must be an env_var for MS-Windows)
 
46
GRASS_VERSION="GRASS_VERSION_NUMBER"
 
47
export GRASS_VERSION
 
48
 
 
49
# Get the command name
 
50
CMD_NAME=START_UP
 
51
 
 
52
# Get the system name
 
53
SYSTEM=`uname -s`
 
54
case $SYSTEM in
 
55
MINGW*)
 
56
        MINGW=1
 
57
        ;;
 
58
CYGWIN*)
 
59
        CYGWIN=1
 
60
        ;;
 
61
Darwin*)
 
62
        MACOSX=1
 
63
        ;;
 
64
esac
 
65
 
 
66
 
 
67
# Go through the command line options
 
68
for i in "$@" ; do
 
69
    
 
70
    # Use a case to check the command line options
 
71
    case "$i" in
 
72
    
 
73
        # Check if the user asked for the version
 
74
        -v|--version)
 
75
            echo "GRASS GIS $GRASS_VERSION"
 
76
            echo
 
77
            cat "$GISBASE/etc/license"
 
78
            exit
 
79
            ;;
 
80
 
 
81
        # Check if the user asked for help
 
82
        help|-h|-help|--help)
 
83
            echo "Usage:"
 
84
            echo "  $CMD_NAME [-h | -help | --help] [-v | --version] [-c]"
 
85
            echo "          [-text | -gui | -tcltk | -oldtcltk | -wxpython | -wx]"
 
86
            echo "          [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]"
 
87
            echo
 
88
            echo "Flags:"
 
89
            echo "  -h or -help or --help          print this help message"
 
90
            echo "  -v or --version                show version information and exit"
 
91
            echo "  -c                             create given mapset if it doesn't exist"
 
92
            echo "  -text                          use text based interface"
 
93
            echo "                                   and set as default"
 
94
            echo "  -gui                           use graphical user interface ($DEFAULT_GUI by default)"
 
95
            echo "                                   and set as default"
 
96
            echo "  -tcltk                         use Tcl/Tk based graphical user interface"
 
97
            echo "                                   and set as default"
 
98
            echo "  -oldtcltk                      use old Tcl/Tk based graphical user interface"
 
99
            echo "                                   and set as default"
 
100
            echo "  -wxpython or -wx               use wxPython based graphical user interface"
 
101
            echo "                                   and set as default"
 
102
            echo
 
103
            echo "Parameters:"
 
104
            echo "  GISDBASE                       initial database (path to GIS data)"
 
105
            echo "  LOCATION_NAME                  initial location"
 
106
            echo "  MAPSET                         initial mapset"
 
107
            echo
 
108
            echo "  GISDBASE/LOCATION_NAME/MAPSET  fully qualified initial mapset directory"
 
109
            echo
 
110
            echo "Environment variables relevant for startup:"
 
111
            echo "  GRASS_GUI                      select GUI (text, gui, tcltk, oldtcltk, wxpython)"
 
112
            echo "  GRASS_TCLSH                    set tclsh shell name to override 'tclsh'"
 
113
            echo "  GRASS_WISH                     set wish shell name to override 'wish'"
 
114
            echo "  GRASS_HTML_BROWSER             set html web browser for help pages"
 
115
            echo "  GRASS_ADDON_PATH               set additional path(s) to local GRASS modules"
 
116
            echo "  GRASS_BATCH_JOB                shell script to be processed as batch job"
 
117
            echo "  GRASS_PYTHON                   set python shell name to override 'python'"
 
118
            exit
 
119
            ;;
 
120
 
 
121
        # Check if the -text flag was given
 
122
        -text)
 
123
            GRASS_GUI="text"
 
124
            shift
 
125
            ;;
 
126
 
 
127
        # Check if the -gui flag was given
 
128
        -gui)
 
129
            GRASS_GUI="$DEFAULT_GUI"
 
130
            shift
 
131
            ;;
 
132
 
 
133
        # Check if the -tcltk flag was given
 
134
        -tcltk)
 
135
            GRASS_GUI="tcltk"
 
136
            shift
 
137
            ;;
 
138
 
 
139
        # Check if the -oldtcltk flag was given
 
140
        -oldtcltk)
 
141
            GRASS_GUI="oldtcltk"
 
142
            shift
 
143
            ;;
 
144
 
 
145
        # Check if the -wxpython flag was given
 
146
        -wxpython | -wx)
 
147
            GRASS_GUI="wxpython"
 
148
            shift
 
149
            ;;
 
150
 
 
151
        # Check if the user wants to create a new mapset
 
152
        -c)
 
153
            CREATE_NEW=1
 
154
            shift
 
155
            ;;
 
156
    esac
 
157
done
 
158
 
 
159
# Set the GIS_LOCK variable to current process id
 
160
GIS_LOCK=$$
 
161
export GIS_LOCK
 
162
 
 
163
# Set the global grassrc file
 
164
if [ -n "$GRASS_BATCH_JOB" ] ; then
 
165
        GISRCRC="$HOME/.grassrc6.`uname -n`"
 
166
        if [ ! -f "$GISRCRC" ] ; then
 
167
                GISRCRC="$HOME/.grassrc6"
 
168
        fi
 
169
else
 
170
        GISRCRC="$HOME/.grassrc6"
 
171
fi
 
172
 
 
173
# Set the session grassrc file
 
174
if [ "$MINGW" ] ; then
 
175
        PWD=`pwd -W`
 
176
        USER="$USERNAME"
 
177
        if [ ! "$USER" ] ; then
 
178
                USER="user_name"
 
179
        fi
 
180
        if [ ! -f "$GISBASE/etc/monitorcap" ] ; then
 
181
                # create an empty monitorcap
 
182
                touch "$GISBASE/etc/monitorcap"
 
183
        fi
 
184
else
 
185
        PWD=`pwd`
 
186
        USER="`whoami`"
 
187
fi
 
188
 
 
189
 
 
190
# all exits after setting up $tmp should also tidy it up
 
191
cleanup_tmp()
 
192
{
 
193
  # remove session files from tmpdir
 
194
  rm -rf "$tmp"
 
195
}
 
196
 
 
197
## use TMPDIR if it exists, otherwise /tmp
 
198
#tmp=${TMPDIR-/tmp}
 
199
#tmp="$tmp/grass6-$USER-$GIS_LOCK"
 
200
tmp=/tmp/grass6-$USER-$GIS_LOCK
 
201
(umask 077 && mkdir "$tmp") || {
 
202
    echo "Cannot create temporary directory! Exiting." 1>&2
 
203
    exit 1
 
204
}
 
205
GISRC="$tmp/gisrc"
 
206
export GISRC
 
207
 
 
208
# remove invalid GISRC file to avoid disturbing error messages:
 
209
cat "$GISRCRC" 2>/dev/null| grep UNKNOWN >/dev/null
 
210
if [ $? -eq 0 ] ; then
 
211
   rm -f "$GISRCRC"
 
212
fi
 
213
 
 
214
# Copy the global grassrc file to the session grassrc file
 
215
if [ -f "$GISRCRC" ] ; then
 
216
    cp "$GISRCRC" "$GISRC"
 
217
    if [ $? -eq 1 ] ; then
 
218
        echo "Cannot copy '$GISRCRC' to '$GISRC'"
 
219
        cleanup_tmp
 
220
        exit 1
 
221
    fi
 
222
fi
 
223
 
 
224
# Copy global grassrc file to session grassrc
 
225
 
 
226
# At this point the GRASS user interface variable has been set from the
 
227
# command line, been set from an external environment variable, or is 
 
228
# not set. So we check if it is not set
 
229
if [ ! "$GRASS_GUI" ] ; then
 
230
 
 
231
    # Check for a reference to the GRASS user interface in the grassrc file
 
232
    if [ -f "$GISRC" ] ; then
 
233
        GRASS_GUI=`awk '/GRASS_GUI/ {print $2}' "$GISRC"`
 
234
    fi
 
235
    
 
236
    # Set the GRASS user interface to the default if needed
 
237
    if [ ! "$GRASS_GUI" ] ; then
 
238
        GRASS_GUI="$DEFAULT_GUI"
 
239
    fi
 
240
else
 
241
    if [ "$GRASS_GUI" = "gui" ] ; then
 
242
        GRASS_GUI="$DEFAULT_GUI"
 
243
    elif [ "$GRASS_GUI" = "wx" ] ; then
 
244
        GRASS_GUI="wxpython"
 
245
    fi
 
246
fi
 
247
 
 
248
# Set PATH to GRASS bin, ETC to GRASS etc
 
249
ETC="$GISBASE/etc"
 
250
 
 
251
if [ "$LC_ALL" ] ; then
 
252
        LCL=`echo "$LC_ALL" | sed 's/\(..\)\(.*\)/\1/'`
 
253
elif [ "$LC_MESSAGES" ] ; then
 
254
        LCL=`echo "$LC_MESSAGES" | sed 's/\(..\)\(.*\)/\1/'`
 
255
else
 
256
        LCL=`echo "$LANG" | sed 's/\(..\)\(.*\)/\1/'`
 
257
fi
 
258
 
 
259
if [ -n "$GRASS_ADDON_PATH" ] ; then
 
260
   PATH="$GISBASE/bin:$GISBASE/scripts:$GRASS_ADDON_PATH:$PATH"
 
261
else
 
262
   PATH="$GISBASE/bin:$GISBASE/scripts:$PATH"
 
263
fi
 
264
export PATH
 
265
 
 
266
# Set LD_LIBRARY_PATH to find GRASS shared libraries
 
267
if [ ! "$LD_LIBRARY_PATH_VAR" ] ; then
 
268
  LD_LIBRARY_PATH_VAR="$GISBASE/lib"
 
269
else
 
270
  LD_LIBRARY_PATH_VAR="$GISBASE/lib:$LD_LIBRARY_PATH_VAR"
 
271
fi
 
272
export LD_LIBRARY_PATH_VAR
 
273
# Additional copy of variable to use with grass-run.sh
 
274
GRASS_LD_LIBRARY_PATH="$LD_LIBRARY_PATH_VAR"
 
275
export GRASS_LD_LIBRARY_PATH
 
276
 
 
277
# Set some environment variables if they are not set
 
278
if [ ! "$GRASS_PAGER" ] ; then
 
279
    if [ -x /bin/more ] || [ -x /usr/bin/more ] ; then
 
280
        GRASS_PAGER=more
 
281
    elif [ -x /bin/less ] || [ -x /usr/bin/less ] ; then
 
282
        GRASS_PAGER=less
 
283
    elif [ "$MINGW" ] ; then
 
284
        GRASS_PAGER=more
 
285
    else
 
286
        GRASS_PAGER=cat
 
287
    fi
 
288
    export GRASS_PAGER
 
289
fi
 
290
 
 
291
# Set up tcltk and wish environment 
 
292
 
 
293
if [ ! "$GRASS_TCLSH" ] ; then
 
294
   GRASS_TCLSH=tclsh
 
295
   export GRASS_TCLSH
 
296
fi   
 
297
 
 
298
#WISH_OS=`echo 'puts $tcl_platform(platform) ; exit 0' | wish`
 
299
 
 
300
if [ ! "$GRASS_WISH" ] ; then
 
301
   GRASS_WISH=wish
 
302
   export GRASS_WISH
 
303
fi
 
304
 
 
305
if [ ! "$GRASS_PYTHON" ] ; then
 
306
    if [ "$MACOSX" ] ; then
 
307
        GRASS_PYTHON=pythonw
 
308
    else
 
309
        GRASS_PYTHON=python
 
310
    fi
 
311
    export GRASS_PYTHON
 
312
fi
 
313
 
 
314
# Set PYTHONPATH to find GRASS Python modules
 
315
if [ ! "$PYTHONPATH" ] ; then
 
316
    PYTHONPATH="$GISBASE/etc/python"
 
317
else
 
318
    PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
 
319
fi
 
320
export PYTHONPATH
 
321
if [ "$MINGW" ] ; then
 
322
    PATHEXT="${PATHEXT};.PY"
 
323
    export PATHEXT
 
324
fi
 
325
 
 
326
# try and find a web browser if one isn't already specified
 
327
if [ ! "$GRASS_HTML_BROWSER" ] ; then
 
328
 
 
329
    if [ "$MACOSX" ] ; then
 
330
        # OSX doesn't execute browsers from the shell PATH - route thru a script
 
331
        GRASS_HTML_BROWSER="$ETC/html_browser_mac.sh"
 
332
        GRASS_HTML_BROWSER_MACOSX="-b com.apple.helpviewer"
 
333
        export GRASS_HTML_BROWSER_MACOSX
 
334
 
 
335
    elif [ "$MINGW" -o "$CYGWIN" ] ; then
 
336
        # MinGW startup moved to into init.bat
 
337
        iexplore="$SYSTEMDRIVE/Program Files/Internet Explorer/iexplore.exe"
 
338
        if [ -f "$iexplore" ] ; then
 
339
            GRASS_HTML_BROWSER=$iexplore
 
340
        else
 
341
            GRASS_HTML_BROWSER="iexplore"
 
342
        fi
 
343
 
 
344
    else
 
345
      # the usual suspects
 
346
      BROWSERS="htmlview konqueror mozilla mozilla-firefox firefox opera netscape dillo"
 
347
      for BROWSER in $BROWSERS ; do
 
348
        for i in `echo "$PATH" | sed 's/^:/.:/
 
349
                                s/::/:.:/g
 
350
                                s/:$/:./
 
351
                                s/:/ /g'`
 
352
        do
 
353
          if [ -f "$i/$BROWSER" ] ; then  
 
354
            GRASS_HTML_BROWSER="$BROWSER"
 
355
            break
 
356
          fi
 
357
        done
 
358
        if [ -n "$GRASS_HTML_BROWSER" ] ; then
 
359
           break
 
360
        fi
 
361
      done
 
362
    fi
 
363
   
 
364
elif [ "$MACOSX" ] ; then
 
365
    # OSX doesn't execute browsers from the shell PATH - route thru a script
 
366
    GRASS_HTML_BROWSER_MACOSX="-b $GRASS_HTML_BROWSER"
 
367
    export GRASS_HTML_BROWSER_MACOSX
 
368
    GRASS_HTML_BROWSER="$ETC/html_browser_mac.sh"
 
369
fi
 
370
 
 
371
if [ ! "$GRASS_HTML_BROWSER" ] ; then
 
372
    echo "WARNING: Searched for a web browser, but none found." 1>&2
 
373
    # even so we set konqueror to make lib/gis/parser.c happy:
 
374
    GRASS_HTML_BROWSER=konqueror
 
375
fi
 
376
export GRASS_HTML_BROWSER
 
377
 
 
378
 
 
379
#predefine monitor size for certain architectures
 
380
if [ "$HOSTTYPE" = "arm" ] ; then
 
381
   #small monitor on ARM (iPAQ, zaurus... etc)
 
382
   GRASS_HEIGHT=320
 
383
   GRASS_WIDTH=240
 
384
   export GRASS_HEIGHT GRASS_WIDTH
 
385
fi
 
386
 
 
387
 
 
388
if [ ! "$GRASS_GNUPLOT" ] ; then
 
389
    GRASS_GNUPLOT="gnuplot -persist"
 
390
    export GRASS_GNUPLOT
 
391
fi
 
392
 
 
393
if [ ! "$GRASS_PROJSHARE" ] ; then
 
394
    GRASS_PROJSHARE=CONFIG_PROJSHARE
 
395
    export GRASS_PROJSHARE
 
396
fi
 
397
 
 
398
# First time user - GISRC is defined in the GRASS script
 
399
if [ ! -f "$GISRC" ] ; then
 
400
 
 
401
    if [ ! -f "$GISBASE/locale/$LCL/etc/grass_intro" ] ; then
 
402
        cat "$ETC/grass_intro"
 
403
    else
 
404
        cat "$GISBASE/locale/$LCL/etc/grass_intro"
 
405
    fi
 
406
 
 
407
    echo
 
408
    echo "Hit RETURN to continue"
 
409
    read ans
 
410
 
 
411
    #for convenience, define pwd as GISDBASE:
 
412
    echo "GISDBASE: $HOME" > "$GISRC"
 
413
    echo 'LOCATION_NAME: <UNKNOWN>' >> "$GISRC"
 
414
    echo 'MAPSET: <UNKNOWN>' >> "$GISRC"
 
415
 
 
416
    # This is a hack for not having a good initial gui - should be removed
 
417
    # with next version of initialization gui
 
418
    #GRASS_GUI="text"
 
419
    
 
420
else
 
421
    echo "Cleaning up temporary files ..."
 
422
    ("$ETC/clean_temp" > /dev/null &)
 
423
fi
 
424
 
 
425
 
 
426
echo "Starting GRASS ..."
 
427
 
 
428
# Check if we are running X windows by checking the DISPLAY variable
 
429
if [ "$DISPLAY" -o "$MINGW" ] ; then
 
430
 
 
431
    # Check if python is working properly
 
432
    if [ "$GRASS_GUI" = "wxpython" ]; then
 
433
        echo 'variable=True' | "$GRASS_PYTHON" >/dev/null 2>&1
 
434
    fi
 
435
    # Check if we need to find wish
 
436
    if [ "$GRASS_GUI" = "tcltk" ] || \
 
437
        [ "$GRASS_GUI" = "gis.m" ] || \
 
438
        [ "$GRASS_GUI" = "oldtcltk" ] || \
 
439
        [ "$GRASS_GUI" = "d.m" ] ; then
 
440
 
 
441
        # Check if wish is working properly
 
442
        echo 'exit 0' | "$GRASS_WISH" >/dev/null 2>&1
 
443
    fi
 
444
 
 
445
    # ok
 
446
    if [ "$?" = 0 ] ; then
 
447
        # Set the tcltkgrass base directory
 
448
        TCLTKGRASSBASE="$ETC"
 
449
        # Set the wxpython base directory
 
450
        WXPYTHONGRASSBASE="$ETC/wxpython"
 
451
    else
 
452
 
 
453
        # Wish was not found - switch to text interface mode
 
454
        echo
 
455
        echo "WARNING: The wish command does not work as expected!"
 
456
        echo "Please check your GRASS_WISH environment variable."
 
457
        echo "Use the -help option for details."
 
458
        echo "Switching to text based interface mode."
 
459
        echo
 
460
        echo "Hit RETURN to continue."
 
461
        read ans
 
462
 
 
463
        GRASS_GUI="text"
 
464
    fi
 
465
else
 
466
 
 
467
    # Display a message if a graphical interface was expected
 
468
    if [ "$GRASS_GUI" != "text" ] ; then
 
469
        # Set the interface mode to text
 
470
        echo
 
471
        echo "WARNING: It appears that the X Windows system is not active."
 
472
        echo "A graphical based user interface is not supported."
 
473
        echo "Switching to text based interface mode."
 
474
        echo
 
475
        echo "Hit RETURN to continue"
 
476
        read ans
 
477
 
 
478
        GRASS_GUI="text"
 
479
    fi
 
480
fi
 
481
 
 
482
# Save the user interface variable in the grassrc file - choose a temporary
 
483
# file name that should not match another file
 
484
if [ -f "$GISRC" ] ; then
 
485
    awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
 
486
    echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
 
487
    mv -f "$GISRC.$$" "$GISRC"
 
488
fi
 
489
 
 
490
# Parsing argument to get LOCATION
 
491
if [ ! "$1" ] ; then
 
492
 
 
493
    # Try interactive startup
 
494
    LOCATION=
 
495
else
 
496
 
 
497
    # Try non-interactive startup
 
498
    L=
 
499
    
 
500
    if [ "$1" = "-" ] ; then
 
501
    
 
502
        if [ "$LOCATION" ] ; then
 
503
            L="$LOCATION"
 
504
        fi
 
505
    else
 
506
        L="$1"
 
507
    fi
 
508
 
 
509
    if [ "$L" ] ; then
 
510
        if [ "$L" = "." ] ; then
 
511
            L=$PWD
 
512
        elif [ `echo "$L" | cut -c 1` != "/" ] ; then
 
513
            L="$PWD/$L"
 
514
        fi
 
515
 
 
516
        MAPSET=`basename "$L"`
 
517
        L=`dirname "$L"`
 
518
    
 
519
        if [ "$L" != "." ] ; then
 
520
            LOCATION_NAME=`basename "$L"`
 
521
            L=`dirname "$L"`
 
522
    
 
523
            if [ "$L" != "." ] ; then
 
524
                GISDBASE="$L"
 
525
            fi
 
526
        fi
 
527
    fi
 
528
 
 
529
    #strip off white space from LOCATION_NAME and MAPSET: only supported for $GISDBASE
 
530
    MAPSET=`echo $MAPSET | sed 's+ ++g'`
 
531
    LOCATION_NAME=`echo $LOCATION_NAME | sed 's+ ++g'`
 
532
 
 
533
    if [ "$GISDBASE" -a "$LOCATION_NAME" -a "$MAPSET" ] ; then
 
534
        LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
 
535
 
 
536
        if [ ! -r "$LOCATION/WIND" ] ; then
 
537
                if [ "$LOCATION_NAME" = "PERMANENT" ] ; then
 
538
                   echo "$LOCATION: Not a valid GRASS location"
 
539
                   cleanup_tmp
 
540
                   exit 1
 
541
                else
 
542
                   # the user wants to create mapset on the fly
 
543
                   if [ -n "$CREATE_NEW" ] && [ "$CREATE_NEW" -eq 1 ] ; then
 
544
                     if [ ! -f "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" ] ; then
 
545
                        echo "The LOCATION \"$LOCATION_NAME\" does not exist. Please create it first"
 
546
                        cleanup_tmp
 
547
                        exit 1
 
548
                     else
 
549
                        mkdir -p "$LOCATION"
 
550
                        cp "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" "$LOCATION/WIND"
 
551
                        echo "Missing WIND file fixed"
 
552
                     fi
 
553
                   else
 
554
                        echo "$LOCATION: Not a valid GRASS location"
 
555
                        cleanup_tmp
 
556
                        exit 1
 
557
                   fi
 
558
                fi
 
559
        fi
 
560
 
 
561
        if [ -s "$GISRC" ] ; then
 
562
            sed -e "s|^GISDBASE:.*$|GISDBASE: $GISDBASE|; \
 
563
                s|^LOCATION_NAME:.*$|LOCATION_NAME: $LOCATION_NAME|; \
 
564
                s|^MAPSET:.*$|MAPSET: $MAPSET|" "$GISRC" > "$GISRC.$$"
 
565
    
 
566
            if [ $? -eq 0 ] ; then
 
567
                mv -f "$GISRC.$$" "$GISRC"
 
568
            else
 
569
                rm -f "$GISRC.$$"
 
570
                echo "Failed to create new $GISRC"
 
571
                LOCATION=
 
572
            fi
 
573
        else
 
574
            echo "GISDBASE: $GISDBASE" > "$GISRC"
 
575
            echo "LOCATION_NAME: $LOCATION_NAME" >> "$GISRC"
 
576
            echo "MAPSET: $MAPSET" >> "$GISRC"
 
577
        fi
 
578
    else
 
579
        echo "GISDBASE, LOCATION_NAME and MAPSET variables not set properly."
 
580
        echo "Interactive startup needed."
 
581
        cleanup_tmp
 
582
        exit 1
 
583
    fi
 
584
fi
 
585
 
 
586
# User selects LOCATION and MAPSET if not set
 
587
if [ ! "$LOCATION" ] ; then
 
588
 
 
589
    case "$GRASS_GUI" in
 
590
 
 
591
        # Check for text interface
 
592
        text)
 
593
            "$ETC/set_data"
 
594
 
 
595
            case $? in
 
596
                0) ;;
 
597
                *) 
 
598
                    # Check for an invalid GISRC file
 
599
                    if [ -f "$GISRC" ] ; then
 
600
                        VALUE=`grep "GISDBASE" "$GISRC"`
 
601
                        if [ "$VALUE" = "" ] ; then
 
602
                            echo "Invalid resource file, removing $GISRC"
 
603
                            rm -f "$GISRC"
 
604
                        fi
 
605
                    fi
 
606
                    
 
607
                    cleanup_tmp
 
608
                    exit
 
609
                    ;;
 
610
            esac
 
611
            ;;
 
612
 
 
613
        # Check that GUI support software is functional
 
614
        tcltk | gis.m | oldtcltk | d.m | wxpython)
 
615
 
 
616
            if [ "$GRASS_GUI" = "tcltk" ] || \
 
617
                [ "$GRASS_GUI" = "gis.m" ] || \
 
618
                [ "$GRASS_GUI" = "oldtcltk" ] || \
 
619
                [ "$GRASS_GUI" = "d.m" ] ; then
 
620
 
 
621
                    # eval `foo` will return subshell return code and not app foo return code!!!
 
622
                    eval '"$GRASS_WISH" -file "$TCLTKGRASSBASE/gis_set.tcl"'
 
623
                    thetest=$?
 
624
            else
 
625
                    eval '"$GRASS_PYTHON" "$WXPYTHONGRASSBASE/gis_set.py"'
 
626
                    thetest=$?
 
627
            fi
 
628
 
 
629
 
 
630
            case $thetest in
 
631
                1)
 
632
 
 
633
                    # The gis_set script printed an error message so wait
 
634
                    # for user to read it
 
635
                    echo "Error in GUI startup. If necessary, please"
 
636
                    echo "report this error to the GRASS developers."
 
637
                    echo "Switching to text mode now."
 
638
                    echo "Hit RETURN to continue..."
 
639
                    read ans
 
640
                    
 
641
                    GRASS_GUI="text"
 
642
 
 
643
                    if [ -f "$GISRC" ] ; then
 
644
                        awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
 
645
                        echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
 
646
                        mv -f "$GISRC.$$" "$GISRC"
 
647
                    fi
 
648
 
 
649
                    "$ETC/set_data"
 
650
 
 
651
                    case $? in
 
652
                        0) ;;
 
653
                        *) 
 
654
                            # Check for an invalid GISRC file
 
655
                            if [ -f "$GISRC" ] ; then
 
656
                                VALUE=`grep "GISDBASE" "$GISRC"`
 
657
                                if [ "$VALUE" = "" ] ; then
 
658
                                    echo "Invalid resource file, removing $GISRC" 
 
659
                                    rm -f "$GISRC"
 
660
                                fi
 
661
                            fi
 
662
 
 
663
                            cleanup_tmp
 
664
                            exit
 
665
                            ;;
 
666
                    esac
 
667
                    ;;
 
668
            
 
669
                0)
 
670
                    # These checks should not be necessary with real init stuff
 
671
                    if [ "$LOCATION_NAME" = "##NONE##" ] ; then
 
672
                        "$ETC/set_data"
 
673
                        if [ $? != 0 ]; then
 
674
                            echo "GISDBASE: $OLD_DB" > "$GISRC"
 
675
                            echo "LOCATION_NAME: $OLD_LOC" >> "$GISRC"
 
676
                            echo "MAPSET: $OLD_MAP" >> "$GISRC"
 
677
                            cleanup_tmp
 
678
                            exit
 
679
                        fi
 
680
                    fi
 
681
 
 
682
                    if [ "$LOCATION_NAME" = "##ERROR##" ] ; then
 
683
                        echo "The selected location is not a valid GRASS location"
 
684
                        cleanup_tmp
 
685
                        exit 1
 
686
                    fi
 
687
 
 
688
                    ;;
 
689
                    
 
690
                2)
 
691
                        # User wants to exit from GRASS
 
692
                        echo "Received EXIT message from GUI."
 
693
                        echo "GRASS is not started. Bye."
 
694
                        cleanup_tmp
 
695
                        exit 0
 
696
                        ;;
 
697
                *)
 
698
                    echo "ERROR: Invalid return code from gis_set.tcl."
 
699
                    echo "Please advise GRASS developers of this error."
 
700
                    ;;
 
701
            esac
 
702
            
 
703
            ;;
 
704
        *)
 
705
            # Shouldn't need this but you never know
 
706
            echo "ERROR: Invalid user interface specified - <$GRASS_GUI>."
 
707
            echo "Use the --help option to see valid interface names."
 
708
            cleanup_tmp
 
709
            exit 1
 
710
            ;;
 
711
    esac
 
712
fi
 
713
 
 
714
GISDBASE=`g.gisenv GISDBASE`
 
715
LOCATION_NAME=`g.gisenv LOCATION_NAME`
 
716
MAPSET=`g.gisenv MAPSET`
 
717
 
 
718
if [ -z "$GISDBASE" ] || [ -z "$LOCATION_NAME" ] || [ -z "$MAPSET" ] ; then
 
719
    echo "ERROR: Reading data path information from g.gisenv."
 
720
    echo "GISDBASE=[$GISDBASE]"
 
721
    echo "LOCATION_NAME=[$LOCATION_NAME]"
 
722
    echo "MAPSET=[$MAPSET]"
 
723
    echo
 
724
    echo "Check the <$GISRCRC> file."
 
725
    cleanup_tmp
 
726
    exit 1
 
727
fi
 
728
 
 
729
LOCATION="${GISDBASE?}/${LOCATION_NAME?}/${MAPSET?}"
 
730
 
 
731
# Check for concurrent use
 
732
lockfile="$LOCATION/.gislock"
 
733
"$ETC/lock" "$lockfile" $$
 
734
case $? in
 
735
    0) ;;
 
736
    2)
 
737
        echo "$USER is currently running GRASS in selected mapset (file $lockfile found). Concurrent use not allowed."
 
738
        cleanup_tmp
 
739
        exit 1 ;;
 
740
    *)
 
741
        echo Unable to properly access "$lockfile"
 
742
        echo Please notify system personel.
 
743
        cleanup_tmp
 
744
        exit 1 ;;
 
745
esac
 
746
 
 
747
# build user fontcap if specified but not present
 
748
if [ "$GRASS_FONT_CAP" ] && [ ! -f "$GRASS_FONT_CAP" ] ; then
 
749
        echo "Building user fontcap ..."
 
750
        g.mkfontcap
 
751
fi
 
752
 
 
753
# predefine default driver if DB connection not defined
 
754
#  is this really needed?? Modules should call this when/if required.
 
755
if [ ! -e "$LOCATION/VAR" ] ; then
 
756
   db.connect -c --quiet
 
757
fi
 
758
 
 
759
trap "" 2 3 15
 
760
 
 
761
# cygwin has many problems with the shell setup
 
762
# below, so i hardcoded everything here.
 
763
if [ "$CYGWIN" ] ; then
 
764
    sh="cygwin"
 
765
    shellname="GNU Bash (Cygwin)"
 
766
    export SHELL=/usr/bin/bash.exe
 
767
    export OSTYPE=cygwin
 
768
else 
 
769
    sh=`basename "$SHELL"`
 
770
    case "$sh" in
 
771
        ksh)  shellname="Korn Shell";;
 
772
        csh)  shellname="C Shell" ;;
 
773
        tcsh) shellname="TC Shell" ;;
 
774
        bash) shellname="Bash Shell" ;;
 
775
        sh)   shellname="Bourne Shell";;
 
776
        *)    shellname=shell;;
 
777
    esac
 
778
fi
 
779
 
 
780
# check for SHELL
 
781
if [ ! -x "$SHELL" ] ; then
 
782
    echo "ERROR: The SHELL variable is not set" 1>&2
 
783
    rm -f "$lockfile"
 
784
    cleanup_tmp
 
785
    exit 1
 
786
fi
 
787
 
 
788
# hack to process batch jobs:
 
789
if [ -n "$GRASS_BATCH_JOB" ] ; then
 
790
   # defined, but ...
 
791
   if [ ! -f "$GRASS_BATCH_JOB" ] ; then
 
792
      # wrong file
 
793
      echo "Job file '$GRASS_BATCH_JOB' has been defined in"
 
794
      echo "the 'GRASS_BATCH_JOB' variable but not found. Exiting."
 
795
      echo
 
796
      echo "Use 'unset GRASS_BATCH_JOB' to disable batch job processing."
 
797
      cleanup_tmp
 
798
      exit 1
 
799
   else
 
800
      # right file, but ...
 
801
      if [ ! -x "$GRASS_BATCH_JOB" ] ; then
 
802
         echo "ERROR: change file permission to 'executable' for '$GRASS_BATCH_JOB'"
 
803
         cleanup_tmp
 
804
         exit 1
 
805
      else
 
806
         echo "Executing '$GRASS_BATCH_JOB' ..."
 
807
         GRASS_GUI="text"
 
808
         SHELL="$GRASS_BATCH_JOB"
 
809
      fi
 
810
   fi
 
811
fi
 
812
 
 
813
# Start the chosen GUI but ignore text
 
814
if [ "$GRASS_DEBUG" -ne 0 ] ; then
 
815
   echo "GRASS GUI should be $GRASS_GUI"
 
816
fi
 
817
 
 
818
case "$GRASS_GUI" in
 
819
    
 
820
    # Check for tcltk interface
 
821
    tcltk | gis.m)
 
822
       if [ "$sh" != "bash" ] && [ "$sh" != "msh" ] && [ "$sh" != "cygwin" ]; then
 
823
               # trap is not supported by csh/tcsh and rc
 
824
               "$GISBASE/scripts/gis.m"
 
825
       fi;
 
826
        ;;
 
827
    oldtcltk | d.m)
 
828
        "$GISBASE/scripts/d.m"
 
829
        ;;
 
830
    wxpython)
 
831
        "$GRASS_PYTHON" "$GISBASE/etc/wxpython/wxgui.py" &
 
832
        ;;
 
833
 
 
834
    # Ignore others
 
835
    *)
 
836
        ;;
 
837
esac
 
838
 
 
839
# Display the version and license info
 
840
if [ "$MINGW" ] ; then
 
841
        :
 
842
# TODO: uncomment when PDCurses works.
 
843
#       cls
 
844
else
 
845
        if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
 
846
           tput clear
 
847
        fi
 
848
fi
 
849
 
 
850
 
 
851
say_hello()
 
852
{
 
853
    if [ -f "$GISBASE/locale/$LCL/etc/welcome" ] ; then
 
854
        cat "$GISBASE/locale/$LCL/etc/welcome"
 
855
    else
 
856
        cat "$ETC/welcome"
 
857
    fi
 
858
 
 
859
}
 
860
 
 
861
if [ -n "$GRASS_BATCH_JOB" ] ; then
 
862
  say_hello
 
863
else
 
864
  cat <<EOF
 
865
          __________  ___   __________    _______________
 
866
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
 
867
        / / __/ /_/ / /| | \__ \\\\_  \\   / / __ / / \\__ \\ 
 
868
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ / 
 
869
       \____/_/ |_/_/  |_/____/____/   \____/___//____/  
 
870
 
 
871
EOF
 
872
 
 
873
  say_hello
 
874
  echo "GRASS homepage:                          http://grass.osgeo.org/"
 
875
  echo "This version running thru:               $shellname ($SHELL)"
 
876
  echo "Help is available with the command:      g.manual -i"
 
877
  echo "See the licence terms with:              g.version -c"
 
878
 
 
879
  case "$GRASS_GUI" in
 
880
     tcltk | gis.m)
 
881
        echo "If required, restart the GUI with:       g.gui tcltk"
 
882
        ;;
 
883
     oldtcltk | d.m)
 
884
        echo "If required, restart the GUI with:       g.gui oldtcltk"
 
885
        ;;
 
886
     wxpython)
 
887
        echo "If required, restart the GUI with:       g.gui wxpython"
 
888
        ;;
 
889
     *)
 
890
        echo "Start the GUI with:                      g.gui $DEFAULT_GUI"
 
891
        ;;
 
892
  esac
 
893
 
 
894
  echo "When ready to quit enter:                exit"
 
895
  echo
 
896
fi
 
897
 
 
898
 
 
899
case "$sh" in
 
900
 
 
901
csh|tcsh)
 
902
    USERHOME="$HOME"      # save original home
 
903
    HOME="$LOCATION"
 
904
    export HOME
 
905
    cshrc="$HOME/.cshrc"
 
906
    tcshrc="$HOME/.tcshrc"
 
907
    rm -f "$cshrc" "$tcshrc"
 
908
    echo "set home = $USERHOME" > "$cshrc"
 
909
    echo "set history = 3000 savehist = 3000  noclobber ignoreeof" >> "$cshrc"
 
910
    echo "set histfile = $HOME/.history" >> "$cshrc"
 
911
 
 
912
    echo "set prompt = '\\" >> "$cshrc"
 
913
    echo "Mapset <${MAPSET}> in Location <${LOCATION_NAME}> \\" >> "$cshrc"
 
914
    echo "GRASS GRASS_VERSION_NUMBER > '" >> "$cshrc"
 
915
    echo 'set BOGUS=``;unset BOGUS' >> "$cshrc"
 
916
 
 
917
    if [ -r "$USERHOME/.grass.cshrc" ]
 
918
    then
 
919
        cat "$USERHOME/.grass.cshrc" >> "$cshrc"
 
920
    fi
 
921
 
 
922
    if [ -r "$USERHOME/.cshrc" ]
 
923
    then
 
924
        grep '^ *set  *mail *= *' "$USERHOME/.cshrc" >> "$cshrc"
 
925
    fi
 
926
 
 
927
    if [ -r "$USERHOME/.tcshrc" ]
 
928
    then
 
929
        grep '^ *set  *mail *= *' "$USERHOME/.tcshrc" >> "$cshrc"
 
930
    fi
 
931
 
 
932
    if [ -r "$USERHOME/.login" ]
 
933
    then
 
934
        grep '^ *set  *mail *= *' "$USERHOME/.login" >> "$cshrc"
 
935
    fi
 
936
 
 
937
    echo "set path = ( $PATH ) " | sed 's/:/ /g' >> "$cshrc"
 
938
 
 
939
    cp "$cshrc" "$tcshrc"
 
940
    "$ETC/run" "$SHELL"
 
941
    EXIT_VAL=$?
 
942
 
 
943
    HOME="$USERHOME"
 
944
    export HOME
 
945
    ;;
 
946
 
 
947
bash|msh|cygwin)
 
948
    # save command history in mapset dir and remember more
 
949
    export HISTFILE="$LOCATION/.bash_history"
 
950
    if [ -z "$HISTSIZE" ] && [ -z "$HISTFILESIZE" ] ; then 
 
951
        export HISTSIZE=3000
 
952
    fi
 
953
 
 
954
    # instead of changing $HOME, start bash with: --rcfile "$LOCATION/.bashrc" ?
 
955
    #   if so, must care be taken to explicity call .grass.bashrc et al for
 
956
    #   non-interactive bash batch jobs?
 
957
    USERHOME="$HOME"      # save original home
 
958
    HOME="$LOCATION"      # save .bashrc in $LOCATION
 
959
    export HOME
 
960
    bashrc="$HOME/.bashrc"
 
961
    rm -f "$bashrc"
 
962
    echo "test -r ~/.alias && . ~/.alias" >> "$bashrc"
 
963
    echo "PS1='GRASS GRASS_VERSION_NUMBER ($LOCATION_NAME):\w > '" >> "$bashrc"
 
964
    # Use \"' to allow spaces in $GISBASE
 
965
    echo "PROMPT_COMMAND=\"'$GISBASE/etc/prompt.sh'\"" >> "$bashrc"
 
966
 
 
967
    if [ -r "$USERHOME/.grass.bashrc" ]
 
968
    then
 
969
        cat "$USERHOME/.grass.bashrc" >> "$bashrc"
 
970
    fi
 
971
 
 
972
    echo "export PATH=\"$PATH\"" >> "$bashrc"
 
973
    echo "export HOME=\"$USERHOME\"" >> "$bashrc" # restore user home path
 
974
    echo 'export GRASS_SHELL_PID=$$' >> "$bashrc" # can be used to terminate GRASS session from GUI
 
975
    if [ "$GRASS_GUI" = tcltk ] || [ "$GRASS_GUI" = gis.m ]; then
 
976
       echo '$GISBASE/scripts/gis.m' >> "$bashrc" # Start gis.m
 
977
    fi;
 
978
    echo 'trap "echo \"GUI issued an exit\"; exit" SIGQUIT' >> "$bashrc"
 
979
 
 
980
    "$ETC/run" "$SHELL"
 
981
    EXIT_VAL=$?
 
982
 
 
983
    HOME="$USERHOME"
 
984
    export HOME
 
985
    ;;
 
986
 
 
987
*)
 
988
    if [ "$MINGW" ] ; then
 
989
        GRASS_BASE_VERSION=`echo "$GRASS_VERSION" | cut -f1,2 -d.`
 
990
        PS1="GRASS $GRASS_BASE_VERSION> "
 
991
        export PS1
 
992
 
 
993
        # "$ETC/run" doesn't work at all???
 
994
        "$SHELL"
 
995
        rm -rf "$LOCATION/.tmp"/*  # remove gis.m session files from .tmp
 
996
    else
 
997
        PS1="GRASS $GRASS_VERSION ($LOCATION_NAME):\w > "
 
998
        export PS1
 
999
 
 
1000
        "$ETC/run" "$SHELL"
 
1001
        EXIT_VAL=$?
 
1002
    fi
 
1003
    ;;
 
1004
esac
 
1005
 
 
1006
trap 2 3 15
 
1007
 
 
1008
# GRASS session finished
 
1009
if [ "$MINGW" ] ; then
 
1010
        :
 
1011
# TODO: uncomment when PDCurses works.
 
1012
#       cls
 
1013
else
 
1014
        if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
 
1015
           tput clear
 
1016
        fi
 
1017
fi
 
1018
 
 
1019
echo "Closing monitors ..."
 
1020
for MON  in `d.mon -L | grep running | grep -v "not running" | sed 's/ .*//'`
 
1021
do
 
1022
    if [ "$GRASS_DEBUG" -ne 0 ] ; then
 
1023
       echo "d.mon stop=$MON"
 
1024
    fi
 
1025
    d.mon stop="$MON"
 
1026
done
 
1027
 
 
1028
# Attempt to close any open gis.m instances.
 
1029
if [ -n "$TCLTKGRASSBASE" ] && [ `ps -a | grep -c "$GRASS_WISH"` -ge 1 ] ; then
 
1030
       echo "Closing open gis.m sessions....."
 
1031
       echo 'foreach gwin [lsearch -all -inline [winfo interps] gm_tcl*] {
 
1032
               catch {send -async $gwin Gm::remoteExit $env(GIS_LOCK)}
 
1033
       }
 
1034
       exit' | "$GRASS_WISH" #>/dev/null 2>&1
 
1035
fi
 
1036
 
 
1037
echo "Cleaning up temporary files ..."
 
1038
 
 
1039
"$ETC/clean_temp" > /dev/null
 
1040
rm -f "$lockfile"
 
1041
 
 
1042
# Save GISRC
 
1043
cp "$GISRC" "$GISRCRC"
 
1044
 
 
1045
 
 
1046
cleanup_tmp
 
1047
#### after this point no more grass modules may be called ####
 
1048
 
 
1049
if [ -x "$GRASS_BATCH_JOB" ] ; then
 
1050
   echo "Batch job '$GRASS_BATCH_JOB' (defined in GRASS_BATCH_JOB variable) was executed."
 
1051
   echo "Goodbye from GRASS GIS"
 
1052
   exit $EXIT_VAL
 
1053
else
 
1054
   echo "Done."
 
1055
   echo
 
1056
   echo
 
1057
   echo
 
1058
   echo "Goodbye from GRASS GIS"
 
1059
   echo
 
1060
fi
 
1061