function post_install() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ]; then
    systemctl daemon-reload
    systemctl enable vpnrd.service
    systemctl start vpnrd.service
    systemctl enable --now vpnr-update.timer
fi

}

function post_remove() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ]; then systemctl daemon-reload; fi

}

function post_upgrade() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ]; then
    systemctl daemon-reload
    systemctl enable vpnrd.service
    systemctl start vpnrd.service
    systemctl enable --now vpnr-update.timer
fi

}

function pre_install() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ] && [ -x /usr/lib/vpnr/vpnrd ]; then
    # A manual installation can leave the executable after removing its unit.
    # Still clean its ledger; a missing unit is not a failed service stop.
    load_state=$(systemctl show --property=LoadState --value vpnrd.service)
    if [ "$load_state" != not-found ]; then systemctl stop vpnrd.service; fi
    /usr/lib/vpnr/vpnrd cleanup
fi

}

function pre_remove() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ]; then
    systemctl stop vpnrd.service
    /usr/lib/vpnr/vpnrd cleanup
    case "${1:-}" in
        upgrade|deconfigure) ;;
        *) systemctl disable --now vpnr-update.timer; systemctl disable vpnrd.service ;;
    esac
fi

}

function pre_upgrade() {
#!/bin/sh
set -eu
if [ -d /run/systemd/system ] && [ -x /usr/lib/vpnr/vpnrd ]; then
    # A manual installation can leave the executable after removing its unit.
    # Still clean its ledger; a missing unit is not a failed service stop.
    load_state=$(systemctl show --property=LoadState --value vpnrd.service)
    if [ "$load_state" != not-found ]; then systemctl stop vpnrd.service; fi
    /usr/lib/vpnr/vpnrd cleanup
fi

}

