#!/bin/sh

# Control AppSupport instances.

QUIET=
NO_BLOCK=

detect_instance() {
    ALL_SERVICES="$(appsupport-config --name --instance "*" --prepare-name)"
    INSTANCE_COUNT=$(systemctl list-units --state=active --state=activating --state=deactivating "$ALL_SERVICES" | grep ".service.*loaded.*activ" | wc -l)
    if [ $INSTANCE_COUNT -eq 0 ]; then
        >&2 echo "appsupport-service: No instances active."
        exit 1
    elif [ $INSTANCE_COUNT -gt 1 ]; then
        >&2 echo "appsupport-service: More than one instance active, $1 requires instance name."
        >&2 echo -n "appsupport-service: Possible choices: "
        systemctl list-units --state=active --state=activating --state=deactivating "$ALL_SERVICES" | grep ".service.*loaded.*activ" | cut -d' ' -f1 | cut -d'@' -f2 | cut -d'.' -f1 | tr '\n' " " >&2
        >&2 echo "\"*\""
        exit 1
    fi
    INSTANCE=$(systemctl list-units --state=active --state=activating --state=deactivating "$ALL_SERVICES" | grep ".service.*loaded.*activ" | xargs | cut -d' ' -f1 | cut -d'@' -f2 | cut -d'.' -f1)
}

while [ $# -gt 0 ]; do
    case $1 in
        --help|-h)
            echo "Usage: $0 [option] <command>"
            echo ""
            echo " Commands: start stop restart status is-active is-enabled enable disable <instance>"
            echo "           active"
            echo "           --follow --boot [instance]"
            echo ""
            echo " Tip: Use appsupport-service stop \"*\" to stop all instances."
            echo ""
            echo " Options:"
            echo "  --help      -h      This help."
            echo "  --quiet     -q      Be more quiet when appropriate."
            echo "  --follow    -f      Follow journal."
            echo "  --boot      -b      Print journal since boot."
            echo "  --version           Print AppSupport version."
            echo "  --no-block          Exit immediately."
            echo ""
            exit 0
            ;;
        --version)
            exec appsupport-config --version
            ;;
        -f|-b|--follow|--boot)
            CMD="$1"
            INSTANCE="$2"
            if [ -z "$INSTANCE" ]; then
                detect_instance $CMD
            fi
            SERVICE="$(appsupport-config --instance "$INSTANCE" --name)"
            PREPARE_SERVICE="$(appsupport-config --instance "$INSTANCE" --prepare-name)"
            exec journalctl $CMD -u "$SERVICE" -u "$PREPARE_SERVICE"
            ;;
        active)
            $0 "list=active" | grep " active" | cut -d' ' -f1 | cut -d'@' -f2 | cut -d'.' -f1
            exit 0
            ;;
        list*)
            WHAT="$(echo "$1" | cut -d'=' -f2)"
            if [ "$WHAT" = "list" ]; then
                WHAT=""
            else
                WHAT="--state=$WHAT"
            fi
            ALL_SERVICES="$(appsupport-config --instance "*" --name)"
            NAME_PREFIX="$(echo "$ALL_SERVICES" | cut -d'*' -f1)"
            systemctl list-units $WHAT "$ALL_SERVICES" | grep -o "$NAME_PREFIX.*" | tr -s " " | cut -d' ' -f1,3
            exit 0
            ;;
        start|stop|restart|status|is-active|is-enabled|enable|disable)
            CMD="$1"
            INSTANCE="$2"
            shift
            if [ "$(echo "$INSTANCE" | cut -b1)" = "-" ]; then
                >&2 echo "appsupport-service: $INSTANCE is invalid instance name."
                exit 1
            elif [ -n "$INSTANCE" ]; then
                shift
            else
                case $CMD in
                    stop|restart|status)
                        detect_instance $CMD
                        ;;
                    *)
                        >&2 echo "appsupport-service: $CMD requires instance name."
                        exit 1
                        ;;
                esac
            fi
            SERVICE="$(appsupport-config --instance "$INSTANCE" --name)"
            exec systemctl $QUIET $NO_BLOCK $CMD "$@" "$SERVICE"
            ;;
        --quiet|-q)
            QUIET="-q"
            ;;
        --no-block)
            NO_BLOCK="--no-block"
            ;;
        *)
            >&2 echo "appsupport-service: Invalid arguments."
            exit 1
            ;;
    esac

    shift
done

>&2 echo "appsupport-service: Invalid arguments."
exit 1
