#!/bin/sh
# Created by amanda-3.5.1 PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

# This is run by the package installer which can't be modified
# to have all the current settings

set -e
set -u

# commands captured from Makefile

_fn_chown_makefile() {
  chown "root:${_amandagroup}" "/usr/lib/amanda/ambind"
  chmod "04750" "/usr/lib/amanda/ambind"
  chown "root:${_amandagroup}" "/usr/lib/amanda/calcsize"
  chmod "04750" "/usr/lib/amanda/calcsize"
  chown "root:${_amandagroup}" "/usr/lib/amanda/killpgrp"
  chmod "04750" "/usr/lib/amanda/killpgrp"
  chown "root:${_amandagroup}" "/usr/lib/amanda/rundump"
  chmod "04750" "/usr/lib/amanda/rundump"
  chown "root:${_amandagroup}" "/usr/lib/amanda/runtar"
  chmod "04750" "/usr/lib/amanda/runtar"
  chown "root:${_amandagroup}" "/usr/lib/amanda/application/ambsdtar"
  chmod "04750" "/usr/lib/amanda/application/ambsdtar"
  chown "root:${_amandagroup}" "/usr/lib/amanda/application/amgtar"
  chmod "04750" "/usr/lib/amanda/application/amgtar"
  chown "root:${_amandagroup}" "/usr/lib/amanda/application/amstar"
  chmod "04750" "/usr/lib/amanda/application/amstar"
}

# Commands from PKGBUILD
# must run before makefile above

_fn_chown_PKGBUILD() {
  chmod 770 "${_amlog}"
  chown "root:${_amandagroup}" "${_amlog}"
  chmod 700 "${_amhome}"
  chmod 700 "${_amhome}/.ssh/"
  find "${_amhome}/.ssh" -type "f" -exec chmod 600 "{}" "+"
  find "${_ametc}" -mindepth 1 -maxdepth 1 -type "d" -exec chown -R "${_amandauser}:${_amandagroup}" "{}" ";" -exec chmod 750 "{}" ";"
  chown "root:${_amandagroup}" "${_ametc}"
  chown "root:root" "${_amsecurity}"
  chmod 644 "${_amsecurity}"
  chmod 775 "${_ametc}"
  chmod 600 "${_amhome}/.amandahosts"
  chown -R "${_amandauser}:${_amandagroup}" "${_amhome}"
  chown "root:${_amandagroup}" "/usr/bin"/{amoldrecover,amrecover,amcheckdb,amanda-rest-server,amrestore,amfetchdump,amstatus,amreindex,amflush,amdump,amvault,amtape,amreport,amlabel,amrmtape,amcleanupdisk,amcleanup,amcheckdump,amtoc,amserverconfig,amoverview,amaddclient,amcheck,amadmin,amtapetype,amdevcheck,activate-devpay,ambackup,amdump_client,amarchiver,amcrypt-ossl-asym,amcrypt-ossl,amaespipe,amcrypt,amssl,amgetconf,amcryptsimple,amgpgcrypt,amservice}
  chmod 754 "/usr/bin"/{amoldrecover,amrecover,amcheckdb,amanda-rest-server,amrestore,amfetchdump,amstatus,amreindex,amflush,amdump,amvault,amtape,amreport,amlabel,amrmtape,amcleanupdisk,amcleanup,amcheckdump,amtoc,amserverconfig,amoverview,amaddclient,amcheck,amadmin,amtapetype,amdevcheck,activate-devpay,ambackup,amdump_client,amarchiver,amcrypt-ossl-asym,amcrypt-ossl,amaespipe,amcrypt,amssl,amgetconf,amcryptsimple,amgpgcrypt,amservice}
  chown -R "${_amandauser}:${_amandagroup}" "${_amdump}"
}

_fn_post_install() {
  echo "Test the system with"
  echo "  sudo -u '${_amandauser}' amcheck 'MyConfig'"
  echo "  su -c \"su ${_amandauser} -c 'amcheck MyConfig'\""
}

_fn_post_upins() {
  if [ -z "$(getent group "${_amandagroup}")" ]; then
    groupadd -g "${_amandagid}" "${_amandagroup}"
    echo "amanda: Group ${_amandagroup} ${_amandagid} added"
  fi
  if [ -z "$(getent passwd "${_amandauser}")" ]; then
    # Amanda is in the disk group so it can image drives using xfsrestore and other tools
    useradd -u "${_amandauid}" -g "${_amandagroup}" -G "storage,disk" -m -d "${_amhome}" -s '/bin/bash' -c 'Amanda Backup Daemon' "${_amandauser}"
    echo "amanda: User ${_amandauser} ${_amandauid} added"
  fi
  if ! grep -q '^[a-z]\+tar:' "${_amsecurity}"; then
    echo "amanda warning: no archivers are enabled in ${_amsecurity}"
  fi
  rm -f '/usr/bin/amanda-uninstall.sh'
}

