#!/bin/bash

set -e

# This is a wrapper meant to be installed into /usr/sbin for debian
# packages, so root can run the scripts safely.

get_help () {
    cat <<EOF
Usage: amusewiki action [ argument1, argument2]

Run the amusewiki scripts as user amusewiki in the correct directory.

Available commands:
EOF
    for exe in /usr/bin/amusewiki-*; do
        echo $exe | sed -e 's!/usr/bin/amusewiki-!amusewiki !'
    done

    cat <<EOF

amusewiki shell

The shell subcommand will start a shell with the amusewiki user

See man amusewiki-<action> for usage of each command
or run amusewiki <action> --help

Other useful commands:

amusewiki [ start | stop | restart ]

start and stop actions simply call systemctl, while the restart action
performs a restart without downtime (so being a bit more useful than
just "systemctl restart amusewiki-web amusewiki-jobber"

EOF

	exit 1
}


if [ "$#" == "0" ]; then
    get_help;
fi

action=$1
shift
if [ "x$action" = "xshell" ]; then
    exec su - amusewiki
    exit
fi

if [ "x$action" = "xstart" ]; then
    systemctl start amusewiki-web amusewiki-jobber
    exit
fi

if [ "x$action" = "xstop" ]; then
    systemctl stop amusewiki-web amusewiki-jobber
    exit
fi

if [ "x$action" = "xrestart" ]; then
    cd /var/lib/amusewiki
    tmppidfile=/var/lib/amusewiki/.amusewiki-backup.tmp.pid
    sudo -u amusewiki -- /usr/bin/plackup -s FCGI \
         --listen /var/lib/amusewiki/amusewiki.socket \
         --pid $tmppidfile \
         --nproc 3 -E deployment \
         /usr/share/perl5/AmuseWikiFarm/psgi/amusewiki.psgi &

    for i in $(seq 1 10); do
        echo "$i. waiting 10 seconds for the backup app to come up"
        sleep 10
        if [ -f "$tmppidfile" ]; then
            break
        fi
    done
    if [ -f "$tmppidfile" ]; then
        temp_pid=`cat $tmppidfile`
        if [ -n "$temp_pid" ]; then
            echo "Started copy of the application with pid $temp_pid."
            echo "If this script exits prematurely, please kill it yourself."
            echo "Waiting for the backup app to come up..."
            sleep 20
            echo "Restarting the apps"
            systemctl restart amusewiki-web amusewiki-jobber
            echo "Waiting for the real app to come up..."
            sleep 20
            echo "Stopping the backup app $temp_pid"
            for i in 1 2 3; do
                if sudo -u amusewiki -- kill -0 $temp_pid >/dev/null 2>/dev/null; then
                    echo "Trying to kill $temp_pid"
                    sudo -u amusewiki -- kill $temp_pid || echo;
                    sleep 2
                fi
            done

            if sudo -u amusewiki -- kill -0 $temp_pid >/dev/null 2>/dev/null; then
                echo "$temp_pid is still alive"
            else
                echo "All done, no errors"
            fi
            exit
        else
            echo "$tmppidfile is empty"
        fi
    else
        echo "Cannot find PID file for temporary amusewiki process, expecting $tmppidfile"
        exit 2
    fi
fi

if [ ! -x "/usr/bin/amusewiki-$action" ]; then
    echo "Unknown action $action"
    echo
    get_help;
fi

cd /var/lib/amusewiki
sudo -u amusewiki -- amusewiki-$action "$@"
