--- 2003-03-14-14/moz	Fri Mar 14 14:53:24 2003
+++ latest/moz	Fri Mar 28 23:14:19 2003
@@ -1,5 +1,5 @@
	#!/bin/sh
-	ver="0.2.2b+.2003031414"
+	ver="0.2.2b+.2003032823"
	copy="2002, 2003"
	mail="PointedEars@gmx.de"
	mail_feedback="bug-moz@PointedEars.de"
@@ -58,6 +58,8 @@ mail_feedback="bug-moz@PointedEars.de"
	# * _XPG=1 for `$( `$command` )' and `$(( `$expression` ))' in `sh' (@IRIX 6.5)
	# * `--version': Display version information and exit
	# * Speed-up Mozilla process start-up by using ping() only for first -remote
+	# * Allow to use local directories and filenames as URI argument; the argument
+	#   is then preceded by file://`pwd`/
	# * Cosmetics:
	#   - Added status messages
	#   - Bold-formatted title with changed tagline
@@ -72,15 +74,51 @@ mail_feedback="bug-moz@PointedEars.de"
	#
	# See the ChangeLog text file for further information.
	#
-	# TO DO:
+	# TO DO (0.2.2 release, scheduled in first week of April 2003)
	#
-	# * POSIX conform parsing of options and arguments
-	# * `-M': Support other programs similar to Mozilla like Phoenix 
+	# * POSIX conform parsing of options and arguments (incomplete):
+	#   - `-?' may become obsolete as `getopt' doesn't support it
+	#     (seems uncommon anyway, so it shouldn't be a great loss);
+	#     `make-nightly' scripts must be altered to use `--help' instead
+	# * `-M', `--mozilla': Support other programs similar to Mozilla like Phoenix
+	# * `-l -f': Force full output format; ignore if not supported (incomplete)
+	# * `-C', `--command' options to execute arbitrary Mozilla commands
	# * Cosmetics:
	#   - More modular code (incomplete)
	# 
+	# TO DO (later):
+	# 
+	# * New option to call Mozilla if and only if it is not running
+	# 
	# ----------------------------------------------------------------------------
	
+	# Note that we use `"$@"' to let each command-line parameter expand to a
+	# separate word. The quotes around `$@' are essential!
+	# We need `tmp' as the `eval set --' would nuke the return value of getopt.
+	
+	tmp=`getopt -o vL:M:klfn:w:t:-m::ic \
+	            --long verbose,locale:,mozilla:,help,version,term,kill,list,\
+	full,new:,win:,tab:,mailto::,inbox,compose \
+	            -n $(basename "$0") -- "$@"`
+	
+	getopt_exit_code=$?
+	if [ $getopt_exit_code -eq 0 ]; then
+	##     getopt  returns  error  code 0 for successful parsing, 1 if
+	##     getopt(3) returns errors, 2 if it does not understand  its
+	##     own parameters, 3 if an internal error occurs like out-of-
+	##     memory, and 4 if it is called with -T.
+	#
+	# Note the quotes around `$tmp': they are essential!
+	  echo $tmp
+	  # remove "--"
+	  for i in $tmp; do if [ "$i" != "--" ]; then tmp2="${tmp2} $i"; fi; done
+	  eval set -- "$tmp2"
+	else
+	  echo "getopt exited: $getopt_exit_code
+	  " >&2
+	  exit $getopt_exit_code
+	fi
+	
	# check for verbose mode
	verbose=0
	help=0
@@ -127,7 +165,7 @@ else
	        done=""
	        attn=""
	        norm=""
-	        stat=""
+	        stat="... "
	
	     rc_done="done"
	  rc_running="running"
