#!/bin/sh


case "${ADT_REBOOT_MARK:-}" in
"")
    echo "First reboot the host to activate SystemV init system"
    apt install --assume-yes --allow-remove-essential sysvinit-core systemd-sysv- runit-init-
    echo "Downloading packages needed after the SystemV init switch"
    apt-get --download-only --assume-yes install elogind libpam-elogind orphan-sysvinit-scripts systemctl procps strace less
    cp /usr/share/sysvinit/inittab /etc/inittab
    sed -i "/ttyS0/ s/^#//" /etc/inittab
    if [ -e /usr/local/sbin/reboot ]; then
        echo "/usr/local/sbin/reboot already exists; cannot install reboot workaround" >&2
        exit 1
    fi
    cat > /usr/local/sbin/reboot <<'EOF'
#!/bin/sh
exec /usr/bin/systemctl reboot "$@"
EOF
    chmod 0755 /usr/local/sbin/reboot
    /tmp/autopkgtest-reboot "fixinit"
    ;;
fixinit)
    echo "Installing packages required after the SystemV init switch"
    rm -f /usr/local/sbin/reboot
    apt-get --no-download --assume-yes install elogind libpam-elogind orphan-sysvinit-scripts systemctl procps strace less
    echo "Second reboot to activate the installed packages"
    /tmp/autopkgtest-reboot "elogind"
    ;;
elogind)
    echo "$ADT_REBOOT_MARK"
    ;;
*)
    echo "Unexpected autopkgtest reboot marker: $ADT_REBOOT_MARK" >&2
    exit 1
    ;;
esac

is_syslogng_run()
{
    LINES=$(ps uawx | grep "syslog-ng " | grep -v grep)
    echo $LINES | grep -q syslog-ng
    RET=$?
    return $RET
}

exit_with_comment()
{
    comment=$1
    exit_code=$2
    echo $comment
    ps uawx | grep syslog-ng | grep -v grep
    sleep 5
    ps uawx | grep syslog-ng | grep -v grep
    exit $exit_code
}

set -e
echo "Checking if syslog-ng was able to start"
is_syslogng_run || exit_with_comment "Syslog-ng is not running, cannot test" 0

echo "Testing stop parameter"
/etc/init.d/syslog-ng stop

is_syslogng_run && exit_with_comment "Syslog-ng cannot be stopped" 1

echo "Testing status when syslog-ng is not running"
/etc/init.d/syslog-ng status && exit_with_comment "Syslog-ng status returned with error"

echo "Testing start parameter"
/etc/init.d/syslog-ng start

is_syslogng_run || exit_with_comment "Syslog-ng cannot be started" 1

echo "Testing status when syslog-ng is running"
/etc/init.d/syslog-ng status || exit_with_comment "Syslog-ng status returned with error"

echo "Testing restart parameter"
PIDFILE1=$(cat /var/run/syslog-ng.pid)
/etc/init.d/syslog-ng restart

is_syslogng_run || exit_with_comment "Syslog-ng stopped during restart" 1
PIDFILE2=$(cat /var/run/syslog-ng.pid)

test $PIDFILE1 -ne $PIDFILE2 || exit_with_comment "Syslog-ng was not restarted" 1

echo "Stopping syslog-ng for the next test"
/etc/init.d/syslog-ng stop

is_syslogng_run && exit_with_comment "Syslog-ng cannot be stopped 2" 1

echo "Testing wether restart start syslog-ng or not"
/etc/init.d/syslog-ng restart

is_syslogng_run || exit_with_comment "Syslog-ng is not started by restart" 1

echo "Testing the try-restart parameter"
PIDFILE1=$(cat /var/run/syslog-ng.pid)
/etc/init.d/syslog-ng try-restart

is_syslogng_run || exit_with_comment "Syslog-ng stopped during restart" 1
PIDFILE2=$(cat /var/run/syslog-ng.pid)

test $PIDFILE1 -ne $PIDFILE2 || exit_with_comment "Syslog-ng was not restarted by try-restart" 1

echo "Stopping syslog-ng before the next test"
/etc/init.d/syslog-ng stop

is_syslogng_run && exit_with_comment "Syslog-ng cannot be stopped 3" 1

echo "try-restart should not start the daemon if it didn't run before"
/etc/init.d/syslog-ng try-restart

is_syslogng_run && exit_with_comment "Syslog-ng started by try-restart" 1

exit 0
