#!/bin/bash

set -e

COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_CYAN='\033[0;36m'
COLOR_YELLOW='\033[1;33m'
COLOR_WHITE='\033[0;37m'
COLOR_RESET='\033[0m'

log_info() {
    echo -e "${COLOR_CYAN}$*${COLOR_RESET}"
}

log_error() {
    echo -e "${COLOR_RED}ERROR: $*${COLOR_RESET}" >&2
}

log_success() {
    echo -e "${COLOR_GREEN}$*${COLOR_RESET}"
}

DIRECTORY="."
VMAF="95.0"
PRESET="6"
WORKERS="1"
SAMPLES="4"
HWACCEL=""
USE_HW_WORKER=""
PLEX_URL=""
PLEX_TOKEN=""
LOG_DIR="/opt/Optmiser/logs"


# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        --directory)
            DIRECTORY="$2"
            shift 2
            ;;
        --vmaf)
            VMAF="$2"
            shift 2
            ;;
        --preset)
            PRESET="$2"
            shift 2
            ;;
        --workers)
            WORKERS="$2"
            shift 2
            ;;
        --samples)
            SAMPLES="$2"
            shift 2
            ;;
        --hwaccel)
            HWACCEL="$2"
            shift 2
            ;;
        --use-hardware-worker)
            USE_HARDWARE_WORKER="true"
            shift
            ;;
        --plex-url)
            PLEX_URL="$2"
            shift 2
            ;;
        --plex-token)
            PLEX_TOKEN="$2"
            shift 2
            ;;
        --log-dir)
            LOG_DIR="$2"
            shift 2
            ;;
        *)
            DIRECTORY="$1"
            shift
            ;;
    esac
done

# Check if python3 is available
if ! command -v python3 &> /dev/null; then
    if ! command -v python &> /dev/null; then
        log_error "Python 3 not found. Please install Python 3."
        exit 1
    else
        PYTHON_CMD="python"
    fi
else
    PYTHON_CMD="python3"
fi

# Check if optimize_library.py exists
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_PATH="$SCRIPT_DIR/optimize_library.py"

if [[ ! -f "$SCRIPT_PATH" ]]; then
    log_error "optimize_library.py not found in: $SCRIPT_DIR"
    exit 1
fi

# Build command arguments
ARGS=(
    "$PYTHON_CMD" "$SCRIPT_PATH"
    "$DIRECTORY"
    --vmaf "$VMAF"
    --preset "$PRESET"
    --workers "$WORKERS"
    --samples "$SAMPLES"
    --log-dir "$LOG_DIR"
)

if [[ -n "$THOROUGH" ]]; then
    ARGS+=(--thorough)
fi

if [[ -n "$HWACCEL" ]]; then
    ARGS+=(--hwaccel "$HWACCEL")
fi

# Print configuration
log_info "========================================"
log_info "VMAF Library Optimiser (Linux/Server)"
log_info "========================================"
echo ""
log_info "Directory: $DIRECTORY"
log_info "Target VMAF: $VMAF"
log_info "Preset: $PRESET"
log_info "Workers: $WORKERS"
log_info "Samples: $SAMPLES"
log_info "Encoder: $ENCODER"
if [[ -n "$THOROUGH" ]]; then
    log_info "Thorough: Yes"
fi
if [[ -n "$HWACCEL" ]]; then
    log_info "HW Accel: $HWACCEL"
fi
echo ""
log_info "Running optimize_library.py..."
echo ""

# Run the optimisation
"${ARGS[@]}"
EXIT_CODE=$?

# Handle exit code
if [ $EXIT_CODE -eq 0 ]; then
    log_success "SUCCESS: Library optimisation completed"
else
    log_error "optimize_library.py exited with code $EXIT_CODE"
fi

exit $EXIT_CODE