@@ -213,10 +251,12 @@ ${extd}moz $ver -- "`L "Mozilla process 
	Mozilla-Prozesse"`"${norm}
	Copyright (C) $copy  Thomas Lahn <$mail>
	"`L "Distributed under the terms of the" \
-	    "Veröffentlicht unter den Bedingungen der"`" GNU General Public License (GPL),
-	"`L "see COPYING file or http://www.fsf.org/copyleft/gpl.html for details." \
+	    "Veröffentlicht unter den Bedingungen der"`" GNU General Public License \
+	(GPL)," | $fold
+	  L "see COPYING file or http://www.fsf.org/copyleft/gpl.html for \
+	details." \
	    "Details in Datei COPYING oder unter http://www.fsf.org/copyleft/gpl.html" \
-	  | $fold`
+	  | $fold
	}
	
	function help {
@@ -225,8 +265,8 @@ function help {
	  [${extd}-v${norm}] [${extd}-L${norm} LOCALE] [${extd}-M${norm} COMMAND]
	  [ ( ${extd}-?${norm} | ${extd}--version${norm} | ${extd}--term${norm} | \
	${extd}--kill${norm} | ${extd}-l${norm} [${extd}-f${norm}] ) ]
-	  [ ( [ ( ${extd}-w${norm} | ${extd}-t${norm} ) ] [URI] [...] | ${extd}-${norm} \
-	) [...] ]
+	  [ ( [ ( ${extd}-w${norm} | ${extd}-t${norm} ) ] [URI] [...] | ${extd}-${norm}\
+	 ) [...] ]
	  [ ( ${extd}-m${norm} [ADDRESS[,...]] | ${extd}-i${norm} | ${extd}-c${norm} ) \
	[...] ]
	  "
@@ -283,7 +323,8 @@ L "\
	                     path is correct." \
	  "\
	                     Standard ist \`mozilla', was es erforderlich macht, dass
-	                     sich die ausführbare Datei \`mozilla' im \`\$PATH' befindet,
+	                     sich die ausführbare Datei \`mozilla' im \`\$PATH' \
+	befindet,
	                     falls Sie nicht mit der Umgebungsvariable \`MOZILLA_CMD'
	                     etwas anderes definiert haben. Sie können hier auch einen
	                     Befehl angeben, der sich im \`\$PATH' befindet, daher wird
@@ -294,7 +335,8 @@ L "\
	L "Display this help and exit." "Anzeige dieser Hilfe und beenden." | $fold
	
	  echo -n "      ${extd}--version${norm}    "
-	L "Display version information and exit." "Anzeige der Version und beenden." | $fold
+	L "Display version information and exit." \
+	  "Anzeige der Version und beenden." | $fold
	
	# version 0.2.3 proposal
	# echo "    --block   Block the shell for the 'mozilla' process, i.e. prevent"
@@ -356,10 +398,12 @@ L "List running Mozilla processes and th
	L "Force full output format. The default is to use the" \
	  "Erzwinge das vollständige Ausgabeformat. Standardmässig" | $fold
	L "\
-	                     user-oriented format if supported. ${extd}For future use.${norm}" \
+	                     user-oriented format if supported. \
+	${extd}For future use.${norm}" \
	  "\
	                     wird das benutzerorientierte Format benutzt, falls es
-	                     unterstützt wird. ${extd}Für zukünftige Verwendung.${norm}" | $fmt
+	                     unterstützt wird. \
+	${extd}Für zukünftige Verwendung.${norm}" | $fmt
	  
	  echo -n "  ${extd}-n${norm}, ${extd}--new${norm}        "
	L "Same as \`${extd}-w${norm}'. For backwards compatibility only," \
@@ -466,10 +510,40 @@ L "Opens a new Message Compose window. S
	  echo
	}
	
+	function isMozRunning {
+	  # check for supported `ps' options 
+	  whoami=`whoami`
+	  # BSD style user-oriented
+	  args="u "
+	  ps $args 1>/dev/null 2>&1
+	  if [ $? -eq 0 ]; then
+	    fallback=$args
+	    args=$args" -u $whoami" # current user's processes only
+	    ps $args 1>/dev/null 2>&1
+	    test $? -ne 0 && args=$fallback
+	  else
+	    # SysV/Unix98 (i.e. IRIX) style
+	    fallback="-"
+	    args="-f" # full output
+	    ps $args 1>/dev/null 2>&1
+	    if [ $? -eq 0 ]; then
+	      fallback=$args
+	    else
+	      args=$fallback
+	    fi
+	    args=$args"u $whoami" # current user's processes only
+	    ps $args 1>/dev/null 2>&1
+	    test $? -ne 0 && args=$fallback
+	  fi
+	  chk=`ps $args | grep mozilla-bin | grep -v grep`
+	  test -z "$chk" && return 1
+	  return 0
+	}
+	  
	function KillMoz {
	  local result=0
	
-	  if [ -n "`ps -Af | grep mozilla-bin | grep -v grep`" ]; then
+	  if isMozRunning; then
	    echo -n `L "Sending all Mozilla processes the" \
	               "Sende allen Mozilla-Prozessen das"`" "
	    case "$1" in
@@ -477,9 +551,11 @@ function KillMoz {
	         *) echo -n "TERM";;
	    esac
	    Lout=`L " signal" "-Signal"`
-	    echo -n "${Lout}... "
+	    echo -n "$Lout$stat"
	    killall $1 mozilla-bin 2> /dev/null
	    result=$?
+	# FIX ME!
+	# allow other user's processes to run and be successful anyway
	    if [ $result -ne 0 ]; then
	      msg_failed
	    else
@@ -500,7 +576,7 @@ $ping more times before giving up. Press
	            "Mozilla wurde $ping Mal erfolglos gepingt, es werden weitere
	$ping Versuche unternommen. Drücken Sie Strg+C, um abzubrechen." | $fmt >&2
	          echo -n "
-	... " >&2
+	$stat" >&2
	        fi
	      done
	      if [ $error -ne 0 ]; then
@@ -521,7 +597,7 @@ Bitte überprüfen Sie ihre Mozilla-Instal
	      fi
	    fi
	  else
-	    echo `L "No Mozilla process is running that could be terminated or killed." \
+	    echo `L "No Mozilla process is running that could be terminated or killed."\
	            "Es läuft kein Mozilla-Prozess, der beendet werden kann."` \
	| $fold >&2
	  fi
@@ -682,14 +758,20 @@ _XPG=1
	  chk=`ps -fu $(whoami) 2> /dev/null | grep mozilla-bin | grep -v grep`
	_XPG=0
	  test -z "$uri" && uri="$1"
+	  
+	  # check if argument denotes a local file or directory,
+	  # determine the current working directory and append
+	  # to the URI if successful
+	  test -d "$uri" -o -f "$uri" && uri="file://"`pwd 2>/dev/null`"/$uri"
+	  
	  if [ -z "$chk" ]; then
	    echo -n `L "Starting a new Mozilla process" \
	               "Starte einen neuen Mozilla-Prozess"`
-	    test -n "$uri" && test $mailto -eq 1 && test $inbox -eq 0 && test $compose -eq 0 \
-	      echo -n " "`L "with URI" "mit dem URI"`" '$uri'"
-	    echo -n "... "
+	    test -n "$uri" && test $mailto -eq 1 && test $inbox -eq 0 \
+	      && test $compose -eq 0 && echo -n " "`L "with URI" "mit dem URI"`" '$uri'"
+	    echo -n "$stat"
	    #if [ $block -eq 1 ]; then
-	    #  mozilla "$1"
+	    #  mozilla "$uri"
	    #else
	      mozilla "$uri" &
	    #fi
@@ -712,7 +794,7 @@ $ping more times before giving up. Press
	            "Mozilla wurde $ping Mal erfolglos gepingt, es werden weitere
	$ping Versuche unternommen. Drücken Sie Strg+C, um abzubrechen." | $fmt >&2
	          echo -n "
-	... " >&2
+	$stat" >&2
	        fi
	      done
	      if [ $error -eq 0 ]; then
@@ -772,8 +854,8 @@ $(L "not found. Cannot handle \`mailto:'
	_XPG=0
	    if [ $mailto -eq 0 ] && [ $inbox -eq 0 ] && [ $compose -eq 0 ]; then
	      echo -n `L "Accessing URI" "Lade URI"`" "
-	      if [ -n "$1" ]; then
-	        echo -n "'$1'"
+	      if [ -n "$uri" ]; then
+	        echo -n "'$uri'"
	      else
	        echo -n "'about:blank'"
	      fi
@@ -793,7 +875,7 @@ _XPG=0
	                   "Neues Fenster zur Nachrichtenbearbeitung wird geöffnet"`
	      fi
	    fi
-	    echo -n "... "
+	    echo -n "$stat"
	    mozilla -remote "$cmd" &
	    if [ $? -eq 0 ]; then msg_done; else msg_failed; fi
	  fi
@@ -802,5 +884,6 @@ _XPG=0
	done
	
	echo
-	test $verbose -eq 1 && L "Press any key..." "Beliebige Taste drücken..." && read -n 1
+	test $verbose -eq 1 && L "Press any key..." "Beliebige Taste drücken ..." \
+	  | $fold && read -n 1
	exit 0