#! /bin/sh -e

set -e

if [ "$1" = purge ]; then

	if [ -e /usr/share/debconf/confmodule ]; then
		. /usr/share/debconf/confmodule
		db_purge
	fi

	if [ "$(readlink /etc/systemd/system/samba-ad-dc.service)" = '/dev/null' ]; then
		# Remove masking done by postinst (#832352)
		rm /etc/systemd/system/samba-ad-dc.service
	fi

	if [ -f /etc/apparmor.d/samba/smbd-shares ]; then
		rm /etc/apparmor.d/samba/smbd-shares
	fi
fi

# Automatically added by dh_installdeb/13.23~bpo12+1
dpkg-maintscript-helper rm_conffile /etc/init.d/samba 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/nmbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/reload-smbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/samba-ad-dc.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/smbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper mv_conffile /etc/default/ctdb /etc/ctdb/ctdbd.conf 2:4.5.0\+dfsg-1\~ -- "$@"
# End automatically added section
# Automatically added by dh_installsystemd/13.23~bpo12+1
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.23~bpo12+1
if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'ctdb.service' 'nmbd.service' 'samba-bgqd.service' 'smbd.service' 'winbind.service' 'wsdd.service' >/dev/null || true
	fi
fi
# End automatically added section

# postrm script for ctdb
#
# see: dh_installdeb(1)

# 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

disable_legacy() { # From ctdb/packaging/RPM/ctdb.spec.in
    # Uninstall, not upgrade.  Clean up by removing any remaining links.
    if [ "$1" = "0" ] ; then
        for i in "/etc/ctdb/events/legacy/"*.script ; do
            if [ -L "$i" ] ; then
                rm -f "$i"
            fi
        done
    fi
}

remove_leftover() {

    # Remove files created after initalization

    rm -f /etc/ctdb/nodes
    rm -f /etc/ctdb/public_addresses
    rm -f /var/lib/ctdb/volatile/*
    rm -f /var/lib/ctdb/state/*
    rm -f /var/lib/ctdb/persistent/*
    rm -rf /var/lib/ctdb/scripts
}

case "$1" in
    purge)
        disable_legacy 0
        remove_leftover
    ;;

    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

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

# samba-common.postrm
if [ "$1" = purge ]; then

	# Remove Samba's state files, both volatile and non-volatile
	rm -Rf /run/samba/ /var/cache/samba/ /var/lib/samba

	# Remove log files
	rm -Rf /var/log/samba/

	# Remove config files
	rm -Rf /etc/samba/
	if [ -x "`which ucf 2>/dev/null`" ]; then
		ucf --purge /etc/samba/smb.conf
	fi
	if [ -x "`which ucfr 2>/dev/null`" ]; then
		ucfr --purge samba-common /etc/samba/smb.conf
	fi
	if [ -f /var/lib/samba/dhcp.conf ]; then
		rm /var/lib/samba/dhcp.conf
	fi
fi

# winbind.postrm
if [ "$1" = purge ]; then
	winbindd_privileged_socket_directory='/var/lib/samba/winbindd_privileged'
	rm -rf /var/cache/samba/netsamlogon_cache.tdb /var/cache/samba/winbindd_cache.tdb
	rm -rf "$winbindd_privileged_socket_directory"
	rm -rf /var/log/samba/log.winbind* /var/log/samba/log.wb*
	rm -rf /run/samba/winbindd.pid
fi

chattr -i /var/empty
rmdir /var/empty

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

# Automatically added by dh_installdeb/13.23~bpo12+1
dpkg-maintscript-helper rm_conffile /etc/init.d/samba 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/nmbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/reload-smbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/samba-ad-dc.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init/smbd.conf 2:4.6.5\+dfsg-5\~ -- "$@"
dpkg-maintscript-helper mv_conffile /etc/default/ctdb /etc/ctdb/ctdbd.conf 2:4.5.0\+dfsg-1\~ -- "$@"
# End automatically added section
# Automatically added by dh_installsystemd/13.23~bpo12+1
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.23~bpo12+1
if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'ctdb.service' 'nmbd.service' 'samba-bgqd.service' 'smbd.service' 'winbind.service' 'wsdd.service' >/dev/null || true
	fi
fi
# End automatically added section


exit 0
