#!/usr/bin/env bash
# Hytale Launcher wrapper script for Gentoo
# Based on AUR package by SCDevel

# Copyright 2026 - Community maintained
# This script is an unofficial community tool and is not affiliated with,
# endorsed by, or owned by Hypixel Studios Canada.

set -e

PKG_NAME="hytale-launcher-bin"
INSTALL_DIR="${HOME}/.local/share/hytale-launcher/bin"
OPT_DIR="/opt/${PKG_NAME}"

# Color output helpers
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

warn() {
    echo -e "${YELLOW}[WARNING]${NC} $1" >&2
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

# Check system clock synchronization
check_time_sync() {
    if command -v timedatectl &> /dev/null; then
        local sync_status
        sync_status=$(timedatectl show -p NTPSynchronized --value 2>/dev/null || echo "unknown")
        if [[ "$sync_status" != "yes" ]]; then
            warn "System clock may not be synchronized (NTPSynchronized=$sync_status)"
            warn "This may cause authentication issues with Hytale servers."
            warn "Consider enabling NTP: timedatectl set-ntp true"
        fi
    fi
}

# Check IPv6 status
check_ipv6() {
    if [[ -f /proc/sys/net/ipv6/conf/all/disable_ipv6 ]]; then
        local ipv6_disabled
        ipv6_disabled=$(cat /proc/sys/net/ipv6/conf/all/disable_ipv6)
        if [[ "$ipv6_disabled" == "1" ]]; then
            warn "IPv6 is disabled on this system."
            warn "Hytale authentication requires IPv6 to be enabled."
            warn "Enable IPv6: sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0"
        fi
    fi

    # Check for global IPv6 address
    if command -v ip &> /dev/null; then
        local has_global_ipv6
        has_global_ipv6=$(ip -6 addr show scope global 2>/dev/null | grep -c "inet6" || echo "0")
        if [[ "$has_global_ipv6" == "0" ]]; then
            warn "No global IPv6 address detected."
            warn "This may cause authentication issues if your ISP doesn't provide IPv6."
        fi
    fi
}

# Environment variables for compatibility
export __NV_DISABLE_EXPLICIT_SYNC=1
export WEBKIT_DISABLE_DMABUF_RENDERER=1

# Nautilus crash workaround - prepend our fix to PATH
export PATH="${OPT_DIR}/nautilus-fix:${PATH}"

# Desktop startup notification
if [[ -n "$DESKTOP_STARTUP_ID" ]]; then
    export DESKTOP_STARTUP_ID="com.hypixel.HytaleLauncher-$DESKTOP_STARTUP_ID"
fi

# Parse arguments
CUSTOM_DIR=""
PASS_ARGS=()

while [[ $# -gt 0 ]]; do
    case $1 in
        -d|--directory)
            CUSTOM_DIR="$2"
            shift 2
            ;;
        *)
            PASS_ARGS+=("$1")
            shift
            ;;
    esac
done

# Use custom directory if specified
if [[ -n "$CUSTOM_DIR" ]]; then
    INSTALL_DIR="$CUSTOM_DIR"
fi

# Perform system checks
check_time_sync
check_ipv6

# First run: copy launcher to user directory
if [[ ! -f "${INSTALL_DIR}/hytale-launcher" ]]; then
    echo "First run detected. Installing launcher to ${INSTALL_DIR}..."
    mkdir -p "${INSTALL_DIR}"
    cp "${OPT_DIR}/hytale-launcher" "${INSTALL_DIR}/hytale-launcher"
    chmod +x "${INSTALL_DIR}/hytale-launcher"
    echo "Installation complete."
fi

# Execute the launcher
cd "${INSTALL_DIR}"
exec "${INSTALL_DIR}/hytale-launcher" "${PASS_ARGS[@]}"
