#!/bin/bash
# Build script for Word Search application
# This script builds the application for the current platform

echo "========================================="
echo "Word Search Application Builder"
echo "========================================="
echo ""

# Check if PyInstaller is installed
if ! command -v pyinstaller &> /dev/null; then
    echo "PyInstaller not found. Installing dependencies..."
    pip install -r requirements.txt
fi

# Clean previous builds
echo "Cleaning previous builds..."
rm -rf build dist

# Detect platform
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    PLATFORM="Linux"
    OUTPUT_NAME="WordSearch"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    PLATFORM="macOS"
    OUTPUT_NAME="WordSearch.app"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "win32" ]]; then
    PLATFORM="Windows"
    OUTPUT_NAME="WordSearch.exe"
else
    PLATFORM="Unknown"
    OUTPUT_NAME="WordSearch"
fi

echo "Building for platform: $PLATFORM"
echo ""

# Build the application
echo "Running PyInstaller..."
pyinstaller word_search.spec

# Check if build was successful
if [ -d "dist" ]; then
    echo ""
    echo "========================================="
    echo "Build completed successfully!"
    echo "========================================="
    echo ""
    echo "Platform: $PLATFORM"
    echo "Output location: dist/"

    if [[ "$PLATFORM" == "macOS" ]]; then
        echo "Application: dist/WordSearch.app"
        echo ""
        echo "To run: open dist/WordSearch.app"
    elif [[ "$PLATFORM" == "Linux" ]]; then
        echo "Executable: dist/WordSearch"
        echo ""
        echo "To run: ./dist/WordSearch"
        chmod +x dist/WordSearch
    else
        echo "Executable: dist/WordSearch.exe"
        echo ""
        echo "To run: dist\\WordSearch.exe"
    fi
else
    echo ""
    echo "Build failed! Check the error messages above."
    exit 1
fi
