wilde@680: #!/bin/bash wilde@707: set -u wilde@680: wilde@698: ME=`basename "$0"` wilde@704: DEFAULT_PREFIX="$HOME/TrustBridge" wilde@707: SYSDEFAULT_PREFIX="/usr/local" wilde@724: CFGPATH="${XDG_CONFIG_HOME:-$HOME/.config}/BSI" wilde@724: DATAPATH="${XDG_DATA_HOME:-$HOME/.local/share}/BSI/TrustBridge" wilde@700: SYSCFGPATH="/etc/TrustBridge" wilde@727: # FIXME: wilde@727: # Set the real data path for system wide installation once its known. wilde@727: SYSDATAPATH="$DATAPATH" wilde@724: INSTCFGNAME="TrustBridge-inst.cfg" wilde@705: FORCE=0 wilde@707: SYSINST=0 wilde@711: DEINSTALL=0 wilde@711: BINNAMES="###BINNAMES###" wilde@698: wilde@705: declare -A instcfg oldinstcfg wilde@724: declare inst_default_prefix instdata_path instcfg_path instcfg_file wilde@700: instcfg=( wilde@700: [TIMESTMP]=`date -u +%Y%m%d%H%M%S` wilde@700: [VERSION]='@PROJECT_VERSION@' wilde@700: [PREFIX]='' wilde@700: ) wilde@705: oldinstcfg=( wilde@705: [TIMESTMP]='' wilde@705: [VERSION]='' wilde@705: [PREFIX]='' wilde@705: ) wilde@698: wilde@795: declare -A L10N_DE wilde@795: ###L10N_DE### wilde@795: wilde@795: getxt() wilde@795: { wilde@795: # Poor mans gettext for l10n completely self contained in one shell wilde@795: # script. wilde@795: MSGID="$1" wilde@795: shift wilde@795: case ${LANGUAGE:-${LC_ALL:-${LC_MESSAGES:-$LANG}}} in wilde@795: de*) wilde@795: if [ "${L10N_DE[$MSGID]}" ] ; then wilde@795: MSG="${L10N_DE[$MSGID]}" wilde@795: else wilde@795: MSG="$MSGID" wilde@795: fi wilde@795: ;; wilde@795: *) wilde@795: MSG="$MSGID" wilde@795: ;; wilde@795: esac wilde@795: wilde@795: printf "$MSG" "$@" wilde@795: } wilde@795: wilde@698: version() wilde@698: { wilde@698: cat <=2). See LICENSE.txt for details. wilde@698: There is NO WARRANTY, to the extent permitted by law. wilde@698: EOF wilde@698: exit 0 wilde@698: } wilde@680: wilde@680: fatal() wilde@680: { wilde@795: getxt "$1" >&2 wilde@711: if [ $DEINSTALL -eq 1 ] ; then wilde@795: getxt "Deinstallation failed.\n" >&2 wilde@711: else wilde@795: getxt "Installation failed.\n" >&2 wilde@711: fi wilde@680: exit 1 wilde@680: } wilde@680: wilde@698: usage() wilde@698: { wilde@799: getxt "Usage: %s [OPTION]...\n" "$ME" wilde@799: getxt "Install TrustBridge.\n\n" wilde@799: getxt "Options:\n" wilde@799: getxt " -p, --prefix=PATH install files in PATH\n" wilde@799: getxt " -f, --force install to given prefix, even when a current\n" wilde@799: getxt " installation with different prefix exists.\n" wilde@799: getxt " -d, --deinstall deinstall files from current installation\n" wilde@799: getxt " -s, --system create a system wide (de)installation\n" wilde@799: getxt " --help display this help and exit\n" wilde@799: getxt " --version output version information and exit\n" wilde@698: exit $1 wilde@698: } wilde@698: wilde@711: yorn() wilde@711: { wilde@711: local c wilde@711: while true ; do wilde@711: read -n 1 c wilde@711: echo wilde@711: case "$c" in wilde@711: y|Y|j|J) wilde@711: return 0 wilde@711: ;; wilde@711: n|N) wilde@711: return 1 wilde@711: ;; wilde@711: *) wilde@795: getxt >&2 "Answer [Y]es or [N]o:\n" wilde@711: esac wilde@711: done wilde@711: } wilde@711: wilde@698: parse_args() wilde@698: { wilde@698: OPTS=`getopt \ wilde@711: -l deinstall,force,help,prefix:,system,version \ wilde@711: -o d,f,p:,s -n "$ME" -- "$@"` wilde@698: [ $? -eq 0 ] || usage 23 wilde@698: wilde@698: eval set -- "$OPTS" wilde@698: wilde@698: while true ; do wilde@698: case "$1" in wilde@698: --prefix|-p) wilde@700: instcfg[PREFIX]="$2" wilde@698: shift 2 wilde@698: ;; wilde@707: --system|-s) wilde@707: SYSINST=1 wilde@707: shift 1 wilde@707: ;; wilde@705: --force|-f) wilde@705: FORCE=1 wilde@705: shift 1 wilde@705: ;; wilde@711: --deinstall|-d) wilde@711: DEINSTALL=1 wilde@711: shift 1 wilde@711: ;; wilde@698: --help) wilde@698: usage 0 wilde@698: ;; wilde@698: --version) wilde@698: version wilde@698: ;; wilde@698: --) wilde@698: shift wilde@698: break wilde@698: ;; wilde@698: esac wilde@698: done wilde@698: } wilde@698: wilde@707: init_vars() wilde@707: { wilde@707: if [ $SYSINST -eq 1 ] ; then wilde@707: inst_default_prefix="$SYSDEFAULT_PREFIX" wilde@723: instcfg_path="${SYSCFGPATH}" wilde@727: instdata_path="${SYSDATAPATH}" andre@785: autostart_path="$(getent passwd "${SUDO_USER}" | cut -d ':' -f 6)/.config/autostart" wilde@707: else wilde@707: inst_default_prefix="$DEFAULT_PREFIX" wilde@723: instcfg_path="${CFGPATH}" wilde@724: instdata_path="${DATAPATH}" andre@785: autostart_path=${XDG_CONFIG_HOME:-~/.config/autostart} wilde@707: fi wilde@723: instcfg_file="${instcfg_path}/${INSTCFGNAME}" wilde@707: } wilde@707: wilde@700: write_instcfg() wilde@700: { wilde@700: install -d `dirname "$instcfg_file"` wilde@700: echo "# Created by TrustBridge-Installer, don't touch!" >"$instcfg_file" wilde@700: for key in "${!instcfg[@]}" ; do wilde@700: echo "${key}=${instcfg[$key]}" >>"$instcfg_file" wilde@700: done wilde@700: } wilde@700: wilde@705: read_oldinstcfg() wilde@705: { wilde@705: if [ -r "$instcfg_file" ] ; then wilde@795: getxt "Reading '%s' ...\n" "$instcfg_file" wilde@705: for key in "${!oldinstcfg[@]}" ; do wilde@705: oldinstcfg[$key]=`sed -n "/$key/s/[^=]*=\(.*\)/\1/p" "$instcfg_file"` wilde@705: done wilde@707: fi wilde@705: } wilde@705: wilde@707: check_priv() wilde@707: { wilde@707: if [ $SYSINST -eq 1 -a "$UID" -ne 0 ] ; then wilde@795: fatal "System wide installation requires root privileges!\n" wilde@707: fi wilde@707: } wilde@707: wilde@728: rm_empty_dirs() wilde@728: { wilde@728: # Args: $1 - DIRECTORY wilde@728: # wilde@728: # Recursively remove DIRECTORY and all it _parent_ directories as wilde@728: # long as they are empty. wilde@728: local directory="$1" wilde@752: while [ -d "$directory" -a -z "$(ls 2>/dev/null -A "$directory")" ] ; do wilde@795: getxt "Deleting empty directory '%s' ...\n" "$directory" wilde@728: rmdir "$directory" wilde@728: directory=`dirname "$directory"` wilde@728: done wilde@728: } wilde@728: wilde@752: rm_files() wilde@752: { wilde@752: for file in "$@" ; do wilde@752: if [ -e "$file" ] ; then wilde@795: getxt "Deleting '%s' ...\n" "$file" wilde@752: rm "$file" wilde@752: fi wilde@752: done wilde@752: } wilde@752: wilde@749: setup_cronjob() wilde@749: { wilde@749: local tmpcrontab=`mktemp` wilde@765: wilde@764: if [ $SYSINST -eq 1 -a "${SUDO_USER+X}" ] ; then wilde@765: local crontabopt="-u $SUDO_USER" wilde@765: else wilde@765: local crontabopt='' wilde@764: fi wilde@765: wilde@766: if [ "$1" != "deinstall" ] ; then wilde@766: local trustbridge_tray_starter="${instcfg[PREFIX]}/bin/trustbridge-tray-starter.sh" wilde@766: else wilde@766: local trustbridge_tray_starter="${oldinstcfg[PREFIX]}/bin/trustbridge-tray-starter.sh" wilde@766: fi wilde@766: wilde@764: crontab $crontabopt -l | \ wilde@766: grep -vF "$trustbridge_tray_starter" \ wilde@764: >"$tmpcrontab" wilde@750: if [ "$1" != "deinstall" ] ; then wilde@766: echo "$(( $RANDOM / 555 )) 12 * * * \"$trustbridge_tray_starter\"" \ wilde@750: >>"$tmpcrontab" wilde@750: fi wilde@764: crontab $crontabopt "$tmpcrontab" wilde@749: rm "$tmpcrontab" wilde@749: } wilde@749: wilde@750: remove_cronjob() wilde@750: { wilde@750: setup_cronjob deinstall wilde@750: } wilde@750: wilde@723: deinstall_certs() wilde@723: { wilde@723: local cinst="${oldinstcfg[PREFIX]}/bin/cinst" wilde@752: local certlist=`ls 2>/dev/null -1 ${instdata_path}/list-*.txt | sort -nr | head -n 1` wilde@723: wilde@795: getxt "Uninstalling certificates ...\n" wilde@728: wilde@723: if [ "$certlist" ] ; then wilde@795: getxt "Using certificate list '%s'.\n" "$certlist" wilde@725: if [ -x "$cinst" ] ; then wilde@723: "$cinst" "list=$certlist" "choices=uninstall" wilde@723: else wilde@795: getxt >&2 "WARNING: can't execute %s for certificate deinstallation.\n" "$cinst" wilde@723: fi wilde@723: else wilde@795: getxt "No certificate list found. Nothing to do.\n" wilde@723: fi wilde@723: } wilde@723: wilde@729: deinstall_etc() wilde@729: { wilde@795: getxt "Removing cron job ...\n" wilde@750: remove_cronjob wilde@750: wilde@729: # FIXME: delete all files created by the application. wilde@751: local tbcfg_files=( "${instcfg_path}/TrustBridge.ini" wilde@751: "${instcfg_path}/trustbridge-tray-starter.cfg" wilde@751: "$instcfg_file" ) wilde@752: wilde@795: getxt "Removing certificate lists from: %s:\n" "$instdata_path" wilde@752: rm_files "$instdata_path"/list-*.txt wilde@795: wilde@795: getxt "Removing PID file from: %s:\n" "$instdata_path" wilde@795: rm_files "$instdata_path"/*.pid wilde@729: rm_empty_dirs "$instdata_path" wilde@751: wilde@795: getxt "Removing configuration files:\n" wilde@752: rm_files "${tbcfg_files[@]}" wilde@729: rm_empty_dirs "$instcfg_path" andre@777: wilde@795: getxt "Removing TrustBridge from autostart\n" andre@785: rm_files "${autostart_path}/tustbridge.desktop" wilde@729: } wilde@729: wilde@711: deinstall() wilde@711: { wilde@711: if [ "${oldinstcfg[PREFIX]}" ] ; then wilde@795: getxt "Really deinstall TrustBridge from '%s'? [y/n]\n" "${oldinstcfg[PREFIX]}" wilde@711: yorn || exit 0 wilde@723: deinstall_certs wilde@711: local deinstdir="${oldinstcfg[PREFIX]}/bin" wilde@795: getxt "Deinstalling from '%s'.\n" "${oldinstcfg[PREFIX]}" wilde@711: for file in $BINNAMES ; do wilde@711: local path="${deinstdir}/$file" wilde@795: getxt "Deleting '%s' ...\n" "$path" wilde@795: rm "$path" || getxt >&2 "WARNING: Could not delete: '%s'!\n" "$path" wilde@711: done wilde@728: rm_empty_dirs "$deinstdir" wilde@729: deinstall_etc wilde@795: getxt "Deinstallation finished.\n" wilde@711: else wilde@795: getxt "No current installation found! No harm done.\n" wilde@711: fi wilde@711: } wilde@711: andre@777: write_autostart() andre@777: { andre@777: cat > "$1" << EOF andre@777: [Desktop Entry] andre@777: Type=Application andre@777: Name=TrustBridge andre@777: Exec="${instcfg[PREFIX]}/bin/trustbridge" --tray andre@777: EOF andre@777: chown "${SUDO_USER:-${USER}}" "$1" andre@777: chmod 700 "$1" andre@777: } andre@777: andre@777: setup_autostart() andre@777: { andre@777: # Supported desktop environments: Unity, GNOME, XFCE, LXDE, KDE andre@785: # System wide installation with a nonstandard XDG_CONFIG_HOME or KDEHOME is not andre@785: # respected with regards to autostart. andre@785: if [ ! -d "${autostart_path}" ]; then andre@785: install -d "${autostart_path}" || \ wilde@795: fatal "Failed to create autostart directory: '%s'\n" "$autostart_path" andre@785: fi andre@777: andre@785: write_autostart "${autostart_path}/tustbridge.desktop" andre@777: } wilde@707: wilde@698: #====================================================================== wilde@698: # main() wilde@698: wilde@698: parse_args "$@" wilde@707: init_vars wilde@707: check_priv wilde@705: read_oldinstcfg wilde@698: wilde@680: cat <