#!/bin/bash
appname=`basename "$0"`
ver="0.3.5a.2003041017"
copy="2002, 2003"
mail="PointedEars@gmx.de"
mail_feedback="bug-mozilla@PointedEars.de"

# ********************************** MAIN **************************************

# 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.

# ${extd}${appname}${norm}
#   [${extd}-?${norm}] [${extd}-v${norm}] \
# [${extd}-L${norm} LOCALE] [${extd}-I${norm} INSTALLER] [${extd}-U${norm} \
# [USER]] [${extd}-R${norm}] [${extd}-D${norm} RESOURCE]
#   [( ${extd}-c${norm} | ${extd}-s${norm} | ${extd}-h${norm} ) [SOURCE] \
# [PLUGIN_DIR] [...] [${extd}-0o${norm}]]
#   [${extd}-f${norm} [PLUGIN_DIR] [INST_PATH] [TGZ_PATH]]
#   [ ( ${extd}-i${norm} | ${extd}-u${norm} ) [APP_NAME] [...]] [${extd}-d${norm} MOZ_DIR]
#   SOURCE [TARGET]

echo "$*"

if `getopt -T >/dev/null 2>&1` ; [ $? = 4 ] ; then
  echo "Enhanced getopt(1)"
  tmp=`getopt -o hvL:I:U::RD::C::c::S::s::H::l::0oOf::F::a:i:u:d:T:: \
              --long help,verbose,locale,installer,user,repair,download::,\
copy::,symlink::,hardlink::,null,overwrite,flash::,apps:,install:,uninstall:\
,directory:,truetype:: \
              -n "$appname" -s bash \
              -- "$@"`
  echo "$tmp"
  eval set -- "$tmp"
else
  echo "Old getopt(1)"
  tmp=`getopt hvL:I:U:RD:c:s:l:0iof:a:i:u:d: "$@"`
fi

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 "$*"
  #exit 0
else
  echo "getopt exited: $getopt_exit_code
  " >&2
  exit $getopt_exit_code
fi

# Flags and setup data
verbose=0
repair=0
download=""
single=0
src=""
target=""
installer="mozilla-installer"
import=0
importNull=0
copyArgs=()
linkArgs=()
hardArgs=()
overwrite=0
flash=0
inst=0
instList=""
uninst=0
uninstList=""
truetype=0
truetype_dirs=()

while true; do
  case "$1" in
         -h | --help)        help=1; shift;;
         -v | --verbose)     verbose=1; echo "
${extd}Working in verbose mode.${norm}
\$* = \"$*\"
                               "; shift;;
         -L | --locale)      lg=$2; shift 2;;
         -I | --installer)   installer=$2; shift 2;;
         -U | --single-user) single=1; shift;;
         -R | --repair)      repair=1; shift;;
         -D | --download)    download=${2:-http://ftp.mozilla.org/pub/mozilla/nightly/latest/mozilla-i686-pc-linux-gnu-sea.tar.gz}; shift 2;;
    -C | -c | --copy)
      import=1
      # -c has an optional argument. As we are in quoted mode,
      # an empty parameter will be generated if its optional
      # argument is not found.
      copyArgs[${#copyArgs[*]}]="$2"
      shift 2;;
    -S | -s | --symlink)
      import=1
      linkArgs[${#linkArgs[*]}]=""
      shift 2;;
    -H | -l | --hardlink)
      import=1
      hardArgs[${#hardArgs[*]}]="$2"
      shift 2;;
    -0      | --null)        importNull=1; shift;;
    -O | -o | --overwrite)   overwrite=1; shift;;
    -F | -f | --flash)
      flash=1
      for i in $2; do
        if [ -z "$flashPluginDir" ]; then
          flashPluginDir="$i"
        else
          if [ -z "$flashInstPath" ]; then
            flashInstPath="$i"
          else
            test -z "$flashTGZpath" && flashTGZpath="$i"
          fi
        fi
      done
      shift 2;;
    -A | -a | --apps | -i | --install)
      inst=1
      test -n "$instList" && instList="$instList "
      instList="$instList$2"
      shift 2;;
    -u | --uninstall)
      uninst=1
      test -n "$uninstList" && uninstList="$uninstList "
      uninstList="$uninstList$2"
      shift 2;;
    -d | --directory)
      moz_dir=$2
      shift 2;;
    -T | --truetype)
      truetype=1
      for i in $2; do
        truetype_dirs[${#truetype_dirs[*]}]="$i"
      done
      shift 2;;
         --)
           shift; break;;
    *) echo "Internal error!" ; exit 1 ;;
  esac
done

# Remaining arguments
test -z "$src" && src="$1"
test -z "$target" && target="$1"

test -z "$src" && src="-"
test -z "$target" && target="-"
if [ $verbose -eq 1 ]; then
  echo "src=\"$src\"
target=\"$target\"
installer=\"$installer\""
fi

test -z "$flashPluginDir" -o "$flashPluginDir" = "-" &&
flashPluginDir="/usr/local/mozilla/plugins"
test -z "$flashInstPath" -o "$flashInstPath" = "-" &&
flashInstPath="./flash-linux-installer"
if [ $verbose -eq 1 ]; then
  echo "import=$import
importNull=$importNull
copyArgs='${copyArgs[@]}'
linkArgs='${linkArgs[@]}'
hardOpt='${hardArgs[@]}'
overwrite=$overwrite
flash=$flash
flashPluginDir=\"$flashPluginDir\"
flashInstPath=\"$flashInstPath\"
flashTGZpath=\"$flashTGZpath\"
inst=$inst
instList=\"$instList\"
uninst=$uninst
uninstList=\"$uninstList\"
  "
fi

