#!/bin/sh
# postrm script for weewx debian package
# Copyright 2013-2024 Matthew Wall
#
# ways this script might be invoked:
#
# postrm remove
# postrm purge
# old-postrm upgrade new-version
# disappearer's-postrm disappear overwriter overwriter-version
# new-postrm failed-upgrade old-version
# new-postrm abort-install
# new-postrm abort-install old-version
# new-postrm abort-upgrade old-version

# abort if any command returns error
set -e

# see which init system is running
pid1=none
if [ -d /run/systemd/system ]; then
    pid1=systemd
else
    pid1=init
fi

case "$1" in
remove)
    # remove the startup configuration
    if [ "$pid1" = "systemd" ]; then
        echo "Removing systemd units"
        systemctl disable weewx > /dev/null || true
        dst="/usr/lib/systemd/system"
        if [ ! -d $dst ]; then
            dst="/lib/systemd/system"
        fi
        for f in weewx.service weewx@.service; do
	    if [ -f $dst/$f ]; then
	        rm -f $dst/$f
	    fi
        done
    elif [ "$pid1" = "init" ]; then
        echo "Removing SysV rc script"
        update-rc.d weewx remove > /dev/null || true
        if [ -f /etc/init.d/weewx ]; then
            rm /etc/init.d/weewx
        fi
    fi
    # remove udev rules
    dst=/usr/lib/udev/rules.d
    if [ ! -d $dst ]; then
        dst=/lib/udev/rules.d
    fi
    if [ -f $dst/60-weewx.rules ]; then
        echo "Removing udev rules"
        rm -f $dst/60-weewx.rules
    fi
    ;;

purge)
    # remove any debconf entries
    if [ -e /usr/share/debconf/confmodule ]; then
        . /usr/share/debconf/confmodule
        db_purge
    fi
    ;;

upgrade)
    ;;

abort-install)
    ;;

failed-upgrade)
    ;;

abort-install)
    ;;

abort-upgrade)
    ;;

esac

#DEBHELPER#

exit 0
