#!/bin/sh
set -e

case "$1" in
    purge)
        # Forget which instances were enabled at boot
        rm -f /etc/systemd/system/multi-user.target.wants/terraria@*.service
        rm -rf /etc/systemd/system/terraria@.service.d
        # The daemon: whether it's on, its certificate and the computers it trusts
        rm -f /etc/systemd/system/multi-user.target.wants/terractld.service
        rm -rf /etc/systemd/system/terractld.service.d /var/lib/terractl
        cat >&2 <<MSG
terraria-server-manager purged. Worlds and instances in /var/lib/terraria and the
'terraria' user were kept. Remove them manually if they are no longer needed:
  sudo rm -rf /var/lib/terraria && sudo userdel terraria
MSG
        ;;
esac

case "$1" in
    remove|purge)
        if [ -d /run/systemd/system ]; then
            systemctl daemon-reload || true
        fi
        ;;
esac

exit 0
