#!/bin/sh

cd "$AUTOPKGTEST_TMP"

# directory for settings file, logs, and other cruft
DIR="settings"

fresh_ini() {
	mkdir -p "$AUTOPKGTEST_TMP/$DIR" >/dev/null 2>&1 || exit 1
	mktemp -u -p "$AUTOPKGTEST_TMP/$DIR" -t XXXXXXXX.ini
}

cleanup() {
	[ -d "$DIR" ] && rm -vrf "$DIR"
}

# display usage info
sabnzbdplus --help --config-file "$(fresh_ini)"
RET1=$?
cleanup

# console run, empty settings file
sabnzbdplus --logging 1 --browser 0 --config-file "$(fresh_ini)" &
pid=$!
sleep 20
kill -s TERM $pid || kill -s KILL $pid
# this doesn't strictly collect the retval from the program itself
# but gets close enough since most error conditions would cause an
# early exit of sab, which in turn causes kill to return non-zero
RET2=$?
sleep 10
cleanup

exit $((RET1+RET2))
