#!/bin/sh

set -eu


PROJECT_DIR="/project"
COMPOSE_FILE="$PROJECT_DIR/compose.yaml"


# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------

require_host_project_path() {
    if [ -z "${HOST_PROJECT_PATH:-}" ]; then
        echo "ERROR: HOST_PROJECT_PATH is not set." >&2
        echo "The SSH container environment should initialize it." >&2
        exit 1
    fi
}


require_ssh_role() {
    if [ -z "${SSH_ROLE:-}" ]; then
        echo "ERROR: SSH_ROLE is not set." >&2
        exit 1
    fi
}


show_status() {
    docker ps \
        --filter "name=gpu-node-" \
        --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
}


# ------------------------------------------------------------
# Start llama.cpp
# ------------------------------------------------------------

start_llama() {
    MODEL="${1:-model.gguf}"

    shift || true

    cd "$PROJECT_DIR"

    require_host_project_path

    EXTRA_ARGS="$*"

    echo "Host project: $HOST_PROJECT_PATH"
    echo "Starting llama..."
    echo "Model: $MODEL"
    echo "Models: $HOST_PROJECT_PATH/gpu/llama_models"

    if [ -n "$EXTRA_ARGS" ]; then
        echo "Extra args: $EXTRA_ARGS"
    fi

    HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \
    PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \
    LLAMA_MODELS_HOST="$HOST_PROJECT_PATH/gpu/llama_models" \
    LLAMA_MODEL="$MODEL" \
    LLAMA_EXTRA_ARGS="$EXTRA_ARGS" \
        docker compose \
            -f "$COMPOSE_FILE" \
            --profile llama \
            up -d \
            --force-recreate \
            llama

    echo

    docker ps \
        --filter "name=gpu-node-llama" \
        --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
}


# ------------------------------------------------------------
# Start ComfyUI
# ------------------------------------------------------------

start_comfy() {
    cd "$PROJECT_DIR"

    require_host_project_path

    EXTRA_ARGS="$*"

    echo "Host project: $HOST_PROJECT_PATH"
    echo "Starting ComfyUI..."
    echo "Base directory: $HOST_PROJECT_PATH/gpu/comfy"
    echo "Run directory:  $HOST_PROJECT_PATH/comfyui/run"

    if [ -n "$EXTRA_ARGS" ]; then
        echo "Extra args: $EXTRA_ARGS"
    fi

    HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \
    PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \
    COMFY_BASE_HOST="$HOST_PROJECT_PATH/gpu/comfy" \
    COMFY_RUN_HOST="$HOST_PROJECT_PATH/comfyui/run" \
    COMFY_EXTRA_ARGS="$EXTRA_ARGS" \
        docker compose \
            -f "$COMPOSE_FILE" \
            --profile comfy \
            up -d \
            --force-recreate \
            comfyui

    echo

    docker ps \
        --filter "name=gpu-node-comfyui" \
        --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
}


# ------------------------------------------------------------
# Update SSH container
# ------------------------------------------------------------

update_ssh() {
    cd "$PROJECT_DIR"

    require_host_project_path
    require_ssh_role

    SSH_KEYS_HOST="$HOST_PROJECT_PATH/ssh/authorized_keys"


    case "$SSH_ROLE" in

        main)
            TARGET_SERVICE="ssh-companion"
            ;;

        companion)
            TARGET_SERVICE="ssh"
            ;;

        *)
            echo "ERROR: unknown SSH role: $SSH_ROLE" >&2
            exit 1
            ;;

    esac


    echo "Current SSH role: $SSH_ROLE"
    echo "Host project: $HOST_PROJECT_PATH"
    echo "SSH keys: $SSH_KEYS_HOST"
    echo "Updating: $TARGET_SERVICE"


    HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \
    PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \
    SSH_KEYS_HOST="$SSH_KEYS_HOST" \
        docker compose \
            -f "$COMPOSE_FILE" \
            up -d \
            --build \
            --force-recreate \
            "$TARGET_SERVICE"
}


# ------------------------------------------------------------
# Stop service
# ------------------------------------------------------------

