#!/bin/bash
#
# see: dh_installdeb(1)

set -e

case "$1" in
    configure)
        sharedir='/usr/share/yandex-hbf-agent-init'
        # SystemD
        if [ -x /bin/systemctl ]
        then
            if [ ! -e /etc/systemd/system/yandex-hbf-agent.service ]
            then
                /bin/systemctl enable \
                               "$sharedir/systemd/yandex-hbf-agent.service"
            fi
            if [ ! -e /dev/lxd ]; then
                ln -sf ${sharedir}/systemd/modules-load.d/yandex-hbf-agent.conf /usr/lib/modules-load.d/yandex-hbf-agent.conf
                /bin/systemctl restart systemd-modules-load
            fi
        fi
        if [ -x /bin/systemd-tmpfiles ]
        then
            /bin/systemd-tmpfiles --create \
                                  /usr/lib/tmpfiles.d/yandex-hbf-agent.conf
        fi

        # It's better to return true on fail to not break installation of package.
        if [ -x /bin/systemctl ]; then
            /bin/systemctl start yandex-hbf-agent.service || :
        fi

        # Upstart
        if [[ -d /etc/init && ! -e /etc/init/yandex-hbf-agent.conf ]]
        then
            ln -s "$sharedir/upstart/yandex-hbf-agent.conf" \
                  /etc/init/yandex-hbf-agent.conf
        fi
        if [ -x /sbin/initctl ]
        then
            /sbin/initctl reload-configuration || true
            sleep 3
            # Same here, return true on fail to not break package installation
            /sbin/initctl start yandex-hbf-agent || :
        fi

    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0
