#-*-sh-*-
ME=`basename "$0"`
DEFAULT_PREFIX="$HOME/TrustBridge"
SYSDEFAULT_PREFIX="/usr/local"
CFGPATH="${XDG_CONFIG_HOME:-$HOME/.config}/BSI"
DATAPATH="${XDG_DATA_HOME:-$HOME/.local/share}/BSI/TrustBridge"
SYSCFGPATH="/etc/TrustBridge"
# FIXME: a user independent data path would be nice for system wide
#        installations, but we wont open this can of worms for now.
#        So we assume the system wide installation is always used and
#        maintained by the same user.
SYSDATAPATH="$DATAPATH"
INSTCFGNAME="TrustBridge-inst.cfg"
FORCE=0
SYSINST=0
DEINSTALL=0
UPDATE=0
BINNAMES="###BINNAMES###"
ICONNAME="###ICONNAME###"
HELPNAMES="###HELPNAMES###"
HELPNAMES_SOURCES="###HELPNAMES_SOURCES###"
HELPNAMES_STATIC="###HELPNAMES_STATIC###"
HELPNAMES_IMG="###HELPNAMES_IMG###"
ARCH="###ARCH###"

declare -A instcfg oldinstcfg
declare inst_default_prefix instdata_path instcfg_path instcfg_file
instcfg=(
  [TIMESTMP]=`date -u +%Y%m%d%H%M%S`
  [VERSION]='@PROJECT_VERSION@'
  [PREFIX]=''
)
oldinstcfg=(
  [TIMESTMP]=''
  [VERSION]=''
  [PREFIX]=''
)

declare -A L10N_DE
###INCLUDE:linux-installer-common.l10n-de

getxt()
{
  # Poor mans gettext for l10n completely self contained in one shell
  # script.
  MSGID="$1"
  shift
  case ${LANGUAGE:-${LC_ALL:-${LC_MESSAGES:-$LANG}}} in
    de*)
      if [ "${L10N_DE[$MSGID]}" ] ; then
          MSG="${L10N_DE[$MSGID]}"
      else
        MSG="$MSGID"
      fi
      ;;
    *)
      MSG="$MSGID"
      ;;
  esac

  printf "$MSG" "$@"
}

version()
{
  cat <<EOF
TrustBridge ${instcfg[VERSION]} Installer

Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
Software engineering by Intevation GmbH

This file is Free Software under the GNU GPL (v>=2)
and comes with ABSOLUTELY NO WARRANTY!
See LICENSE.txt for details.
EOF
  exit 0
}

fatal()
{
  getxt "$1" >&2
  if [ $DEINSTALL -eq 1 ] ; then
      getxt "Deinstallation failed.\n" >&2
  else
    getxt "Installation failed.\n" >&2
  fi
  exit 1
}

yorn()
{
  local c
  while true ; do
    read -n 1 c
    echo
    case "$c" in
      y|Y|j|J)
        return 0
        ;;
      n|N)
        return 1
        ;;
      *)
        getxt >&2 "Answer [Y]es or [N]o:\n"
    esac
  done
}

check_priv()
{
  if [ $SYSINST -eq 1 -a "$UID" -ne 0 ] ; then
      fatal "System wide installation or deinstallation requires root privileges!\n"
  fi
}

init_vars()
{
  if [ -n "${SUDO_USER-}" ] ; then
    # Default to system wide installation when running with sudo
    SYSINST=1
  fi

  if [ $SYSINST -eq 1 ] ; then
      inst_default_prefix="$SYSDEFAULT_PREFIX"
      instcfg_path="${SYSCFGPATH}"
      instdata_path="${SYSDATAPATH}"
      autostart_path="$(getent passwd "${SUDO_USER}" | cut -d ':' -f 6)/.config/autostart"
      startmenu_path="/usr/share/applications"
  else
    inst_default_prefix="$DEFAULT_PREFIX"
    instcfg_path="${CFGPATH}"
    instdata_path="${DATAPATH}"
    autostart_path=${XDG_CONFIG_HOME:-~/.config/autostart}
    startmenu_path=${XDG_DATA_HOME:-~/.local/share/applications}
    if [ $DEINSTALL -eq 1 ] ; then
      if [ ! -r ${instcfg_path}/${INSTCFGNAME} ]; then
        if [ -r ${SYSCFGPATH}/${INSTCFGNAME} ]; then
           # Fall back to system uninstallation if no user config found
           SYSINST=1
           init_vars
           check_priv
        fi
      fi
    fi
  fi
  if [ $DEINSTALL -eq 0 ]; then
    # extra bin path not needed on deinstall:
    extra_bin_path=`mktemp --tmpdir -d tmpbin.XXXXXXXXXX`
  fi
  instcfg_file="${instcfg_path}/${INSTCFGNAME}"
  HOST_ARCH=$(uname -m)
}

read_oldinstcfg()
{
  if [ -r "$instcfg_file" ] ; then
      getxt "Reading '%s' ...\n" "$instcfg_file"
      for key in "${!oldinstcfg[@]}" ; do
        oldinstcfg[$key]=`sed -n "/$key/s/[^=]*=\(.*\)/\1/p" "$instcfg_file"`
      done
  fi
}


setup_cronjob()
{
  local tmpcrontab=`mktemp`

  if [ $SYSINST -eq 1 -a "${SUDO_USER+X}" ] ; then
      local crontabopt="-u $SUDO_USER"
  else
      local crontabopt=''
  fi

  if [ "$1" != "deinstall" ] ; then
      local trustbridge_tray_starter="${instcfg[PREFIX]}/bin/trustbridge-tray-starter.sh"
  else
    local trustbridge_tray_starter="${oldinstcfg[PREFIX]}/bin/trustbridge-tray-starter.sh"
  fi

  crontab $crontabopt -l | \
      grep -vF "$trustbridge_tray_starter" \
           >"$tmpcrontab"
  if [ "$1" != "deinstall" ] ; then
      echo "$(( $RANDOM / 555 )) 12  * * * \"$trustbridge_tray_starter\"" \
           >>"$tmpcrontab"
  fi
  crontab $crontabopt "$tmpcrontab"
  rm "$tmpcrontab"
}
