#!/bin/sh

<% if attributes[:deb_maintainerscripts_force_errorchecks?] -%>
set -e
<% end -%>

before_upgrade() {
<%# Making sure that at least one command is in the function -%>
<%# avoids a lot of potential errors, including the case that -%>
<%# the script is non-empty, but just whitespace and/or comments -%>
    :
<% if script?(:before_upgrade) -%>
<%=  script(:before_upgrade) %>
<% end -%>
}

before_install() {
<%# Making sure that at least one command is in the function -%>
<%# avoids a lot of potential errors, including the case that -%>
<%# the script is non-empty, but just whitespace and/or comments -%>
    :
<% if script?(:before_install) -%>
<%=  script(:before_install) %>
<% end -%>
}

if [ "${1}" = "install" -a -z "${2}" ]
then
    before_install
elif [ "${1}" = "upgrade" -a -n "${2}" ]
then
    upgradeFromVersion="${2}"
    before_upgrade "${upgradeFromVersion}"
elif [ "${1}" = "install" -a -n "${2}" ]
then
    upgradeFromVersion="${2}"
    # Executed when a package is removed but its config files aren't,
    # and a new version is installed.
    # Looks a _lot_ like an upgrade case, I say we just execute the
    # same "before upgrade" script as in the previous case
    before_upgrade "${upgradeFromVersion}"
elif echo "${1}" | grep -E -q '(fail|abort)'
then
    echo "Failed to install before the pre-installation script was run." >&2
    exit 1
fi
