#!/bin/sh
set -e

#run with
#autopkgtest ../dh-runit_2.17.1_all.deb ../runit-helper_2.17.1_all.deb  . --test-name=purge-sv_bin -- qemu ../autopkgtest.img

rcadd() { printf '%s\n' "$*"; rc=$((rc+1)); }
fail() { rcadd "FAIL: $*"; }
warn() { printf '%s\n' "WARN: $*"; }

echo "preparing test setup..."
#enable verbose/debug mode
touch /etc/runit/verbose
touch /etc/runit/debug

rc=0
NAME=installedfull-sv
PKG=dh-runit
cp -r "debian/tests/sv/$NAME" "/usr/share/runit/sv/"


#runsvchdir to default (trigger_sv enables there)
if [ ! -h /etc/service ]; then
   fail "setup: /etc/service is not a symlink" && exit 1
else
    runsvchdir default
    servicelink=$(readlink -f /etc/service)
    if [ "$servicelink" != /etc/runit/runsvdir/default ]; then
        fail "setup: /etc/service does not point to default"  && exit 1
    fi
fi

echo "..done"

if systemctl is-active --quiet runit.service ; then

echo "testing purge: runtime dir /usr/share/runit/sv.now/$NAME .."
    /lib/runit/trigger_sv setup
    [ ! -h "/etc/service/$NAME" ] && fail "purge sv: expected enabled, but it's not"
    for i in 1 2 3 4 5 6 7; do #wait for the service to be up
        sleep 1 && echo "waiting for service $NAME to be up: $i"
        [ -p "/etc/service/$NAME/supervise/ok" ] && break
    done
    sleep 1
    sv check "$NAME" || fail "$NAME: not in the requested status"
    [ -d "/usr/share/runit/sv.now/$NAME" ] || fail "$NAME dir not found in /usr/share/runit/sv.now (before purge)"
    [ -d "/var/log/runit/$NAME" ] || fail "$NAME log dir not found in /var/log/runit/ (before purge)"
    #now purge
    NAME=$NAME PKG=$PKG /usr/lib/runit-helper/runit-helper sv_purge
    [ ! -d "/usr/share/runit/sv.now/$NAME" ] || fail "$NAME dir found in /usr/share/runit/sv.now (after purge)"
    [ ! -d "/var/log/runit/$NAME" ] || fail "$NAME log dir found in /var/log/runit/ (after purge)"
    [ ! -h "/etc/service/$NAME" ] || fail "purge sv: expected disabled (no link), but but link found in /etc/service"
    [ ! -h "/etc/service/.$NAME" ] || fail "purge sv: expected disabled (no link), but but .link found in /etc/service"
    [ ! -h "/etc/runit/runsvdir/default/$NAME" ] || fail "purge sv: expected disabled (no link), but but link found in default dir"
    [ ! -h "/etc/runit/runsvdir/default/.$NAME" ] || fail "purge sv: expected disabled (no link), but but .link found in default dir"
    rm -rf "/usr/share/runit/sv/$NAME"

echo "..done"

#ALRM // runsvdir forced rescan
echo "forcing runsvdir to rescan.."
systemctl kill --signal=14 runit.service
sleep 2
echo "..done"
#NOTE either remove supervise here, or use chpst -L when waiting for service to be up below
#NOTE 2:FIXED by removing the supervise dir on purge // #rm -rf "/run/runit/supervise/$NAME"

echo "testing purge: etc dir /etc/sv/$NAME .."
    rm -rf "/usr/share/runit/sv/$NAME"
    rm -rf "/usr/share/runit/sv.now/$NAME"
    rm -rf "/etc/sv/$NAME"
    rm -rf "/var/log/runit/$NAME"
    cp -r "debian/tests/sv/$NAME" "/etc/sv/"
    cpsv p "$NAME" || fail "cpsv: failed to create symlinks in /etc/sv/$NAME"
    /lib/runit/trigger_sv setup
    [ -d "/etc/sv/$NAME" ] || fail "$NAME dir not found in /etc/sv (before purge)"
    [ ! -d "/usr/share/runit/sv.now/$NAME" ] || fail "$NAME dir found in /usr/share/runit/sv.now (before etc purge test)"
    [ ! -d "/usr/share/runit/sv/$NAME" ] || fail "$NAME dir found in /usr/share/runit/sv (before etc purge test)"
    [ ! -h "/etc/service/$NAME" ] && fail "purge sv: expected enabled, but it's not"
    servicelink=$(readlink -f /etc/service/$NAME)
    if [ "$servicelink" != /etc/sv/$NAME ]; then
        fail "setup: /etc/service/$NAME does not point to /etc/sv/$NAME"
    fi
    [ ! -h "/etc/service/$NAME" ] && fail "purge sv: expected enabled, but it's not"
    for i in 1 2 3 4 5 6 7; do #wait for the service to be up
        sleep 1 && echo "waiting for service $NAME to be up: $i"
        #NOTE use chpst -L if supervise dir was not removed
        #chpst -L  "/etc/service/$NAME/supervise/lock" true || break
        [ -p "/etc/service/$NAME/supervise/ok" ] && break
    done
    sleep 1
    sv check "$NAME" || fail "$NAME: not in the requested status"
    [ -d "/etc/sv/$NAME" ] || fail "$NAME log dir not found in /var/log/runit/ (before etc purge)"
    [ -d "/var/log/runit/$NAME" ] || fail "$NAME log dir not found in /var/log/runit/ (before etc purge)"
    #now purge
    NAME=$NAME PKG=$PKG /usr/lib/runit-helper/runit-helper sv_purge
    [ ! -d "/etc/sv/$NAME" ] || fail "$NAME log dir not found in /var/log/runit/ (after etc purge)"
    [ ! -d "/var/log/runit/$NAME" ] || fail "$NAME log dir not found in /var/log/runit/ (after etc purge)"
    [ ! -h "/etc/service/$NAME" ] || fail "purge sv: expected disabled (no link), but but link found in /etc/service"
    [ ! -h "/etc/service/.$NAME" ] || fail "purge sv: expected disabled (no link), but but .link found in /etc/service"
    [ ! -h "/etc/runit/runsvdir/default/$NAME" ] || fail "purge sv: expected disabled (no link), but but link found in default dir"
    [ ! -h "/etc/runit/runsvdir/default/.$NAME" ] || fail "purge sv: expected disabled (no link), but but .link found in default dir"
echo "..done"
else
	 warn "can't perform purge_sv test, skipping (runsvdir not running)"
fi

exit "$rc"
