#!/bin/sh

set -e

period="daily hourly monthly weekly"
operation="balance defrag scrub trim"

if [ "$1" = "remove" ] || [ "$1" = "purge" ]; then
    for i in $period; do
        for op in $operation; do
            if [ -L "/etc/cron.$i/btrfs-$op" ] \
                   && [ ! -e "/etc/cron.$i/btrfs-$op" ]; then
                rm "/etc/cron.$i/btrfs-$op"
            fi
            if [ -L "/etc/systemd/system/timers.target.wants/btrfs-$op.timer" ]; then
                rm "/etc/systemd/system/timers.target.wants/btrfs-$op.timer"
            fi
            if [ -L "/etc/systemd/system/btrfs-$op.timer.d/schedule.conf" ]; then
                rm "/etc/systemd/system/btrfs-$op.timer.d/schedule.conf"
                rmdir "/etc/systemd/system/btrfs-$op.timer.d"
            fi
        done
    done
fi
if [ -L "/etc/systemd/system/multi-user.target.wants/btrfsmaintenance-refresh.service" ]; then
    rm "/etc/systemd/system/multi-user.target.wants/btrfsmaintenance-refresh.service"
fi
if [ -L "/etc/systemd/system/multi-user.target.wants/btrfsmaintenance-refresh.path" ]; then
    rm "/etc/systemd/system/multi-user.target.wants/btrfsmaintenance-refresh.path"
fi

#DEBHELPER#

exit 0
