#!/bin/bash

# shellcheck source=/dev/null
source /usr/lib/ee-ips/helpers

case "$1" in
    list)
        if [[ "$2" == "enabled" ]]; then
            list_hooks enabled
        else
            list_hooks
        fi
        ;;

    enable)
        hook="${2:-}"
        [[ -z "$hook" ]] && echo "usage: $0 enable [hook]" && exit 1
        src_hook_file=$( locate_hook "$hook" ) || (
            echo "Hook '$hook' not found (or not executable)";
            exit 1;
        )
        dst_hook_file="$ENABLED_HOOKS_DIR/$hook"
        if [[ -e "$dst_hook_file" ]]; then
            if [[ -h "$dst_hook_file" ]] && [[ "$( realpath "$dst_hook_file" )" == "$src_hook_file" ]]; then
                echo "Hook '$hook' already enabled"
                exit 0
            fi
            echo "$dst_hook_file already exists and is not an symlink to $src_hook_file"
            exit 1
        fi

        if ln -s "$src_hook_file" "$dst_hook_file"; then
            echo "Hook $hook enabled"
            exit 0
        fi
        echo "Failed to enabled hook $hook."
        exit 1
        ;;

    disable)
        hook="${2:-}"
        [[ -z "$hook" ]] && echo "usage: $0 disable [hook]" && exit 1
        hook_file="$ENABLED_HOOKS_DIR/$hook"
        [[ ! -e "$hook_file" ]] && echo "No hook $hook found" && exit 1
        [[ ! -h "$hook_file" ]] && echo "Hook $hook file is not a symlink" && exit 1
        if rm "$hook_file"; then
            echo "Hook $hook disabled"
            exit 0
        fi
        echo "Failed to disabled hook $hook."
        exit 1
        ;;

    install)
        ERROR=0
        ARGS=( "${@:2}" )
        # shellcheck disable=SC2034
        in_array "auto" "${ARGS[@]}" && AUTO=1
        mapfile -t hooks < <( list_hooks )
        for hook in "${hooks[@]}"; do
            [[ "${#ARGS[@]}" -gt 0 ]] && ! in_array "$hook" "${ARGS[@]}" && continue
            dst_hook_file="$ENABLED_HOOKS_DIR/$hook"
            if [[ -e "$dst_hook_file" ]]; then
                echo "Hook $hook already enabled"
                continue
            fi
            src_hook_file=$( locate_hook "$hook" ) || continue
            "$src_hook_file" detect || continue
            confirm "Install hook for $hook" || continue
            echo "Try to run hook for a first time:"
            while ! "$src_hook_file"; do
                if ! confirm "Failure. Retry"; then
                    echo Abort.
                    ERROR=1
                    continue 2
                fi
            done
            echo "Successfully executed hook $hook, enable it"
            if ln -s "$src_hook_file" "$dst_hook_file"; then
                echo "Hook $hook enabled"
            else
                echo "Failed to enabled hook $hook."
                ERROR=1
            fi
        done
        exit "$ERROR"
        ;;

    update)
        run-parts "$ENABLED_HOOKS_DIR"
        exit $?
        ;;

    *)
        echo "usage: $( basename "$0" ) [list|enable|disable|install|update]"
        echo "  list [enabled]      List existing (or enabled) hooks"
        echo "  enable [hook]       Enable one hook"
        echo "  disable [hook]      Enable one hook"
        echo "  install [auto]      Detect and (auto-)install compatible hooks"
        echo "  update              Run installed hooks to update EE IPs on system"
        echo
        exit 1
esac
