#!/bin/bash

# Ask for version number
read -p "Enter the version number: " version
release_folder="releases/curd-$version"
windows_folder="$release_folder/windows"
linux_folder="$release_folder/linux"
macos_folder="$release_folder/macos"
installer_script="Build/curd-windows-build.iss"

# Ensure required directories exist
mkdir -p "$windows_folder" "$linux_folder" "$macos_folder"
mkdir -p "$windows_folder/bin"

# Update version in the installer script
sed -i "s/^AppVersion=.*/AppVersion=$version/" "$installer_script"

# Update Source paths in the installer script
sed -i "s|Source: \".*curd.exe\"|Source: \"Z:$windows_folder/curd.exe\"|" "$installer_script"
# sed -i "s|Source: \".*mpv.exe\"|Source: \"Z:$windows_folder/mpv.exe\"|" "$installer_script"

# Build Linux binary
echo "Building Linux binary..."
bash Build/buildlinux

# Move the Linux binary to the release folder
if [ -f "curd" ]; then
    mv curd "$linux_folder/curd-linux-x86_64"
else
    echo "Linux build failed. Please check Build/buildlinux."
    exit 1
fi

# Build macOS binaries
echo "Building macOS binaries..."
bash Build/buildmac
if [ -f "curd" ]; then
    mv curd "$macos_folder/curd-macos-x86_64"
else
    echo "macOS Intel build failed. Please check Build/buildmac."
    exit 1
fi

bash Build/buildmac-arm64
if [ -f "curd" ]; then
    mv curd "$macos_folder/curd-macos-arm64"
else
    echo "macOS ARM build failed. Please check Build/buildmac-arm64."
    exit 1
fi

# Create Universal binary for macOS
echo "Creating macOS Universal binary..."
llvm-lipo -create "$macos_folder/curd-amd64" "$macos_folder/curd-arm64" -output "$macos_folder/curd"
rm "$macos_folder/curd-amd64" "$macos_folder/curd-arm64"

# Build Windows binary
echo "Building Windows binary..."
bash Build/buildwindows

# Uncompress mpv.exe.gz
echo "Uncompressing mpv.exe..."
gunzip -c Build/mpv.exe.gz > "Build/mpv.exe"

# Move the Windows binary to the release folder
if [ -f "curd.exe" ]; then
    mv curd.exe "$windows_folder/curd-windows.exe"
else
    echo "Windows build failed. Please check Build/buildwindows."
    exit 1
fi

# # Copy mpv.exe to the Windows release folder
# if [ -f "Build/mpv.exe" ]; then
#     cp "Build/mpv.exe" "$windows_folder/"
# else
#     echo "mpv.exe not found. Skipping copy."
# fi

# Create Windows installer with Inno Setup
echo "Creating Windows installer..."
wine "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" "$installer_script"

# Move installer to the release folder
installer_output="Build/Output/CurdInstaller.exe"  # Replace with actual output location if different
if [ -f "$installer_output" ]; then
    mv "$installer_output" "$windows_folder/Curd-Windows-Installer.exe"
else
    echo "Installer creation failed. Please check Inno Setup script output."
fi

rm "Build/mpv.exe"

echo "Release build completed in $release_folder."
