#!/bin/bash

# FL42 Compilation Assistant
# Author: Your Name

# Check for required dependencies
check_dependencies() {
    if ! command -v dialog &> /dev/null; then
        echo "dialog required but not found. Please install:"
        echo "  Debian/Ubuntu: sudo apt install dialog"
        echo "  Red Hat/Fedora: sudo dnf install dialog"
        exit 1
    fi
}

# Main compilation function
compile_project() {
    local target=$1
    local output_name=$(grep '\\ftype\\' main.fl42 | cut -d"'" -f2 | sed 's/\.f42$//')
    
    dialog --infobox "Compiling for $target..." 3 40
    sleep 2
    
    # Simulated compilation command (replace with actual FL42 compiler command)
    if fl42c --target $target -o "${output_name:-output}" main.fl42; then
        dialog --msgbox "Compilation successful!\nOutput: ${output_name:-output}" 7 40
    else
        dialog --msgbox "Compilation failed!\nCheck source code for errors." 7 40
        exit 1
    fi
}

# Server configuration wizard
server_wizard() {
    local fcom_config=$(grep '\\fcom\\' main.fl42 | sed "s/\\\\fcom\\//")
    
    dialog --yesno "Found FCOM configuration:\n\n$fcom_config\n\nConfigure server?" 12 50
    [ $? -eq 0 ] && configure_server
}

# Server configuration function
configure_server() {
    dialog --form "Server Configuration" 15 50 5 \
        "Server Address:" 1 1 "LOCALHOST" 1 20 30 0 \
        "DNS Suffix:" 2 1 "example.com" 2 20 30 0 \
        "Subnet Mask:" 3 1 "255.255.255.0" 3 20 30 0 \
        "IPv6 Address:" 4 1 "::1" 4 20 30 0 > /tmp/server.conf
    
    # Process configuration
    local server_config=$(paste -sd ' ' /tmp/server.conf)
    sed -i "/\\\\fcom\\/c\\\\fcom\\ '$server_config'" main.fl42
    
    dialog --msgbox "Server configuration updated!" 5 40
}

# Main menu
main_menu() {
    while true; do
        choice=$(dialog --menu "FL42 Project Manager" 15 50 5 \
            1 "Compile Project" \
            2 "Edit Metadata" \
            3 "Server Configuration" \
            4 "View Documentation" \
            0 "Exit" --stdout)

        case $choice in
            1)  target_selection ;;
            2)  edit_metadata ;;
            3)  server_wizard ;;
            4)  show_documentation ;;
            0)  exit 0 ;;
            *)  exit 1 ;;
        esac
    done
}

# Target selection dialog
target_selection() {
    choice=$(dialog --menu "Select target platform:" 10 30 2 \
        1 "Linux" \
        2 "Windows" --stdout)
    
    case $choice in
        1) compile_project "linux" ;;
        2) compile_project "windows" ;;
        *) dialog --msgbox "Invalid selection" 5 30 ;;
    esac
}

# Metadata editing function
edit_metadata() {
    local author=$(grep '\\author\\' main.fl42 | cut -d"'" -f2)
    local desc=$(grep '\\desc\\' main.fl42 | cut -d"'" -f2)
    
    dialog --form "Edit Metadata" 12 50 3 \
        "Author:" 1 1 "$author" 1 20 30 0 \
        "Description:" 2 1 "$desc" 2 20 30 0 > /tmp/metadata.conf
    
    # Update metadata in source file
    sed -i "/\\\\author\\/c\\\\author\\ '$(sed -n 1p /tmp/metadata.conf)'" main.fl42
    sed -i "/\\\\desc\\/c\\\\desc\\ '$(sed -n 2p /tmp/metadata.conf)'" main.fl42
    
    dialog --msgbox "Metadata updated successfully!" 5 40
}

# Documentation viewer
show_documentation() {
    dialog --textbox docs/fl42_help.txt 20 80
}

# Initialize script
check_dependencies
[ ! -f "main.fl42" ] && dialog --msgbox "Error: main.fl42 not found!" 5 40 && exit 1
main_menu