#!/usr/bin/env python3
import sys
import platform
from pathlib import Path
import subprocess

def check_pyinstaller():
    """Check if PyInstaller is installed and available"""
    try:
        subprocess.run(
            [sys.executable, "-m", "PyInstaller", "--version"],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
        return True
    except (subprocess.CalledProcessError, FileNotFoundError):
        return False

def install_pyinstaller():
    """Install PyInstaller if not already installed"""
    print("PyInstaller not found. Installing now...")
    try:
        subprocess.check_call(
            [sys.executable, "-m", "pip", "install", "--upgrade", "pyinstaller"],
            stdout=subprocess.DEVNULL
        )
        print("PyInstaller installed successfully.")
        return True
    except subprocess.CalledProcessError as e:
        print(f"Failed to install PyInstaller: {e}")
        return False

def main():
    # Check and install PyInstaller if needed
    if not check_pyinstaller():
        if not install_pyinstaller():
            print("Error: PyInstaller installation failed. Please install it manually:")
            print("pip install pyinstaller")
            sys.exit(1)

    # Check if requirements.txt exists and install dependencies
    req_file = Path("requirements.txt")
    if req_file.exists():
        print("\nInstalling dependencies from requirements.txt...")
        try:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
                stdout=subprocess.DEVNULL
            )
        except subprocess.CalledProcessError as e:
            print(f"Warning: Failed to install some dependencies: {e}")
    else:
        print("\nWarning: requirements.txt not found. Installing default dependencies...")
        try:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", "pillow>=9.0.0", "pngmeta>=1.0.0"],
                stdout=subprocess.DEVNULL
            )
        except subprocess.CalledProcessError as e:
            print(f"Warning: Failed to install default dependencies: {e}")

    # Determine platform-specific build options
    system = platform.system()
    base_args = [
        "--name", "PNG Metadata Editor",
        "--windowed",
        "--onefile"
    ]

    # Check for version.txt
    version_file = Path("version.txt")
    if version_file.exists():
        print(f"\nUsing version info from {version_file}")
        base_args.extend(["--version-file", "version.txt"])
    else:
        print("\nWarning: version.txt not found. Using default version.")

    if system == "Windows":
        base_args.extend(["--icon", "AppIcon.ico"])
    elif system == "Darwin":  # macOS
        base_args.extend(["--icon", "AppIcon.icns"])
    else:  # Linux
        pass

    # Build the PyInstaller command
    pyinstaller_args = ["--distpath", "dist", "--workpath", "build"] + base_args
    cmd = [sys.executable, "-m", "PyInstaller"] + pyinstaller_args + ["png-meta-editor.py"]

    print(f"\nBuilding PNG Metadata Editor for {system}...")
    try:
        result = subprocess.run(cmd)
        if result.returncode == 0:
            print("\nBuild completed successfully!")
            print(f"Your executable is in the dist/ folder")
        return result.returncode
    except subprocess.CalledProcessError as e:
        print(f"\nBuild failed with error: {e}")
        return 1

if __name__ == "__main__":
    sys.exit(main())
