#!/usr/bin/env bash
# =============================================================================
# lighthouse-setup.sh
#
# One-time setup script for the WMD SED Lighthouse Monitor.
# Run as the wmdsed user (or whichever user owns the WMD SED installation).
#
# Usage:
#   bash lighthouse-setup.sh
#   bash lighthouse-setup.sh --dry-run
#
# Edit the variables below if your installation uses non-standard paths.
#
# NOTE: Windows installations
#   This script is for Linux/macOS only. For Windows, perform setup manually:
#   Step 1 - Install the lighthouse npm package:
#              cd C:\path\to\node-folder
#              npm install --save lighthouse
#   Step 2 - Copy lighthouse-scan.js from tools\lighthouse\ to your Node folder,
#              then run: php tools\lighthouse\lighthouse-gather.php --setup-columns
#
# NOTE ABOUT LINE ENDINGS
#   This script must be saved with Unix LF line endings, not Windows CRLF.
#   If edited on Windows and uploaded with CRLF endings, Bash may fail with
#   errors such as:
#     $'\r': command not found
#     invalid option name: pipefail
#
#   To fix on Linux:
#     dos2unix lighthouse-setup.sh
#
#   Or:
#     sed -i 's/\r$//' lighthouse-setup.sh
# =============================================================================

set -euo pipefail

# ---------------------------------------------------------------------------
# Configuration — edit these if your paths differ from the standard layout
# ---------------------------------------------------------------------------

# Root of your WMD SED installation
WMD_ROOT="/home/wmdsed/sites/wmdsed60"

# The Node.js/npm working folder (where node_modules and scripts live)
# This is typically the folder the WMD SED install script created for Node scripts.
# It is usually symlinked from the WMD log path as "websites".
NODE_DIR="/home/wmdsed/webdata"

# Whether to install a cron job automatically (true/false)
INSTALL_CRON="true"

# Cron schedule — default: every 30 minutes
# Effective per-domain rescan frequency is controlled by LIGHTHOUSE_SKIP_RECENT_DAYS in gather.php (default 30 days)
CRON_SCHEDULE="*/30 * * * *"

# Batch size for the cron job — Lighthouse scans are heavy; keep this low
CRON_BATCH="2"

# Log file for cron output
CRON_LOG="/home/wmdsed/logs/wmdsed/lighthouse-cron.log"

# ---------------------------------------------------------------------------
# Internal — do not edit below this line unless you know what you are doing
# ---------------------------------------------------------------------------

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GATHER_SCRIPT="$WMD_ROOT/tools/lighthouse/lighthouse-gather.php"
SCAN_SCRIPT_SRC="$WMD_ROOT/tools/lighthouse/lighthouse-scan.js"
SCAN_SCRIPT_DST="$NODE_DIR/lighthouse-scan.js"

DRY_RUN=false
if [[ "${1:-}" == "--dry-run" ]]; then
    DRY_RUN=true
fi

# Colours
RED='\033[0;31m'
GRN='\033[0;32m'
YLW='\033[1;33m'
BLD='\033[1m'
RST='\033[0m'

divider() { echo "------------------------------------------------------------------------"; }
info()    { echo -e "${GRN}[INFO]${RST}  $*"; }
warn()    { echo -e "${YLW}[WARN]${RST}  $*"; }
error()   { echo -e "${RED}[ERROR]${RST} $*"; }
step()    { echo -e "\n${BLD}$*${RST}"; divider; }
dry()     { echo -e "  ${YLW}[DRY-RUN]${RST} Would: $*"; }

if $DRY_RUN; then
    echo ""
    warn "DRY-RUN mode — no changes will be made."
    echo ""
fi

echo ""
echo -e "${BLD}WMD SED Lighthouse Monitor — Setup Script${RST}"
divider

# ---------------------------------------------------------------------------
# Step 1: Pre-flight checks
# ---------------------------------------------------------------------------
step "Step 1 — Pre-flight checks"

PREFLIGHT_OK=true

