#!/bin/bash

# Parse command line arguments
BUILD_DEBIAN=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --debian)
            BUILD_DEBIAN=true
            shift
            ;;
        *)
            echo "Unknown option: $1"
            echo "Usage: $0 [--debian]"
            exit 1
            ;;
    esac
done

# Check if virtual environment exists, create if not
if [ ! -d "venv" ]; then
    echo "Creating virtual environment..."
    python3 -m venv venv
fi

# Activate virtual environment
. venv/bin/activate

# Install requirements
echo "Installing requirements..."
pip3 install -r requirements.txt

# Install pyinstaller if not already installed
pip3 install pyinstaller

# Create dist directory if not exists
mkdir -p dist

# Generate SSL certificates if they don't exist
if [ ! -f "cert.pem" ] || [ ! -f "key.pem" ]; then
    echo "Generating SSL certificates..."
    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
fi

# Generate logos and icons from image.jpg if it exists
if [ -f "image.jpg" ] && command -v convert &> /dev/null; then
    echo "Generating logos and icons..."
    mkdir -p logos

    # Generate various logo sizes
    convert image.jpg -resize 512x512 logos/logo-512.png
    convert image.jpg -resize 256x256 logos/logo-256.png
    convert image.jpg -resize 128x128 logos/logo-128.png
    convert image.jpg -resize 64x64 logos/logo-64.png

    # Generate icon sizes
    for size in 16 32 48 64 128 256; do
        convert image.jpg -resize ${size}x${size} logos/icon-${size}.png
    done

    # Generate additional assets
    convert image.jpg -resize 800x200 logos/banner-800x200.png
    convert image.jpg -quality 100 logos/logo-high-quality.png
    convert image.jpg -resize 32x32 logos/favicon.ico

    echo "Logo generation complete."
fi

# Build Python binaries
echo "Building Python binaries..."

# Build wssshd (server) binary with certificates and web assets
pyinstaller --onefile --distpath dist --add-data "cert.pem:." --add-data "key.pem:." --add-data "templates:templates" --add-data "static:static" --runtime-tmpdir /tmp --clean wssshd.py

# Build wssshc (client) binary
pyinstaller --onefile --distpath dist --runtime-tmpdir /tmp --clean wssshc.py

# Build wsssh binary
pyinstaller --onefile --distpath dist --runtime-tmpdir /tmp --clean wsssh.py

# Build wsscp binary
pyinstaller --onefile --distpath dist --runtime-tmpdir /tmp --clean wsscp.py

# Build C version if wssshtools directory exists
if [ -d "wssshtools" ]; then
    echo "Building C version..."
    cd wssshtools
    if [ -f "configure.sh" ]; then
        ./configure.sh
        make
        cd ..
    else
        echo "Warning: configure.sh not found in wssshtools/"
        cd ..
    fi
fi

# Build Debian package if requested
if [ "$BUILD_DEBIAN" = true ]; then
    echo "Checking Debian build dependencies..."

    # Check for required Debian build tools
    missing_deps=""
    for dep in debhelper gcc make pkg-config libssl-dev; do
        if ! dpkg -l | grep -q "^ii  $dep"; then
            missing_deps="$missing_deps $dep"
        fi
    done

    if [ -n "$missing_deps" ]; then
        echo "Error: Missing Debian build dependencies:$missing_deps"
        echo "Please install them with: sudo apt-get install$missing_deps"
        echo "Or run: sudo apt-get install debhelper gcc make pkg-config libssl-dev"
        exit 1
    fi

    echo "All Debian build dependencies found."
    echo "Building Debian package..."

    if [ -d "wssshtools" ] && [ -d "wssshtools/debian" ]; then
        cd wssshtools
        dpkg-buildpackage -us -uc
        cd ..
        # Move deb file to dist
        mv ../wsssh-tools*.deb dist/ 2>/dev/null || true
    else
        echo "Warning: Debian packaging not available (missing wssshtools/debian/)"
    fi
fi

# Deactivate venv
deactivate

echo "Build complete. Binaries are in dist/ directory:"
echo "- dist/wssshd (server with web interface)"
echo "- dist/wssshc (client)"
echo "- dist/wsssh (SSH wrapper)"
echo "- dist/wsscp (SCP wrapper)"

if [ -d "wssshtools" ] && [ -f "wssshtools/wssshc" ]; then
    echo "- wssshtools/wssshc (C client)"
    echo "- wssshtools/wsssh (C SSH wrapper)"
    echo "- wssshtools/wsscp (C SCP wrapper)"
fi

if [ "$BUILD_DEBIAN" = true ] && ls dist/wsssh-tools*.deb >/dev/null 2>&1; then
    echo "- dist/wsssh-tools*.deb (Debian package)"
fi