#!/usr/bin/env bash
# install.sh — Darkdump installer for Linux and macOS
# Creates `darkdump` (web) and `darkdump-cli` launcher commands.

set -e

BOLD="\033[1m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
RESET="\033[0m"

info()    { echo -e "${BOLD}${GREEN}[+]${RESET} $*"; }
warn()    { echo -e "${BOLD}${YELLOW}[!]${RESET} $*"; }
error()   { echo -e "${BOLD}${RED}[x]${RESET} $*" >&2; }
die()     { error "$*"; exit 1; }

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

OS="$(uname -s)"
case "$OS" in
    Linux*)  PLATFORM="linux" ;;
    Darwin*) PLATFORM="mac"   ;;
    *)       die "Unsupported platform: $OS. This installer supports Linux and macOS." ;;
esac
info "Detected platform: $OS"

PYTHON=""
for candidate in python3 python; do
    if command -v "$candidate" &>/dev/null; then
        ver="$("$candidate" -c 'import sys; print(sys.version_info.major)')"
        if [ "$ver" = "3" ]; then
            PYTHON="$(command -v "$candidate")"
            break
        fi
    fi
done
[ -n "$PYTHON" ] || die "Python 3 not found. Please install Python 3 and re-run this script."
info "Using Python: $PYTHON ($($PYTHON --version))"

PIP=""
for candidate in pip3 pip; do
    if command -v "$candidate" &>/dev/null; then
        ver="$("$candidate" --version 2>&1)"
        if echo "$ver" | grep -q "python 3\|python3"; then
            PIP="$(command -v "$candidate")"
            break
        fi
    fi
done
if [ -z "$PIP" ]; then
    # Fall back to `python -m pip`
    if "$PYTHON" -m pip --version &>/dev/null 2>&1; then
        PIP="$PYTHON -m pip"
    else
        die "pip for Python 3 not found. Install pip and re-run."
    fi
fi
info "Using pip: $PIP"

info "Installing darkdump dependencies..."
$PIP install --quiet -r "$SCRIPT_DIR/requirements.txt"

if [ -f "$SCRIPT_DIR/darkdump-web/requirements.txt" ]; then
    info "Installing darkdump-web dependencies..."
    $PIP install --quiet -r "$SCRIPT_DIR/darkdump-web/requirements.txt"
fi

SYSTEM_BIN="/usr/local/bin"
USER_BIN="$HOME/.local/bin"

if [ -w "$SYSTEM_BIN" ] || sudo -n true 2>/dev/null; then
    INSTALL_DIR="$SYSTEM_BIN"
    USE_SUDO=true
else
    warn "No write access to $SYSTEM_BIN — installing to $USER_BIN (user-local)."
    INSTALL_DIR="$USER_BIN"
    USE_SUDO=false
    mkdir -p "$INSTALL_DIR"
fi

DARKDUMP_LAUNCHER="$INSTALL_DIR/darkdump"

DARKDUMP_SCRIPT="#!/usr/bin/env bash
# Darkdump web launcher — generated by install.sh
exec \"$PYTHON\" \"$SCRIPT_DIR/darkdump-web/app.py\" \"\$@\"
"

if [ "$USE_SUDO" = true ] && [ ! -w "$SYSTEM_BIN" ]; then
    echo "$DARKDUMP_SCRIPT" | sudo tee "$DARKDUMP_LAUNCHER" > /dev/null
    sudo chmod +x "$DARKDUMP_LAUNCHER"
else
    printf '%s' "$DARKDUMP_SCRIPT" > "$DARKDUMP_LAUNCHER"
    chmod +x "$DARKDUMP_LAUNCHER"
fi
info "Installed: $DARKDUMP_LAUNCHER"

DARKDUMP_CLI_LAUNCHER="$INSTALL_DIR/darkdump-cli"

DARKDUMP_CLI_SCRIPT="#!/usr/bin/env bash
# Darkdump CLI launcher — generated by install.sh
exec \"$PYTHON\" \"$SCRIPT_DIR/darkdump.py\" \"\$@\"
"

if [ "$USE_SUDO" = true ] && [ ! -w "$SYSTEM_BIN" ]; then
    echo "$DARKDUMP_CLI_SCRIPT" | sudo tee "$DARKDUMP_CLI_LAUNCHER" > /dev/null
    sudo chmod +x "$DARKDUMP_CLI_LAUNCHER"
else
    printf '%s' "$DARKDUMP_CLI_SCRIPT" > "$DARKDUMP_CLI_LAUNCHER"
    chmod +x "$DARKDUMP_CLI_LAUNCHER"
fi
info "Installed: $DARKDUMP_CLI_LAUNCHER"

if [ "$INSTALL_DIR" = "$USER_BIN" ]; then
    if ! echo "$PATH" | grep -q "$USER_BIN"; then
        warn "$USER_BIN is not in your PATH."
        SHELL_RC=""
        case "$SHELL" in
            */zsh)  SHELL_RC="$HOME/.zshrc"  ;;
            */bash) SHELL_RC="$HOME/.bashrc" ;;
        esac
        if [ -n "$SHELL_RC" ]; then
            warn "Add it by running:"
            echo ""
            echo "    echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $SHELL_RC && source $SHELL_RC"
            echo ""
        fi
    fi
fi

echo ""
info "Installation complete."
echo -e "  ${BOLD}darkdump${RESET}      — start the web interface (then open http://127.0.0.1:5000)"
echo -e "  ${BOLD}darkdump-cli${RESET}  — run the CLI tool"
echo ""
