init.sh 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. # change to wxpython as needed
  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. # try and find a web browser if one isn't already specified
  258. if [ ! "$GRASS_HTML_BROWSER" ] ; then
  259. if [ "$MACOSX" ] ; then
  260. # OSX doesn't execute browsers from the shell PATH - route thru a script
  261. GRASS_HTML_BROWSER="$ETC/html_browser_mac.sh"
  262. GRASS_HTML_BROWSER_MACOSX="-b com.apple.helpviewer"
  263. export GRASS_HTML_BROWSER_MACOSX
  264. elif [ "$MINGW" -o "$CYGWIN" ] ; then
  265. # MinGW startup moved to into init.bat
  266. iexplore="$SYSTEMDRIVE/Program Files/Internet Explorer/iexplore.exe"
  267. if [ -f "$iexplore" ] ; then
  268. GRASS_HTML_BROWSER=$iexplore
  269. else
  270. GRASS_HTML_BROWSER="iexplore"
  271. fi
  272. else
  273. # the usual suspects
  274. BROWSERS="htmlview konqueror mozilla mozilla-firefox firefox opera netscape dillo"
  275. for BROWSER in $BROWSERS ; do
  276. for i in `echo "$PATH" | sed 's/^:/.:/
  277. s/::/:.:/g
  278. s/:$/:./
  279. s/:/ /g'`
  280. do
  281. if [ -f "$i/$BROWSER" ] ; then
  282. GRASS_HTML_BROWSER="$BROWSER"
  283. break
  284. fi
  285. done
  286. if [ -n "$GRASS_HTML_BROWSER" ] ; then
  287. break
  288. fi
  289. done
  290. fi
  291. elif [ "$MACOSX" ] ; then
  292. # OSX doesn't execute browsers from the shell PATH - route thru a script
  293. GRASS_HTML_BROWSER_MACOSX="-b $GRASS_HTML_BROWSER"
  294. export GRASS_HTML_BROWSER_MACOSX
  295. GRASS_HTML_BROWSER="$ETC/html_browser_mac.sh"
  296. fi
  297. if [ ! "$GRASS_HTML_BROWSER" ] ; then
  298. echo "WARNING: Searched for a web browser, but none found." 1>&2
  299. # even so we set konqueror to make lib/gis/parser.c happy:
  300. GRASS_HTML_BROWSER=konqueror
  301. fi
  302. export GRASS_HTML_BROWSER
  303. #predefine monitor size for certain architectures
  304. if [ "$HOSTTYPE" = "arm" ] ; then
  305. #small monitor on ARM (iPAQ, zaurus... etc)
  306. GRASS_HEIGHT=320
  307. GRASS_WIDTH=240
  308. export GRASS_HEIGHT GRASS_WIDTH
  309. fi
  310. if [ ! "$GRASS_GNUPLOT" ] ; then
  311. GRASS_GNUPLOT="gnuplot -persist"
  312. export GRASS_GNUPLOT
  313. fi
  314. if [ ! "$GRASS_PROJSHARE" ] ; then
  315. GRASS_PROJSHARE=CONFIG_PROJSHARE
  316. export GRASS_PROJSHARE
  317. fi
  318. # First time user - GISRC is defined in the GRASS script
  319. if [ ! -f "$GISRC" ] ; then
  320. if [ ! -f "$GISBASE/locale/$LCL/etc/grass_intro" ] ; then
  321. cat "$ETC/grass_intro"
  322. else
  323. cat "$GISBASE/locale/$LCL/etc/grass_intro"
  324. fi
  325. echo
  326. echo "Hit RETURN to continue"
  327. read ans
  328. #for convenience, define pwd as GISDBASE:
  329. echo "GISDBASE: $PWD" > "$GISRC"
  330. echo 'LOCATION_NAME: <UNKNOWN>' >> "$GISRC"
  331. echo 'MAPSET: <UNKNOWN>' >> "$GISRC"
  332. # This is a hack for not having a good initial gui - should be removed
  333. # with next version of initialization gui
  334. #GRASS_GUI="text"
  335. else
  336. echo "Cleaning up temporary files ..."
  337. ("$ETC/clean_temp" > /dev/null &)
  338. fi
  339. echo "Starting GRASS ..."
  340. # Check if we are running X windows by checking the DISPLAY variable
  341. if [ "$DISPLAY" -o "$MINGW" ] ; then
  342. # Check if python is working properly
  343. if [ "$GRASS_GUI" = "wxpython" ]; then
  344. echo 'variable=True' | "$GRASS_PYTHON" >/dev/null 2>&1
  345. fi
  346. # Check if we need to find wish
  347. if [ "$GRASS_GUI" = "tcltk" ] || \
  348. [ "$GRASS_GUI" = "gis.m" ] || \
  349. [ "$GRASS_GUI" = "oldtcltk" ] || \
  350. [ "$GRASS_GUI" = "d.m" ] ; then
  351. # Check if wish is working properly
  352. echo 'exit 0' | "$GRASS_WISH" >/dev/null 2>&1
  353. fi
  354. # ok
  355. if [ "$?" = 0 ] ; then
  356. # Set the tcltkgrass base directory
  357. TCLTKGRASSBASE="$ETC"
  358. # Set the wxpython base directory
  359. WXPYTHONGRASSBASE="$ETC/wxpython"
  360. else
  361. # Wish was not found - switch to text interface mode
  362. echo
  363. echo "WARNING: The wish command does not work as expected!"
  364. echo "Please check your GRASS_WISH environment variable."
  365. echo "Use the -help option for details."
  366. echo "Switching to text based interface mode."
  367. echo
  368. echo "Hit RETURN to continue."
  369. read ans
  370. GRASS_GUI="text"
  371. fi
  372. else
  373. # Display a message if a graphical interface was expected
  374. if [ "$GRASS_GUI" != "text" ] ; then
  375. # Set the interface mode to text
  376. echo
  377. echo "WARNING: It appears that the X Windows system is not active."
  378. echo "A graphical based user interface is not supported."
  379. echo "Switching to text based interface mode."
  380. echo
  381. echo "Hit RETURN to continue"
  382. read ans
  383. GRASS_GUI="text"
  384. fi
  385. fi
  386. # Save the user interface variable in the grassrc file - choose a temporary
  387. # file name that should not match another file
  388. if [ -f "$GISRC" ] ; then
  389. awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
  390. echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
  391. mv -f "$GISRC.$$" "$GISRC"
  392. fi
  393. # Parsing argument to get LOCATION
  394. if [ ! "$1" ] ; then
  395. # Try interactive startup
  396. LOCATION=
  397. else
  398. # Try non-interactive startup
  399. L=
  400. if [ "$1" = "-" ] ; then
  401. if [ "$LOCATION" ] ; then
  402. L="$LOCATION"
  403. fi
  404. else
  405. L="$1"
  406. fi
  407. if [ "$L" ] ; then
  408. if [ "$L" = "." ] ; then
  409. L=$PWD
  410. elif [ `echo "$L" | cut -c 1` != "/" ] ; then
  411. L="$PWD/$L"
  412. fi
  413. MAPSET=`basename "$L"`
  414. L=`dirname "$L"`
  415. if [ "$L" != "." ] ; then
  416. LOCATION_NAME=`basename "$L"`
  417. L=`dirname "$L"`
  418. if [ "$L" != "." ] ; then
  419. GISDBASE="$L"
  420. fi
  421. fi
  422. fi
  423. #strip off white space from LOCATION_NAME and MAPSET: only supported for $GISDBASE
  424. MAPSET=`echo $MAPSET | sed 's+ ++g'`
  425. LOCATION_NAME=`echo $LOCATION_NAME | sed 's+ ++g'`
  426. if [ "$GISDBASE" -a "$LOCATION_NAME" -a "$MAPSET" ] ; then
  427. LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
  428. if [ ! -r "$LOCATION/WIND" ] ; then
  429. if [ "$LOCATION_NAME" = "PERMANENT" ] ; then
  430. echo "$LOCATION: Not a valid GRASS location"
  431. cleanup_tmp
  432. exit 1
  433. else
  434. # the user wants to create mapset on the fly
  435. if [ -n "$CREATE_NEW" ] && [ "$CREATE_NEW" -eq 1 ] ; then
  436. if [ ! -f "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" ] ; then
  437. echo "The LOCATION \"$LOCATION_NAME\" does not exist. Please create it first"
  438. cleanup_tmp
  439. exit 1
  440. else
  441. mkdir -p "$LOCATION"
  442. cp "$GISDBASE/$LOCATION_NAME/PERMANENT/DEFAULT_WIND" "$LOCATION/WIND"
  443. echo "Missing WIND file fixed"
  444. fi
  445. else
  446. echo "$LOCATION: Not a valid GRASS location"
  447. cleanup_tmp
  448. exit 1
  449. fi
  450. fi
  451. fi
  452. if [ -s "$GISRC" ] ; then
  453. sed -e "s|^GISDBASE:.*$|GISDBASE: $GISDBASE|; \
  454. s|^LOCATION_NAME:.*$|LOCATION_NAME: $LOCATION_NAME|; \
  455. s|^MAPSET:.*$|MAPSET: $MAPSET|" "$GISRC" > "$GISRC.$$"
  456. if [ $? -eq 0 ] ; then
  457. mv -f "$GISRC.$$" "$GISRC"
  458. else
  459. rm -f "$GISRC.$$"
  460. echo "Failed to create new $GISRC"
  461. LOCATION=
  462. fi
  463. else
  464. echo "GISDBASE: $GISDBASE" > "$GISRC"
  465. echo "LOCATION_NAME: $LOCATION_NAME" >> "$GISRC"
  466. echo "MAPSET: $MAPSET" >> "$GISRC"
  467. fi
  468. else
  469. echo "GISDBASE, LOCATION_NAME and MAPSET variables not set properly."
  470. echo "Interactive startup needed."
  471. cleanup_tmp
  472. exit 1
  473. fi
  474. fi
  475. # User selects LOCATION and MAPSET if not set
  476. if [ ! "$LOCATION" ] ; then
  477. case "$GRASS_GUI" in
  478. # Check for text interface
  479. text)
  480. "$ETC/set_data"
  481. case $? in
  482. 0) ;;
  483. *)
  484. # Check for an invalid GISRC file
  485. if [ -f "$GISRC" ] ; then
  486. VALUE=`grep "GISDBASE" "$GISRC"`
  487. if [ "$VALUE" = "" ] ; then
  488. echo "Invalid resource file, removing $GISRC"
  489. rm -f "$GISRC"
  490. fi
  491. fi
  492. cleanup_tmp
  493. exit
  494. ;;
  495. esac
  496. ;;
  497. # Check for tcltk interface
  498. tcltk | gis.m | oldtcltk | d.m | wxpython)
  499. if [ "$GRASS_GUI" = "tcltk" ] || \
  500. [ "$GRASS_GUI" = "gis.m" ] || \
  501. [ "$GRASS_GUI" = "oldtcltk" ] || \
  502. [ "$GRASS_GUI" = "d.m" ] ; then
  503. # eval `foo` will return subshell return code and not app foo return code!!!
  504. eval '"$GRASS_WISH" -file "$TCLTKGRASSBASE/gis_set.tcl"'
  505. thetest=$?
  506. else
  507. eval '"$GRASS_PYTHON" "$WXPYTHONGRASSBASE/gis_set.py"'
  508. thetest=$?
  509. fi
  510. case $thetest in
  511. 1)
  512. # The gis_set.tcl script printed an error message so wait
  513. # for user to read it
  514. echo "Error in GUI startup. If necessary, please"
  515. echo "report this error to the GRASS developers."
  516. echo "Switching to text mode now."
  517. echo "Hit RETURN to continue..."
  518. read ans
  519. GRASS_GUI="text"
  520. if [ -f "$GISRC" ] ; then
  521. awk '$1 !~ /GRASS_GUI/ {print}' "$GISRC" > "$GISRC.$$"
  522. echo "GRASS_GUI: $GRASS_GUI" >> "$GISRC.$$"
  523. mv -f "$GISRC.$$" "$GISRC"
  524. fi
  525. "$ETC/set_data"
  526. case $? in
  527. 0) ;;
  528. *)
  529. # Check for an invalid GISRC file
  530. if [ -f "$GISRC" ] ; then
  531. VALUE=`grep "GISDBASE" "$GISRC"`
  532. if [ "$VALUE" = "" ] ; then
  533. echo "Invalid resource file, removing $GISRC"
  534. rm -f "$GISRC"
  535. fi
  536. fi
  537. cleanup_tmp
  538. exit
  539. ;;
  540. esac
  541. ;;
  542. 0)
  543. # These checks should not be necessary with real init stuff
  544. if [ "$LOCATION_NAME" = "##NONE##" ] ; then
  545. "$ETC/set_data"
  546. if [ $? != 0 ]; then
  547. echo "GISDBASE: $OLD_DB" > "$GISRC"
  548. echo "LOCATION_NAME: $OLD_LOC" >> "$GISRC"
  549. echo "MAPSET: $OLD_MAP" >> "$GISRC"
  550. cleanup_tmp
  551. exit
  552. fi
  553. fi
  554. if [ "$LOCATION_NAME" = "##ERROR##" ] ; then
  555. echo "The selected location is not a valid GRASS location"
  556. cleanup_tmp
  557. exit 1
  558. fi
  559. ;;
  560. 2)
  561. # User wants to exit from GRASS
  562. echo "Received EXIT message from GUI."
  563. echo "GRASS is not started. Bye."
  564. cleanup_tmp
  565. exit 0
  566. ;;
  567. *)
  568. echo "ERROR: Invalid return code from gis_set.tcl."
  569. echo "Please advise GRASS developers of this error."
  570. ;;
  571. esac
  572. ;;
  573. *)
  574. # Shouldn't need this but you never know
  575. echo "ERROR: Invalid user interface specified - <$GRASS_GUI>."
  576. echo "Use the --help option to see valid interface names."
  577. cleanup_tmp
  578. exit 1
  579. ;;
  580. esac
  581. fi
  582. GISDBASE=`g.gisenv GISDBASE`
  583. LOCATION_NAME=`g.gisenv LOCATION_NAME`
  584. MAPSET=`g.gisenv MAPSET`
  585. if [ -z "$GISDBASE" ] || [ -z "$LOCATION_NAME" ] || [ -z "$MAPSET" ] ; then
  586. echo "ERROR: Reading data path information from g.gisenv."
  587. echo "GISDBASE=[$GISDBASE]"
  588. echo "LOCATION_NAME=[$LOCATION_NAME]"
  589. echo "MAPSET=[$MAPSET]"
  590. echo
  591. echo "Check the <$GISRCRC> file."
  592. cleanup_tmp
  593. exit 1
  594. fi
  595. LOCATION="${GISDBASE?}/${LOCATION_NAME?}/${MAPSET?}"
  596. # Check for concurrent use
  597. lockfile="$LOCATION/.gislock"
  598. "$ETC/lock" "$lockfile" $$
  599. case $? in
  600. 0) ;;
  601. 1)
  602. echo "$USER is currently running GRASS in selected mapset (file $lockfile found). Concurrent use not allowed."
  603. cleanup_tmp
  604. exit 1 ;;
  605. *)
  606. echo Unable to properly access "$lockfile"
  607. echo Please notify system personel.
  608. cleanup_tmp
  609. exit 1 ;;
  610. esac
  611. # build user fontcap if specified but not present
  612. if [ "$GRASS_FONT_CAP" ] && [ ! -f "$GRASS_FONT_CAP" ] ; then
  613. echo "Building user fontcap ..."
  614. g.mkfontcap
  615. fi
  616. # predefine default driver if DB connection not defined
  617. # is this really needed?? Modules should call this when/if required.
  618. if [ ! -e "$LOCATION/VAR" ] ; then
  619. db.connect -c --quiet
  620. fi
  621. trap "" 2 3 15
  622. # cygwin has many problems with the shell setup
  623. # below, so i hardcoded everything here.
  624. if [ "$CYGWIN" ] ; then
  625. sh="cygwin"
  626. shellname="GNU Bash (Cygwin)"
  627. export SHELL=/usr/bin/bash.exe
  628. export OSTYPE=cygwin
  629. else
  630. sh=`basename "$SHELL"`
  631. case "$sh" in
  632. ksh) shellname="Korn Shell";;
  633. csh) shellname="C Shell" ;;
  634. tcsh) shellname="TC Shell" ;;
  635. bash) shellname="Bash Shell" ;;
  636. sh) shellname="Bourne Shell";;
  637. *) shellname=shell;;
  638. esac
  639. fi
  640. # check for SHELL
  641. if [ ! -x "$SHELL" ] ; then
  642. echo "ERROR: The SHELL variable is not set" 1>&2
  643. rm -f "$lockfile"
  644. cleanup_tmp
  645. exit 1
  646. fi
  647. # hack to process batch jobs:
  648. if [ -n "$GRASS_BATCH_JOB" ] ; then
  649. # defined, but ...
  650. if [ ! -f "$GRASS_BATCH_JOB" ] ; then
  651. # wrong file
  652. echo "Job file '$GRASS_BATCH_JOB' has been defined in"
  653. echo "the 'GRASS_BATCH_JOB' variable but not found. Exiting."
  654. echo
  655. echo "Use 'unset GRASS_BATCH_JOB' to disable batch job processing."
  656. cleanup_tmp
  657. exit 1
  658. else
  659. # right file, but ...
  660. if [ ! -x "$GRASS_BATCH_JOB" ] ; then
  661. echo "Please change file permission to 'executable' for '$GRASS_BATCH_JOB'"
  662. cleanup_tmp
  663. exit 1
  664. else
  665. echo "Executing '$GRASS_BATCH_JOB' ..."
  666. GRASS_GUI="text"
  667. SHELL="$GRASS_BATCH_JOB"
  668. fi
  669. fi
  670. fi
  671. # Start the chosen GUI but ignore text
  672. if [ "$GRASS_DEBUG" -ne 0 ] ; then
  673. echo "GRASS GUI should be $GRASS_GUI"
  674. fi
  675. case "$GRASS_GUI" in
  676. # Check for tcltk interface
  677. tcltk | gis.m)
  678. if [ "$osxaqua" ] ; then
  679. "$GISBASE/scripts/gis.m" | sh &
  680. else
  681. "$GISBASE/scripts/gis.m"
  682. fi
  683. ;;
  684. oldtcltk | d.m)
  685. if [ "$osxaqua" ] ; then
  686. "$GISBASE/scripts/d.m" | sh &
  687. else
  688. "$GISBASE/scripts/d.m"
  689. fi
  690. ;;
  691. wxpython)
  692. "$GISBASE/etc/wxpython/scripts/wxgui"
  693. ;;
  694. # Ignore others
  695. *)
  696. ;;
  697. esac
  698. # Display the version and license info
  699. if [ "$MINGW" ] ; then
  700. :
  701. # TODO: uncomment when PDCurses works.
  702. # cls
  703. else
  704. if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
  705. tput clear
  706. fi
  707. fi
  708. say_hello()
  709. {
  710. if [ -f "$GISBASE/locale/$LCL/etc/welcome" ] ; then
  711. cat "$GISBASE/locale/$LCL/etc/welcome"
  712. else
  713. cat "$ETC/welcome"
  714. fi
  715. }
  716. if [ -n "$GRASS_BATCH_JOB" ] ; then
  717. say_hello
  718. else
  719. cat <<EOF
  720. __________ ___ __________ _______________
  721. / ____/ __ \/ | / ___/ ___/ / ____/ _/ ___/
  722. / / __/ /_/ / /| | \__ \\\\_ \\ / / __ / / \\__ \\
  723. / /_/ / _, _/ ___ |___/ /__/ / / /_/ // / ___/ /
  724. \____/_/ |_/_/ |_/____/____/ \____/___//____/
  725. EOF
  726. say_hello
  727. echo "GRASS homepage: http://grass.osgeo.org/"
  728. echo "This version running thru: $shellname ($SHELL)"
  729. echo "Help is available with the command: g.manual -i"
  730. echo "See the licence terms with: g.version -c"
  731. case "$GRASS_GUI" in
  732. tcltk | gis.m)
  733. echo "If required, restart the GUI with: g.gui tcltk"
  734. ;;
  735. oldtcltk | d.m)
  736. echo "If required, restart the GUI with: g.gui oldtcltk"
  737. ;;
  738. wxpython)
  739. echo "If required, restart the GUI with: g.gui wxpython"
  740. ;;
  741. *)
  742. echo "Start the GUI with: g.gui $DEFAULT_GUI"
  743. ;;
  744. esac
  745. echo "When ready to quit enter: exit"
  746. echo
  747. fi
  748. case "$sh" in
  749. csh|tcsh)
  750. USERHOME="$HOME" # save original home
  751. HOME="$LOCATION"
  752. export HOME
  753. cshrc="$HOME/.cshrc"
  754. tcshrc="$HOME/.tcshrc"
  755. rm -f "$cshrc" "$tcshrc"
  756. echo "set home = $USERHOME" > "$cshrc"
  757. echo "set history = 3000 savehist = 3000 noclobber ignoreeof" >> "$cshrc"
  758. echo "set histfile = $HOME/.history" >> "$cshrc"
  759. echo "set prompt = '\\" >> "$cshrc"
  760. echo "Mapset <${MAPSET}> in Location <${LOCATION_NAME}> \\" >> "$cshrc"
  761. echo "GRASS GRASS_VERSION_NUMBER > '" >> "$cshrc"
  762. echo 'set BOGUS=``;unset BOGUS' >> "$cshrc"
  763. if [ -r "$USERHOME/.grass.cshrc" ]
  764. then
  765. cat "$USERHOME/.grass.cshrc" >> "$cshrc"
  766. fi
  767. if [ -r "$USERHOME/.cshrc" ]
  768. then
  769. grep '^ *set *mail *= *' "$USERHOME/.cshrc" >> "$cshrc"
  770. fi
  771. if [ -r "$USERHOME/.tcshrc" ]
  772. then
  773. grep '^ *set *mail *= *' "$USERHOME/.tcshrc" >> "$cshrc"
  774. fi
  775. if [ -r "$USERHOME/.login" ]
  776. then
  777. grep '^ *set *mail *= *' "$USERHOME/.login" >> "$cshrc"
  778. fi
  779. echo "set path = ( $PATH ) " | sed 's/:/ /g' >> "$cshrc"
  780. cp "$cshrc" "$tcshrc"
  781. "$ETC/run" "$SHELL"
  782. EXIT_VAL=$?
  783. HOME="$USERHOME"
  784. export HOME
  785. ;;
  786. bash|msh|cygwin)
  787. # save command history in mapset dir and remember more
  788. export HISTFILE="$LOCATION/.bash_history"
  789. if [ -z "$HISTSIZE" ] && [ -z "$HISTFILESIZE" ] ; then
  790. export HISTSIZE=3000
  791. fi
  792. # instead of changing $HOME, start bash with: --rcfile "$LOCATION/.bashrc" ?
  793. # if so, must care be taken to explicity call .grass.bashrc et al for
  794. # non-interactive bash batch jobs?
  795. USERHOME="$HOME" # save original home
  796. HOME="$LOCATION" # save .bashrc in $LOCATION
  797. export HOME
  798. bashrc="$HOME/.bashrc"
  799. rm -f "$bashrc"
  800. if [ "$sh" != "cygwin" ] ; then
  801. # this does not work on cygwin for unknown reasons
  802. echo "test -z $PROFILEREAD && . /etc/profile" > "$bashrc"
  803. fi
  804. echo "test -r ~/.alias && . ~/.alias" >> "$bashrc"
  805. echo "PS1='GRASS GRASS_VERSION_NUMBER ($LOCATION_NAME):\w > '" >> "$bashrc"
  806. echo "PROMPT_COMMAND=$GISBASE/etc/prompt.sh" >> "$bashrc"
  807. if [ -r "$USERHOME/.grass.bashrc" ]
  808. then
  809. cat "$USERHOME/.grass.bashrc" >> "$bashrc"
  810. fi
  811. echo "export PATH=\"$PATH\"" >> "$bashrc"
  812. echo "export HOME=\"$USERHOME\"" >> "$bashrc" # restore user home path
  813. "$ETC/run" "$SHELL"
  814. EXIT_VAL=$?
  815. HOME="$USERHOME"
  816. export HOME
  817. ;;
  818. *)
  819. PS1="GRASS $GRASS_VERSION ($LOCATION_NAME):\w > "
  820. export PS1
  821. if [ "$MINGW" ] ; then
  822. # "$ETC/run" doesn't work at all???
  823. "$SHELL"
  824. rm -rf "$LOCATION/.tmp"/* # remove gis.m session files from .tmp
  825. else
  826. "$ETC/run" "$SHELL"
  827. EXIT_VAL=$?
  828. fi
  829. ;;
  830. esac
  831. trap 2 3 15
  832. # GRASS session finished
  833. if [ "$MINGW" ] ; then
  834. :
  835. # TODO: uncomment when PDCurses works.
  836. # cls
  837. else
  838. if [ -z "$GRASS_BATCH_JOB" ] && [ "$GRASS_DEBUG" -eq 0 ] ; then
  839. tput clear
  840. fi
  841. fi
  842. echo "Closing monitors ..."
  843. for MON in `d.mon -L | grep running | grep -v "not running" | sed 's/ .*//'`
  844. do
  845. if [ "$GRASS_DEBUG" -ne 0 ] ; then
  846. echo "d.mon stop=$MON"
  847. fi
  848. d.mon stop="$MON"
  849. done
  850. echo "Cleaning up temporary files ..."
  851. "$ETC/clean_temp" > /dev/null
  852. rm -f "$lockfile"
  853. # Save GISRC
  854. cp "$GISRC" "$GISRCRC"
  855. cleanup_tmp
  856. #### after this point no more grass modules may be called ####
  857. if [ -x "$GRASS_BATCH_JOB" ] ; then
  858. echo "Batch job '$GRASS_BATCH_JOB' (defined in GRASS_BATCH_JOB variable) was executed."
  859. echo "Goodbye from GRASS GIS"
  860. exit $EXIT_VAL
  861. else
  862. echo "Done."
  863. echo
  864. echo
  865. echo
  866. echo "Goodbye from GRASS GIS"
  867. echo
  868. fi