#!/bin/bash
# postinst script for xivocc-recording
#
# see: dh_installdeb(1)

function fix_paths {
    chown asterisk:www-data /etc/asterisk/extensions_extra.d/xivocc-recording.conf
    chown -R asterisk:asterisk /var/spool/xivocc-recording/
    chown -R asterisk:asterisk /var/log/xivocc-recording/
}

case "$1" in
    configure)
        if [ -z "$2" ]; then
            # première installation
            fix_paths
            asterisk -rx "dialplan reload" 2>/dev/null

            # from call-recording-filtering
            chown -R asterisk:asterisk /usr/share/asterisk/agi-bin

        else
            # mise à jour
            fix_paths
            dpkg --compare-versions "$2" le "1.2"
            if [ $? -eq 0 ]; then
                mv /usr/share/xivo-recording/failed/* /var/spool/xivocc-recording/failed/
                rm -rf /usr/share/xivo-recording
            fi;
        fi;
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
