@echo off
title YouTube Channel Archiver - Windows Build Script
echo ================================================================
echo YouTube Channel Archiver - Windows Build Script
echo ================================================================
echo.

echo [STEP 1] Checking Python installation...
python --version > nul 2>&1
if errorlevel 1 (
    echo ERROR: Python is not installed or not in PATH
    echo.
    echo Please install Python from https://python.org
    echo Make sure to check "Add Python to PATH" during installation
    echo.
    pause
    exit /b 1
)

for /f "tokens=2" %%i in ('python --version 2^>^&1') do set PYTHON_VERSION=%%i
echo SUCCESS: Python %PYTHON_VERSION% is installed

echo.
echo [STEP 2] Checking PyInstaller installation...
python -c "import PyInstaller" > nul 2>&1
if errorlevel 1 (
    echo PyInstaller not found. Installing now...
    echo.
    echo [STEP 2a] Updating pip...
    python -m pip install --upgrade pip
    
    echo.
    echo [STEP 2b] Installing PyInstaller...
    python -m pip install pyinstaller
    
    echo.
    echo [STEP 2c] Verifying PyInstaller installation...
    python -c "import PyInstaller; print('PyInstaller version:', PyInstaller.__version__)"
    if errorlevel 1 (
        echo ERROR: PyInstaller installation failed
        pause
        exit /b 1
    )
    echo SUCCESS: PyInstaller installed successfully
) else (
    echo SUCCESS: PyInstaller is already installed
    python -c "import PyInstaller; print('PyInstaller version:', PyInstaller.__version__)"
)

echo.
echo [STEP 3] Checking for source file...
if not exist "youtube_archiver_standalone.py" (
    echo ERROR: youtube_archiver_standalone.py not found in current directory
    echo Current directory: %CD%
    echo.
    echo Files in current directory:
    dir *.py
    echo.
    echo Please make sure the Python script is in the same folder as this batch file
    pause
    exit /b 1
)

echo.
echo [STEP 4] Building executable with PyInstaller...
echo Command: python -m PyInstaller --onefile --windowed --name "YouTubeChannelArchiver" --distpath dist --workpath build --clean youtube_archiver_standalone.py
echo.

REM Use python -m PyInstaller instead of direct pyinstaller command
python -m PyInstaller --onefile --windowed --name "YouTubeChannelArchiver" --distpath dist --workpath build --clean youtube_archiver_standalone.py

echo.
echo [STEP 5] Verifying build...
if exist "dist\YouTubeChannelArchiver.exe" (
    echo SUCCESS: Executable created successfully!
    echo Location: %CD%\dist\YouTubeChannelArchiver.exe
    
    REM Get file size
    for %%A in ("dist\YouTubeChannelArchiver.exe") do (
        set "filesize=%%~zA"
    )
    echo File size: %filesize% bytes
    
    echo.
    echo [STEP 6] Creating portable package...
    if not exist "portable" mkdir portable
    copy "dist\YouTubeChannelArchiver.exe" "portable\" > nul
    if exist "README.txt" copy "README.txt" "portable\" > nul
    if exist "LICENSE.txt" copy "LICENSE.txt" "portable\" > nul
    echo Portable version created in 'portable' folder
    
) else (
    echo ERROR: Executable was not created
    echo.
    echo Checking for build errors in the output above...
    echo Common issues:
    echo - Missing dependencies
    echo - Antivirus blocking the build
    echo - Insufficient disk space
    echo - Path issues with special characters
    echo.
    echo Build files location:
    if exist "build" (
        echo Build directory exists: %CD%\build
    ) else (
        echo Build directory not found
    )
    
    if exist "dist" (
        echo Dist directory exists: %CD%\dist
        echo Contents of dist directory:
        dir dist
    ) else (
        echo Dist directory not found
    )
    
    pause
    exit /b 1
)

echo.
echo ================================================================
echo BUILD COMPLETED SUCCESSFULLY!
echo ================================================================
echo.
echo Files created:
echo - dist\YouTubeChannelArchiver.exe (main executable - %filesize% bytes)
echo - portable\YouTubeChannelArchiver.exe (portable version)
echo.
echo Architecture info:
python -c "import platform; print('Python architecture:', platform.architecture()[0])"
python -c "import platform; print('Machine type:', platform.machine())"

echo.
echo Next steps:
echo 1. Test the executable: dist\YouTubeChannelArchiver.exe
echo 2. Distribute the portable folder, or
echo 3. Create an installer using installer.iss with Inno Setup
echo.
echo Press any key to open the dist folder...
pause > nul

REM Try to open the folder
if exist "dist\YouTubeChannelArchiver.exe" (
    explorer dist
) else (
    echo Could not open folder automatically
)
