#!/bin/sh
# prerm script for weewx debian package
# Copyright 2013-2023 Matthew Wall
#
# ways this script might be invoked:
#
# prerm remove
# old-prerm upgrade new-version
# conflictor's-prerm remove in-favor package new-version
# deconfigured's-prerm deconfigure in-favour package-being-installed version
#   [removing conflicting-package version]
# new-prerm failed-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|upgrade)
    # stop the weewx daemon
    echo "Stopping weewxd using $pid1"
    if [ "$pid1" = "systemd" ]; then
	systemctl stop weewx > /dev/null || true
    elif [ "$pid1" = "init" ]; then
        invoke-rc.d weewx stop > /dev/null || true
    fi
    # remove any bytecompiled code
    if [ -d /usr/share/weewx ]; then
        find /usr/share/weewx -name '*.pyc' -delete
        find /usr/share/weewx -name __pycache__ -delete
    fi
    if [ -d /etc/weewx/bin ]; then
        find /etc/weewx/bin -name '*.pyc' -delete
        find /etc/weewx/bin -name __pycache__ -delete
    fi
    ;;
esac

exit 0
