init.sh 27 KB

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