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@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@680: echo "$1" >&2 wilde@711: if [ $DEINSTALL -eq 1 ] ; then wilde@711: echo "Deinstallation failed." >&2 wilde@711: else wilde@711: echo "Installation failed." >&2 wilde@711: fi wilde@680: exit 1 wilde@680: } wilde@680: wilde@698: usage() wilde@698: { wilde@698: cat <&2 "Answer [Y]es or [N]o: " 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}" wilde@707: else wilde@707: inst_default_prefix="$DEFAULT_PREFIX" wilde@723: instcfg_path="${CFGPATH}" wilde@724: instdata_path="${DATAPATH}" 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@705: echo "Reading '$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@707: fatal "System wide installation requires root privileges!" 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@728: echo "Deleting empty directory '$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@752: echo "Deleting $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@723: echo "Uninstalling certificates ..." wilde@728: wilde@723: if [ "$certlist" ] ; then wilde@723: echo "Using certificate list '$certlist'." wilde@725: if [ -x "$cinst" ] ; then wilde@723: "$cinst" "list=$certlist" "choices=uninstall" wilde@723: else wilde@723: echo >&2 "WARNING: can't execute $cinst for certificate deinstallation." wilde@723: fi wilde@723: else wilde@723: echo "No certificate list found. Nothing to do." wilde@723: fi wilde@723: } wilde@723: wilde@729: deinstall_etc() wilde@729: { wilde@750: echo "Removing cron job ..." 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@729: echo "Removing certificate lists from: ${instdata_path}:" wilde@752: rm_files "$instdata_path"/list-*.txt wilde@729: rm_empty_dirs "$instdata_path" wilde@751: wilde@751: echo "Removing configuration files:" wilde@752: rm_files "${tbcfg_files[@]}" wilde@729: rm_empty_dirs "$instcfg_path" wilde@729: } wilde@729: wilde@711: deinstall() wilde@711: { wilde@711: if [ "${oldinstcfg[PREFIX]}" ] ; then wilde@711: echo "Really deinstall TrustBridge from '${oldinstcfg[PREFIX]}'?" wilde@711: yorn || exit 0 wilde@723: deinstall_certs wilde@711: local deinstdir="${oldinstcfg[PREFIX]}/bin" wilde@711: echo "Deinstalling from '${oldinstcfg[PREFIX]}'." wilde@711: for file in $BINNAMES ; do wilde@711: local path="${deinstdir}/$file" wilde@711: echo "Deleting '$path' ..." wilde@711: rm "$path" || echo >&2 "WARNING: Could not delete: '$path'!" wilde@711: done wilde@728: rm_empty_dirs "$deinstdir" wilde@729: deinstall_etc wilde@711: echo "Deinstallation finished." wilde@711: else wilde@711: echo "No current installation found! No harm done." wilde@711: fi wilde@711: } wilde@711: 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 <