#!/bin/bash

# Ready or Not - Nexus Mods Installer Script
# Extracts and installs .pak files from downloaded zip archives

set -e

# Directories
DOWNLOADS_DIR="/home/cal/Downloads"
MODS_DIR="/mnt/NV2/SteamLibrary/steamapps/common/Ready Or Not/ReadyOrNot/Content/Paks/Mods"
TEMP_DIR="/tmp/ron-mod-install"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

install_mod_zips() {
    log_info "=== Ready or Not Mod Installer ==="
    echo
    
    # Create temp directory
    mkdir -p "$TEMP_DIR"
    
    # Find recently downloaded zip files (last 10 minutes)
    log_info "Looking for recently downloaded mod zip files..."
    local zip_files=($(find "$DOWNLOADS_DIR" -name "*.zip" -mmin -10 2>/dev/null))
    
    if [ ${#zip_files[@]} -eq 0 ]; then
        log_warning "No recent zip files found in $DOWNLOADS_DIR"
        log_info "Looking for all zip files in Downloads..."
        zip_files=($(find "$DOWNLOADS_DIR" -name "*.zip" 2>/dev/null))
    fi
    
    if [ ${#zip_files[@]} -eq 0 ]; then
        log_error "No zip files found in Downloads directory"
        return 1
    fi
    
    log_info "Found ${#zip_files[@]} zip file(s)"
    echo
    
    local installed_count=0
    
    for zip_file in "${zip_files[@]}"; do
        local filename=$(basename "$zip_file")
        log_info "Processing: $filename"
        
        # Create temp extraction directory
        local extract_dir="$TEMP_DIR/$(basename "$zip_file" .zip)"
        mkdir -p "$extract_dir"
        
        # Extract zip file
        if unzip -q "$zip_file" -d "$extract_dir"; then
            log_success "Extracted: $filename"
            
            # Find .pak files in the extraction
            local pak_files=($(find "$extract_dir" -name "*.pak" -type f))
            
            if [ ${#pak_files[@]} -gt 0 ]; then
                for pak_file in "${pak_files[@]}"; do
                    local pak_name=$(basename "$pak_file")
                    local dest_file="$MODS_DIR/$pak_name"
                    
                    # Copy pak file to mods directory
                    if cp "$pak_file" "$dest_file"; then
                        log_success "Installed: $pak_name"
                        ((installed_count++))
                    else
                        log_error "Failed to install: $pak_name"
                    fi
                done
            else
                log_warning "No .pak files found in: $filename"
                log_info "Contents:"
                find "$extract_dir" -type f | head -10
            fi
        else
            log_error "Failed to extract: $filename"
        fi
        
        echo
    done
    
    # Cleanup
    rm -rf "$TEMP_DIR"
    
    log_success "Installation complete!"
    log_info "Installed $installed_count mod file(s)"
    echo
    
    # Show installed mods
    log_info "Currently installed mods:"
    if [ -d "$MODS_DIR" ] && [ "$(ls -A "$MODS_DIR" 2>/dev/null)" ]; then
        ls -1 "$MODS_DIR"/ | grep "\.pak$" || log_info "No .pak files in Mods directory"
    else
        log_info "Mods directory is empty"
    fi
    
    echo
    log_info "=== Next Steps ==="
    echo "1. Launch Ready or Not through Steam Tinker Launch"
    echo "2. Mods should be automatically loaded"
    echo "3. Check in-game settings or mod menu if available"
    echo
    log_warning "Important: Only install mods you trust from reputable sources"
    log_warning "Some mods may require specific load orders or compatibility patches"
}

# Show help
show_help() {
    echo "Ready or Not Mod Installer"
    echo
    echo "Usage:"
    echo "  $0                    - Install all recent zip files from Downloads"
    echo "  $0 --help           - Show this help"
    echo "  $0 --list           - List currently installed mods"
    echo
}

# List installed mods
list_mods() {
    log_info "Currently installed Ready or Not mods:"
    echo
    
    if [ -d "$MODS_DIR" ] && [ "$(ls -A "$MODS_DIR" 2>/dev/null)" ]; then
        ls -la "$MODS_DIR"/ | grep "\.pak$" || log_info "No .pak files found"
    else
        log_info "No mods installed"
    fi
}

# Main execution
case "${1:-}" in
    --help|-h)
        show_help
        ;;
    --list|-l)
        list_mods
        ;;
    *)
        install_mod_zips
        ;;
esac