# WMD root
if [[ -d "$WMD_ROOT" ]]; then
    info "WMD root found:          $WMD_ROOT"
else
    error "WMD root not found:      $WMD_ROOT"
    error "Edit WMD_ROOT at the top of this script."
    PREFLIGHT_OK=false
fi

# Node folder
if [[ -d "$NODE_DIR" ]]; then
    info "Node folder found:       $NODE_DIR"
else
    error "Node folder not found:   $NODE_DIR"
    error "Edit NODE_DIR at the top of this script."
    PREFLIGHT_OK=false
fi

# node_modules (Puppeteer must already be installed by WMD SED install script)
if [[ -d "$NODE_DIR/node_modules/puppeteer" ]] || [[ -d "$NODE_DIR/node_modules/puppeteer-core" ]]; then
    info "Puppeteer found:         OK"
else
    warn "Puppeteer not found in $NODE_DIR/node_modules"
    warn "WMD SED normally installs Puppeteer automatically. Check your WMD SED installation."
    PREFLIGHT_OK=false
fi

# Node.js
if command -v node &>/dev/null; then
    NODE_VER=$(node --version)
    info "Node.js version:         $NODE_VER"
else
    error "node command not found. Install Node.js and try again."
    PREFLIGHT_OK=false
fi

# npm
if command -v npm &>/dev/null; then
    NPM_VER=$(npm --version)
    info "npm version:             $NPM_VER"
else
    error "npm command not found."
    PREFLIGHT_OK=false
fi

# PHP
if command -v php &>/dev/null; then
    PHP_VER=$(php --version | head -1)
    info "PHP:                     $PHP_VER"
else
    error "php command not found."
    PREFLIGHT_OK=false
fi

# Source scan script
if [[ -f "$SCAN_SCRIPT_SRC" ]]; then
    info "lighthouse-scan.js:      found at $SCAN_SCRIPT_SRC"
else
    error "lighthouse-scan.js not found at $SCAN_SCRIPT_SRC"
    error "Ensure the Lighthouse Monitor files are installed in $WMD_ROOT/tools/lighthouse/"
    PREFLIGHT_OK=false
fi

# Gather script
if [[ -f "$GATHER_SCRIPT" ]]; then
    info "lighthouse-gather.php:   found"
else
    error "lighthouse-gather.php not found at $GATHER_SCRIPT"
    PREFLIGHT_OK=false
fi

# Node folder writable
if [[ -w "$NODE_DIR" ]]; then
    info "Node folder writable:    yes"
else
    error "Node folder not writable: $NODE_DIR"
    PREFLIGHT_OK=false
fi

if ! $PREFLIGHT_OK; then
    echo ""
    error "Pre-flight checks failed. Fix the issues above and run again."
    exit 1
fi

# ---------------------------------------------------------------------------
# Step 2: Install lighthouse npm package
# ---------------------------------------------------------------------------
step "Step 2 — Install lighthouse npm package"

if [[ -d "$NODE_DIR/node_modules/lighthouse" ]]; then
    INSTALLED_VER=$(node -e "console.log(require('$NODE_DIR/node_modules/lighthouse/package.json').version)" 2>/dev/null || echo "unknown")
    info "lighthouse already installed (v$INSTALLED_VER) — skipping"
else
    if $DRY_RUN; then
        dry "npm install --prefix $NODE_DIR lighthouse"
    else
        info "Installing lighthouse into $NODE_DIR ..."
        cd "$NODE_DIR"
        npm install --save lighthouse 2>&1 | sed 's/^/    /'
        if [[ -d "$NODE_DIR/node_modules/lighthouse" ]]; then
            NEW_VER=$(node -e "console.log(require('$NODE_DIR/node_modules/lighthouse/package.json').version)" 2>/dev/null || echo "unknown")
            info "lighthouse installed successfully (v$NEW_VER)"
        else
            error "lighthouse installation appears to have failed. Check npm output above."
            exit 1
        fi
    fi
fi

