#!/bin/sh
# postrm script for s6-rc-service-xxxxx.
#
# See: dh_installdeb(1).

#set -e

# Summary of how this script can be called:
#        * <postrm> 'remove'
#        * <postrm> 'purge'
#        * <old-postrm> 'upgrade' <new-version>
#        * <new-postrm> 'failed-upgrade' <old-version>
#        * <new-postrm> 'abort-install'
#        * <new-postrm> 'abort-install' <old-version>
#        * <new-postrm> 'abort-upgrade' <old-version>
#        * <disappearer's-postrm> 'disappear' <overwriter>
#          <overwriter-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

SERVICE1="spice-vdagentd"
SERVICE2="service2"

USER_SERVICE1="spice-vdagent"
USER_SERVICE2="user-service2"

case "$1" in
    remove|purge)
    
    # Remove dead links from /etc that were installed part of this package
    rm -rf /etc/s6-rc/scripts/$SERVICE1
    rm -rf /etc/s6-rc/scripts/$SERVICE2
    rm -rf /etc/s6-rc/scripts/$USER_SERVICE1
    rm -rf /etc/s6-rc/scripts/$USER_SERVICE2
    
    # Recompile database after service removal
    if [ -x /usr/bin/s6-db-reload ] || [ -x /usr/local/bin/s6-db-reload ] ; then
		echo "recompiling system database"
		sleep 1
		s6-db-reload
	getent passwd $(basename -a /home/*) | awk -F: '{print $1}' | while read user; do
	echo "recompiling user database for $user"
			sleep 1
			runuser -u $user -- s6-db-reload -u
			done
	fi		
	;;
	
    purge|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

    *)
        echo "postrm 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