stop_service() {
    SERVICE="$1"

    case "$SERVICE" in

        llama)
            docker stop gpu-node-llama 2>/dev/null || true
            ;;

        comfy)
            docker stop gpu-node-comfyui 2>/dev/null || true
            ;;

        tailscale)
            docker stop gpu-node-tailscale 2>/dev/null || true
            ;;

        ssh)
            docker stop gpu-node-ssh-main 2>/dev/null || true
            ;;

        ssh-main)
            docker stop gpu-node-ssh-main 2>/dev/null || true
            ;;

        ssh-companion)
            docker stop gpu-node-ssh-companion 2>/dev/null || true
            ;;

        webdav)
            docker stop gpu-node-webdav 2>/dev/null || true
            ;;

        *)
            echo "Unknown service: $SERVICE"
            exit 1
            ;;

    esac
}


# ------------------------------------------------------------
# Restart service
# ------------------------------------------------------------

restart_service() {
    SERVICE="$1"

    case "$SERVICE" in

        llama)
            docker restart gpu-node-llama
            ;;

        comfy)
            docker restart gpu-node-comfyui
            ;;

        tailscale)
            docker restart gpu-node-tailscale
            ;;

        ssh)
            docker restart gpu-node-ssh-main
            ;;

        ssh-main)
            docker restart gpu-node-ssh-main
            ;;

        ssh-companion)
            docker restart gpu-node-ssh-companion
            ;;

        webdav)
            docker restart gpu-node-webdav
            ;;

        *)
            echo "Unknown service: $SERVICE"
            exit 1
            ;;

    esac
}


# ------------------------------------------------------------
# Logs
# ------------------------------------------------------------

logs_service() {
    SERVICE="$1"

    case "$SERVICE" in

        llama)
            docker logs -f gpu-node-llama
            ;;

        comfy)
            docker logs -f gpu-node-comfyui
            ;;

        tailscale)
            docker logs -f gpu-node-tailscale
            ;;

        ssh)
            docker logs -f gpu-node-ssh-main
            ;;

        ssh-main)
            docker logs -f gpu-node-ssh-main
            ;;

        ssh-companion)
            docker logs -f gpu-node-ssh-companion
            ;;

        webdav)
            docker logs -f gpu-node-webdav
            ;;

        *)
            echo "Unknown service: $SERVICE"
            exit 1
            ;;

    esac
}


# ------------------------------------------------------------
# CLI
# ------------------------------------------------------------

case "${1:-}" in

    status)
        show_status
        ;;


    start)

        case "${2:-}" in

            llama)
                shift 2
                start_llama "$@"
                ;;

            comfy)
                shift 2
                start_comfy "$@"
                ;;

            *)
                echo "Usage:"
                echo "  gpuctl start llama [model.gguf] [extra args...]"
                echo "  gpuctl start comfy [extra args...]"
                exit 1
                ;;

        esac
        ;;


    update)

        if [ -n "${2:-}" ]; then
            echo "Usage:"
            echo "  gpuctl update"
            exit 1
        fi

        update_ssh
        ;;


    stop)

        if [ -z "${2:-}" ]; then
            echo "Usage:"
            echo "  gpuctl stop <service>"
            exit 1
        fi

        stop_service "$2"
        ;;


    restart)

        if [ -z "${2:-}" ]; then
            echo "Usage:"
            echo "  gpuctl restart <service>"
            exit 1
        fi

        restart_service "$2"
        ;;


    logs)

        if [ -z "${2:-}" ]; then
            echo "Usage:"
            echo "  gpuctl logs <service>"
            exit 1
        fi

        logs_service "$2"
        ;;


    *)

        echo "Usage:"
        echo
        echo "  gpuctl status"
        echo "  gpuctl update"
        echo "  gpuctl start llama [model.gguf] [extra args...]"
        echo "  gpuctl start comfy [extra args...]"
        echo "  gpuctl stop <service>"
        echo "  gpuctl restart <service>"
        echo "  gpuctl logs <service>"
        exit 1
        ;;

esac