#!/usr/bin/env bash
set -Eeuo pipefail
ONMP_HOME=$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")/.." && pwd)
. "$ONMP_HOME/include/release.sh"
. "$ONMP_HOME/include/platform.sh"

usage()
{
    cat <<'EOF'
ONMP for Ubuntu / x86_64 (26.04 experimental)
  onmp --version
  onmp install {onmp|nginx|db|mphp|redis|mongodb}
  onmp install mongodb [7.0|8.0]
  onmp install redis [7.4|8.0|8.6.0|...]
  onmp install {node|pnpm|composer} [series-or-patch-version]
  onmp install nginx [1.28.3|1.30.0|1.30.5]
  onmp install mphp [8.2|8.3|8.4|8.5]
  onmp install db [mysql8.4|mariadb10.11|mariadb11.4|mariadb11.8]
  onmp {redis|mongodb} {start|stop|restart|reload|status|check|version}
  onmp {start|stop|restart|reload|status}
  onmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|restart|reload|status}
  onmp vhost {add|list|del}
  onmp database {add|list|edit|del}
  onmp ssl add
  onmp {dns|onlyssl} <provider>
  onmp upgrade {nginx|php|mphp|phpmyadmin}
  onmp addons {install|uninstall} <addon>
  onmp php-extension {redis|mongodb} [/usr/local/php]
  onmp php-cli enable [/usr/local/php]
Credentials are stored under /etc/onmp/credentials (root only).
EOF
}

core_units()
{
    local full=0
    [[ ! -s "$ONMP_HOME/.state/stack-command" ]] || full=1
    if [[ "$full" == 1 || -f /var/lib/onmp/nginx.owner ]]; then echo nginx; fi
    if [[ "$full" == 1 ]]; then echo php-fpm; fi
    if [[ -x /usr/local/mariadb/bin/mariadbd && ( "$full" == 1 || -f /var/lib/onmp/mariadb.owner ) ]]; then
        echo mariadb
    elif [[ -x /usr/local/mysql/bin/mysqld && ( "$full" == 1 || -f /var/lib/onmp/mysql.owner ) ]]; then
        echo mysql
    fi
    local file
    for file in /etc/init.d/php-fpm8.[2-5]; do
        [[ ! -f "$file" ]] || printf '%s\n' "${file##*/}"
    done
}

unit_action()
{
    local unit=$1 action=$2
    case "$action" in
        status) printf '%s: ' "$unit"; systemctl is-active "$unit" ;;
        start|stop|restart|reload) systemctl "$action" "$unit" ;;
        *) echo 'Choose start / stop / restart / reload / status.' >&2; return 2 ;;
    esac
}

case "${1:-help}" in
    help|-h|--help) usage; exit 0 ;;
    --version|version) printf 'ONMP %s (based on %s)\n' "$ONMP_Ver" "$ONMP_Upstream"; exit 0 ;;
esac
ONMP_Require_Root
ONMP_Require_Platform
cd "$ONMP_HOME"
case "$1" in
    install)
        case "${2:-}" in
            redis|mongodb) shift; exec bash "$ONMP_HOME/services.sh" install "$@" ;;
            node|pnpm|composer) shift; exec bash "$ONMP_HOME/runtimes.sh" "$@" ;;
            onmp|nginx|db|mphp) shift; exec bash "$ONMP_HOME/install.sh" "$@" ;;
            *) usage >&2; exit 2 ;;
        esac ;;
    redis|mongodb) component=$1; shift; action=${1:-status}; [[ $# == 0 ]] || shift; exec bash "$ONMP_HOME/services.sh" "$action" "$component" "$@" ;;
    php-extension) shift; exec bash "$ONMP_HOME/php-extensions.sh" "$@" ;;
    php-cli)
        [[ "${2:-}" == enable ]] || { usage >&2; exit 2; }
        shift 2; exec bash "$ONMP_HOME/php-cli.sh" "$@" ;;
    upgrade) shift; exec bash "$ONMP_HOME/upgrade.sh" "$@" ;;
    addons) shift; exec bash "$ONMP_HOME/addons.sh" "$@" ;;
    start|stop|restart|reload|status)
        result=0
        found=0
        while IFS= read -r unit; do
            [[ -n "$unit" ]] || continue
            found=1
            unit_action "$unit" "$1" || result=$?
        done < <(core_units)
        for service in redis mongodb; do
            if [[ -f "/var/lib/onmp/$service.owner" ]]; then
                found=1
                bash "$ONMP_HOME/services.sh" "$1" "$service" || result=$?
            fi
        done
        if [[ "$found" == 0 ]]; then
            echo 'No ONMP services installed. Use onmp install <component>.' >&2
            exit 1
        fi
        exit "$result" ;;
    nginx|mysql|mariadb|php-fpm)
        [[ -s "$ONMP_HOME/.state/stack-command" || -f "/var/lib/onmp/$1.owner" ]] || { echo 'Install this ONMP component first.' >&2; exit 1; }
        [[ $# -le 2 ]] || exit 2
        unit_action "$1" "${2:-status}" ;;
    pureftpd|vhost|database|ftp|ssl|dns|dnsssl|onlyssl|kill)
        if [[ ! -s "$ONMP_HOME/.state/stack-command" ]]; then
            echo 'Install the ONMP web stack first: onmp install onmp' >&2
            exit 1
        fi
        exec bash "$ONMP_HOME/.state/stack-command" "$@" ;;
    *) usage >&2; exit 2 ;;
esac