_fn_pre_remove() {
  set -u
  local _uid
  _uid="$(id -u "${_amandauser}")"
  if [ $? -eq 0 ]; then
    if [ "${_uid}" -lt 1000 ]; then
      userdel "${_amandauser}" || :
      echo "amanda: User ${_amandauser} deleted"
      # Mysteriously, userdel also removes the group making groupdel fail
      groupdel "${_amandagroup}" 2>/dev/null || :
      echo "amanda: Group ${_amandagroup} deleted"
    else
      echo "uid of amanda is ${_uid}, >=1000, can't delete"
    fi
  fi
  set +u
}

_fn_post_remove() {
  local _cmd='#!/bin/sh
_true=""
if [ "${EUID}" -ne 0 ]; then
  echo "This must be run as root"
  echo "You may want to look through the folders to be deleted for anything valuable"
  _true="true"
else
  echo "This is likely to destroy backup configurations, encryption keys, and ssh keys."
fi
_delself=1
'
  rm -f '/usr/bin/amanda-uninstall.sh'
  local _file
  for _file in '/etc/amanda-security.conf.pacsave' '/var/lib/amanda/.amandahosts.pacsave' '/etc/amanda/MyConfig/amanda.conf.pacsave' '/etc/amanda/MyConfig/disklist.pacsave'; do
    if [ -f "${_file}" ]; then
      echo -n "${_cmd}" >> '/usr/bin/amanda-uninstall.sh'; _cmd=''
      echo "echo rm -f '${_file}'" >> '/usr/bin/amanda-uninstall.sh'
      echo '${_true} '"rm -i '${_file}'" >> '/usr/bin/amanda-uninstall.sh'
      echo "if [ -f '${_file}' ]; then" >> '/usr/bin/amanda-uninstall.sh'
      echo '  _delself=0' >> '/usr/bin/amanda-uninstall.sh'
      echo 'fi' >> '/usr/bin/amanda-uninstall.sh'
    fi
  done

  local _folder
  for _folder in "${_amhome}" "${_ametc}" "${_amdump}" "${_amlog}"; do
    if [ -d "${_folder}" ]; then
      echo -n "${_cmd}" >> '/usr/bin/amanda-uninstall.sh'; _cmd=''
      echo "echo rm -r '${_folder}'" >> '/usr/bin/amanda-uninstall.sh'
      echo '${_true} '"rm -I -r '${_folder}'" >> '/usr/bin/amanda-uninstall.sh'
      echo "if [ -d '${_folder}' ]; then" >> '/usr/bin/amanda-uninstall.sh'
      echo '  _delself=0' >> '/usr/bin/amanda-uninstall.sh'
      echo 'fi' >> '/usr/bin/amanda-uninstall.sh'
    fi
  done

  if [ -s '/usr/bin/amanda-uninstall.sh' ]; then
    chmod 755 '/usr/bin/amanda-uninstall.sh'
    echo 'if [ "${_delself}" -ne 0 ]; then' >> '/usr/bin/amanda-uninstall.sh'
    echo '  ${_true} '"echo 'Amanda folders removed'" >> '/usr/bin/amanda-uninstall.sh'
    echo '  ${_true} '"echo 'Amanda uninstaller removed'" >> '/usr/bin/amanda-uninstall.sh'
    echo '  ${_true} rm -f "$0"' >> '/usr/bin/amanda-uninstall.sh'
    echo 'fi' >> '/usr/bin/amanda-uninstall.sh'
    echo "amanda: use '/usr/bin/amanda-uninstall.sh' to remove all Amanda config files and folders"
  fi
}

# Vars from PKGBUILD
# The .install sets a flag to bypass the functions and source the desired variables for upgrades

if [ ! -z "${_GetOVars:-}" ]; then
  # These variables are from before the upgrade
  ${_GetOLocal:=declare} _Oamandauser='amandabackup'
  ${_GetOLocal} _Oamandagroup='amandabackup'
  ${_GetOLocal} _Oamandauid='112'
  ${_GetOLocal} _Oamandagid='112'
  ${_GetOLocal} _Oamhome='/var/lib/amanda'
  ${_GetOLocal} _Oametc='/etc/amanda'
  ${_GetOLocal} _Oamdump='/dumps'
  ${_GetOLocal} _Oamsecurity='/etc/amanda-security.conf'
  ${_GetOLocal} _Oamlog='/var/log/amanda'
else
  # These variables are from after the upgrade
  ${_GetOLocal:=declare} _amandauser='amandabackup'
  ${_GetOLocal} _amandagroup='amandabackup'
  ${_GetOLocal} _amandauid='112'
  ${_GetOLocal} _amandagid='112'
  ${_GetOLocal} _amhome='/var/lib/amanda'
  ${_GetOLocal} _ametc='/etc/amanda'
  ${_GetOLocal} _amdump='/dumps'
  ${_GetOLocal} _amsecurity='/etc/amanda-security.conf'
  ${_GetOLocal} _amlog='/var/log/amanda'

  if [ -z "${_GetVars:-}" ]; then
    if [ "$#" -eq 0 ]; then
      echo 'The package installer runs this, not you!'
      echo 'Nothing to see here, Move along!'
    else
      while [ "$#" -gt 0 ]; do
        _fn_$1
        shift
      done
    fi
  fi
fi