# ---------------------------------------------------------------------------
# Step 3: Copy lighthouse-scan.js to Node folder
# ---------------------------------------------------------------------------
step "Step 3 — Copy lighthouse-scan.js to Node folder"

if [[ -f "$SCAN_SCRIPT_DST" ]]; then
    # Compare checksums to see if update needed
    SRC_SUM=$(md5sum "$SCAN_SCRIPT_SRC" | awk '{print $1}')
    DST_SUM=$(md5sum "$SCAN_SCRIPT_DST" | awk '{print $1}')
    if [[ "$SRC_SUM" == "$DST_SUM" ]]; then
        info "lighthouse-scan.js already up to date — skipping"
    else
        if $DRY_RUN; then
            dry "cp $SCAN_SCRIPT_SRC $SCAN_SCRIPT_DST  (updated version)"
        else
            cp "$SCAN_SCRIPT_SRC" "$SCAN_SCRIPT_DST"
            info "lighthouse-scan.js updated in $NODE_DIR"
        fi
    fi
else
    if $DRY_RUN; then
        dry "cp $SCAN_SCRIPT_SRC $SCAN_SCRIPT_DST"
    else
        cp "$SCAN_SCRIPT_SRC" "$SCAN_SCRIPT_DST"
        info "lighthouse-scan.js copied to $NODE_DIR"
    fi
fi

# ---------------------------------------------------------------------------
# Step 4: Create custom WMD columns
# ---------------------------------------------------------------------------
step "Step 4 — Create custom WMD columns"

if $DRY_RUN; then
    dry "php $GATHER_SCRIPT --setup-columns"
else
    info "Running --setup-columns ..."
    php "$GATHER_SCRIPT" --setup-columns
fi

# ---------------------------------------------------------------------------
# Step 5: Install cron job
# ---------------------------------------------------------------------------
step "Step 5 — Cron job"

CRON_CMD="${CRON_SCHEDULE} php ${GATHER_SCRIPT} --batch=${CRON_BATCH} >> ${CRON_LOG} 2>&1"

if [[ "$INSTALL_CRON" != "true" ]]; then
    warn "INSTALL_CRON is not set to 'true'. Skipping cron installation."
    echo ""
    info "To add the cron job manually, run: crontab -e"
    info "And add the following line:"
    echo "    $CRON_CMD"
else
    # Check if already installed
    if crontab -l 2>/dev/null | grep -qF "lighthouse-gather.php"; then
        info "Cron job already present — skipping"
        info "Existing entry:"
        crontab -l 2>/dev/null | grep "lighthouse-gather.php" | sed 's/^/    /'
    else
        if $DRY_RUN; then
            dry "Add to crontab: $CRON_CMD"
        else
            # Append to existing crontab
            (crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab -
            info "Cron job installed:"
            info "    $CRON_CMD"
        fi
    fi
fi

# ---------------------------------------------------------------------------
# Step 6: Quick smoke test
# ---------------------------------------------------------------------------
step "Step 6 — Smoke test"

if $DRY_RUN; then
    dry "php $GATHER_SCRIPT --dry-run"
else
    info "Running --dry-run to verify domain selection ..."
    echo ""
    php "$GATHER_SCRIPT" --dry-run || true
fi

# ---------------------------------------------------------------------------
# Done
# ---------------------------------------------------------------------------
echo ""
divider
echo -e "${BLD}Setup complete.${RST}"
divider
echo ""
echo "  Next steps:"
echo ""
echo "  1. In WMD SED, create a category named 'Lighthouse Domains'"
echo "     and assign the domains you want to scan to it."
echo ""
echo "  2. Run a test scan:"
echo "     php $GATHER_SCRIPT --domain=example.com"
echo ""
echo "  3. Run a full scan:"
echo "     php $GATHER_SCRIPT"
echo ""
echo "  4. Access the browser report via the WMD SED Custom Tools page,"
echo "     or directly at:"
echo "     https://yourserver/tools/lighthouse/lighthouse-report.php"
echo ""
if $DRY_RUN; then
    warn "This was a dry run. Run without --dry-run to make changes."
    echo ""
fi