# Can't customize .install scripts so we use an external script

# The PKGBUILD will stop if these vars don't match what's in the PKGBUILD

_amlibexec='/usr/lib'
_amsecurity='/etc/amanda-security.conf'
_amhome='/var/lib/amanda'

pre_install() {
  # During pre_install the settings file amanda-install.sh isn't available yet
  # During pre_upgrade the settings file exists with only the old settings.
  # During the pre_upgrade from the old package, there are no old settings.
  # Here in install we always have the new vars and can guess the old vars.

  # Move file before pacman can remove it
  local _amsother=''
  case "${_amsecurity}" in
  '/etc/amanda-security.conf') _amsother='/etc/amanda/amanda-security.conf';;
  '/etc/amanda/amanda-security.conf') _amsother='/etc/amanda-security.conf';;
  esac
  if [ ! -z "${_amsother}" ] && [ -s "${_amsother}" ] && [ ! -s "${_amsecurity}" ]; then
    echo "amanda mv ${_amsother} ${_amsecurity}"
    mv "${_amsother}" "${_amsecurity}"
  fi

  # Move home folder before pacman can remove it
  _amsother=''
  case "${_amhome}" in
  '/var/lib/amanda') _amsother='/var/amanda';;
  '/var/amanda') _amsother='/var/lib/amanda';;
  esac
  if [ ! -z "${_amsother}" ] && [ -d "${_amsother}" ] && [ ! -d "${_amhome}" ]; then
    echo "amanda mv ${_amsother} ${_amhome}"
    mv "${_amsother}" "${_amhome}"
  fi
}

_post_upins() {
  _GetOVars= _GetOLocal= bash -e -u "${_amlibexec}/amanda/amanda-install.sh" 'post_upins' 'chown_PKGBUILD' 'chown_makefile'
  if [ -s '/etc/xinetd.d/amanda' ]; then
    if ! systemctl -q is-enabled 'xinetd.service' || ! systemctl -q is-active 'xinetd.service'; then
      echo 'amanda: Service xinetd must be running for bsd support, Try'
      if ! systemctl -q is-enabled 'xinetd.service'; then
        echo '  sudo systemctl enable xinetd.service'
      fi
      if ! systemctl -q is-active 'xinetd.service'; then
        echo '  sudo systemctl start xinetd.service'
      fi
    fi
  fi
}

post_install() {
  _post_upins "$1"
  echo 'amanda: Visit the Amanda wiki to configure your backup sets:'
  echo 'amanda:   http://wiki.zmanda.com/index.php/Getting_Started_With_Amanda'
  echo 'amanda: Examples are in /etc/amanda/examples/'
  if [ -s '/etc/xinetd.d/amanda' ]; then
    echo ''
    echo 'amanda: Restart xinetd to start the daemon listening for connections.'
  fi
  _GetOVars= _GetOLocal= bash -e -u "${_amlibexec}/amanda/amanda-install.sh" 'post_install'
}

# During an upgrade the new version's pre_upgrade() is run
pre_upgrade() {
  pre_install
  # Copy the file so we can get to the old vars after the upgrade
  if [ -s "${_amlibexec}/amanda/amanda-install.sh" ]; then
    (umask 077; cp --remove-destination "${_amlibexec}/amanda/amanda-install.sh" '/tmp/amanda-install.sh') # as close to atomic as I can get. -p not allowed here
  fi
}

pre_remove() {
  _GetOVars= _GetOLocal= bash -e -u "${_amlibexec}/amanda/amanda-install.sh" 'pre_remove'
  pre_upgrade
}

post_upgrade() {
  set -u
  if [ -s '/tmp/amanda-install.sh' ]; then
    local _GetOLocal='local'
    local _GetOVars=1
    source '/tmp/amanda-install.sh'
    unset _GetOVars
    rm '/tmp/amanda-install.sh'

    local _GetVars=1
    source "${_amlibexec}/amanda/amanda-install.sh"
    unset _GetVars

    if [ "${_Oamandauser}"  != "${_amandauser}"  ]; then
      echo 'amanda warning: changing username will likely invalidate all cron jobs and crash running jobs'
    fi
    # Copy and delete home if not already moved above
    if [ "${_Oamhome}" != "${_amhome}" ] && [ -d "${_Oamhome}" ] ; then
      if cp --no-target-directory -pr "${_Oamhome}/" "${_amhome}/"; then
        rm -rf "${_Oamhome}"
      fi
    fi
    if [ "${_Oamandauser}"  != "${_amandauser}"  ] ||
       [ "${_Oamandagroup}" != "${_amandagroup}" ] ||
       [ "${_Oamandauid}"   != "${_amandauid}"   ] ||
       [ "${_Oamandagid}"   != "${_amandagid}"   ] ||
       [ "${_Oamhome}"      != "${_amhome}"      ] ||
       [ "$(getent passwd "${_amandauser}" | cut -d':' -f6)" != "${_amhome}" ]; then
      echo 'amanda: Updating user/group info'
      if ! userdel --force "${_Oamandauser}"; then
        echo 'amanda: install package again after backup completes to fix'
      else
        echo "amanda: User ${_Oamandauser} deleted"
      fi
      groupdel "${_Oamandagroup}" 2>/dev/null || :
      echo "amanda: Group ${_Oamandagroup} deleted"
      if grep --color=auto -lr "${_Oamhome}" "${_ametc}"; then
        echo "amanda warning: Some backups reference the old home dir ${_Oamhome}"
      fi
    fi
  fi
  set +u
  _post_upins "$1"
}

post_remove() {
  set -u
  if [ -s '/tmp/amanda-install.sh' ]; then
    _GetOVars= _GetOLocal= bash -e -u '/tmp/amanda-install.sh' 'post_remove'
    rm '/tmp/amanda-install.sh'
  fi
  set +u
}
