#!/bin/bash

set -e

port=8000

# Simple service for python http server

cat > $AUTOPKGTEST_TMP/index.html <<EOF
<html><body>hello world</body></html>
EOF

cat > /etc/systemd/system/http-server-1.service <<EOF
[Service]
ExecStart=/usr/bin/python3 -m http.server --bind 127.0.0.1 $port
WorkingDirectory=/$AUTOPKGTEST_TMP
EOF

cp /etc/systemd/system/http-server-1.service \
   /etc/systemd/system/http-server-2.service

systemctl daemon-reload

# Cleanup

cleanup() {
    if systemctl is-active --quiet http-server-1.service; then
        systemctl stop http-server-1.service
    fi
    if systemctl is-active --quiet http-server-2.service; then
        systemctl stop http-server-2.service
    fi
}

trap cleanup EXIT

# Tests

fail() {
    echo "$@" >&2
    exit 1
}

run_test() {
    local output=
    local rc=

    echo
    echo "================================================================"
    echo "Test  : $1"
    echo "Run   : kali-service-$2 $3 $4"
    echo "Expect: $5"
    
    rc=0
    output=$(kali-service-$2 $3 $4) || rc=$?
    # strip colors otherwise we can't match with grep
    output=$(echo "$output" | ansifilter)
    echo "$output"

    case $5 in
        success) [ $rc -eq 0 ] || fail "Exit code: $rc" ;;
	failure) [ $rc -ne 0 ] || fail "Exit code: $rc" ;;
	*) exit 1 ;;
    esac

    shift 5
    while [ $# -gt 0 ]; do
        case $1 in
            -i|--include)
                echo "$output" | grep -q "$2" || fail "Not found in output: $2" ;;
	    -x|--exclude)
                echo "$output" | grep -q "$2" && fail "Found in output: $2" ;;
	    *)  exit 1 ;;
        esac
        shift 2
    done
}

#### Run the tests!

# service should start
run_test "Start service" \
    start http-server-1 $port success \
    -i "Please wait for the http-server-1 service" \
    -i "Web UI: http://127.0.0.1:$port"

# service is already started, shouldn't fail, should only display info
run_test "Start service (already started)" \
    start http-server-1 $port success \
    -x "Please wait for the http-server-1 service" \
    -i "Web UI: http://127.0.0.1:$port"

# port is already taken, should fail
# COMMAND is a header from lsof, UID a header from ps
run_test "Start service (port already taken)" \
    start http-server-2 $port failure \
    -i "Something is already using port" \
    -i "COMMAND" \
    -i "UID"

# service should stop
run_test "Stop service" \
    stop http-server-1 $port success \
    -i "Active: inactive"

#### Now let's remove some packages and try again

apt-get purge -y --autoremove curl lsof procps
test -x /usr/bin/wget

# service should start. Note that, given that wget is still installed,
# we can still check if the HTTP server is up and happy.
run_test "Start service" \
    start http-server-1 $port success \
    -i "Please wait for the http-server-1 service" \
    -i "Web UI: http://127.0.0.1:$port"

# port is already taken, should fail
# lsof and procps are not installed so we don't get much details
run_test "Start service (port already taken)" \
    start http-server-2 $port failure \
    -i "Something is already using port"

# service should stop
run_test "Stop service" \
    stop http-server-1 $port success \
    -i "Active: inactive"

#### Finally remove wget

apt-get purge -y --autoremove wget

# service should start. Note that, without curl nor wget, 
# the script just display the systemd service status.
run_test "Start service" \
    start http-server-1 $port success \
    -i "Please wait for the http-server-1 service" \
    -i "Active: active"

#### More tests

# should fail
run_test "Start non-existent service" \
	start non-existent.service 1234 failure 2>&1
