
#!/bin/sh
# OpenWrt Hy2 Proxy Installer
# Usage: sh <(curl -fsSL https://f.n1kt.ru/hy2.sh)

set -e

LOGTAG="hy2-installer"
INIT_SCRIPT="/etc/init.d/hy2-proxy"
REMOTE_INIT="http://f.n1kt.ru/hy2-proxy"
CONFIG_SAMPLE="http://f.n1kt.ru/config.yaml.sample"

log() {
    echo "[INSTALL] $1"
    logger -t "$LOGTAG" "$1" 2>/dev/null || true
}

error() {
    echo "[ERROR] $1" >&2
    log "ERROR: $1"
    exit 1
}

check_openwrt() {
    if [ ! -f "/etc/openwrt_release" ] && ! grep -q "OpenWrt" /etc/*release 2>/dev/null; then
        error "This script is for OpenWrt only. Detected: $(cat /etc/*release 2>/dev/null | head -1)"
    fi
    log "✓ OpenWrt detected"
}

check_deps() {
    local missing=""
    
    for pkg in wget nftables; do
        if ! command -v "$pkg" >/dev/null 2>&1; then
            missing="$missing $pkg"
        fi
    done
    
    if [ -n "$missing" ]; then
        log "Installing missing packages:$missing"
        opkg update >/dev/null 2>&1 || true
        for pkg in $missing; do
            opkg install "$pkg" || log "⚠ Failed to install $pkg"
        done
    fi
}

download_init_script() {
    log "Downloading init script..."
    
    for url in "$REMOTE_INIT"; do
        if wget -q -T 30 --no-check-certificate "$url" -O "$INIT_SCRIPT.tmp"; then
            if [ -s "$INIT_SCRIPT.tmp" ]; then
                mv "$INIT_SCRIPT.tmp" "$INIT_SCRIPT"
                chmod +x "$INIT_SCRIPT"
                log "✓ Init script installed: $INIT_SCRIPT"
                return 0
            fi
        fi
    done
    
    rm -f "$INIT_SCRIPT.tmp"
    error "Failed to download init script"
}

setup_config() {
    if [ ! -f "/root/config.yaml" ]; then
        log "Config not found. Downloading sample..."
        if wget -q -T 20 --no-check-certificate "$CONFIG_SAMPLE" -O "/root/config.yaml"; then
            echo "⚠ Sample config saved to /root/config.yaml.sample"
            echo "  Please edit it and save as /root/config.yaml before first start"
        fi
    else
        log "✓ Config found: /root/config.yaml"
    fi
}

enable_service() {
    log "Enabling service..."
    /etc/init.d/hy2-proxy enable || error "Failed to enable service"
    log "✓ Service enabled"
}

finalize() {
    sync
    echo ""
    echo "========================================"
    echo " Installation complete!"
    echo "========================================"
    echo ""
    echo "Next steps:"
    echo "  1. Edit /root/config.yaml with your Hy2 settings"
    echo "  2. Start the service: /etc/init.d/hy2-proxy start"
    echo "  3. Check logs: logread | grep hy2-proxy"
    echo ""
    echo "⚠ IMPORTANT: /tmp is cleared on reboot!"
    echo "   Binaries will be re-downloaded automatically."
    echo "   Ensure your config.yaml is in persistent storage (/root/)"
    echo ""
    echo " Reboot recommended: 'reboot'"
    echo ""
}

main() {
    echo "OpenWrt Hy2 Proxy Installer"
    echo ""
    
    check_openwrt
    check_deps
    download_init_script
    setup_config
    enable_service
    finalize
}

main "$@"
