#!/bin/bash
# Build directory structure

# build/bin/
# ├── x86_64-unknown-linux-gnu/          # Native Linux binary (x86_64)
# ├── musl-unknown-linux-gnu/            # Musl-compatible Linux binary
# ├── x86_64-pc-windows-gnu/             # Windows binary (x86_64)
# ├── aarch64-unknown-linux-gnu/         # Raspberry Pi 64-bit binary (AArch64)
# ├── armv7-unknown-linux-gnueabihf/     # Raspberry Pi 32-bit binary (ARMv7)
# ├── x86_64-apple-darwin/               # macOS Intel binary (x86_64)
# └── aarch64-apple-darwin/              # macOS Apple Silicon binary (ARM64)

# build/package/
# ├── x86_64-unknown-linux-gnu/          # Debian packages for x86_64 Linux
# ├── musl-unknown-linux-gnu/            # Debian packages for musl Linux
# ├── x86_64-pc-windows-gnu/             # Windows MSI packages
# ├── aarch64-unknown-linux-gnu/         # Debian packages for AArch64 Linux
# ├── armv7-unknown-linux-gnueabihf/     # Debian packages for ARMv7 Linux
# ├── x86_64-apple-darwin/               # macOS packages (Intel)
# └── aarch64-apple-darwin/              # macOS packages (Apple Silicon)

# Change to the project directory
cd "$(dirname "$0")/.."

# Create the build directory structure
mkdir -p build/{bin,package}

# Create subdirectories for binaries and packages with Rust-style target triples
mkdir -p build/bin/{x86_64-unknown-linux-gnu,musl-unknown-linux-gnu,x86_64-pc-windows-gnu,aarch64-unknown-linux-gnu,armv7-unknown-linux-gnueabihf,x86_64-apple-darwin,aarch64-apple-darwin}
mkdir -p build/package/{x86_64-unknown-linux-gnu,musl-unknown-linux-gnu,x86_64-pc-windows-gnu,aarch64-unknown-linux-gnu,armv7-unknown-linux-gnueabihf,x86_64-apple-darwin,aarch64-apple-darwin}

# Ensure proper permissions for build directory
chmod -R u+w build/

echo "Building available targets..."

# Build each target individually with error handling
successful_builds=()
failed_targets=()

# Define targets that are actually available in the current flake.nix
targets=(
    "tpnote-x86_64-unknown-linux-gnu"
    "tpnote-x86_64-unknown-linux-musl"
    "tpnote-x86_64-pc-windows-gnu"
    "tpnote-deb"
)

# Build targets
for target in "${targets[@]}"; do
    echo "Building $target..."
    if nix build .#$target --no-link 2>/dev/null; then
        echo "$target built successfully."
        successful_builds+=("$target")
    else
        echo "Warning: Failed to build $target"
        failed_targets+=("$target")
    fi
done

# Handle cross-compilation targets that might not be available
echo "Attempting cross-compilation targets (these may fail due to platform constraints)..."
cross_compilation_targets=(
    "tpnote-armv7-unknown-linux-gnueabihf"
    "tpnote-aarch64-unknown-linux-gnu"
    "tpnote-x86_64-apple-darwin"
    "tpnote-aarch64-apple-darwin"
)

for target in "${cross_compilation_targets[@]}"; do
    echo "Attempting to build $target..."
    if nix build .#$target --no-link 2>/dev/null; then
        echo "$target built successfully."
        successful_builds+=("$target")
    else
        echo "Info: Skipping $target (likely not supported on this platform)"
    fi
done

echo "All target building completed!"
echo "Successful builds: ${#successful_builds[@]}"
echo "Failed targets: ${#failed_targets[@]}"

# Extract and organize built artifacts
echo "Organizing built artifacts..."

for target in "${successful_builds[@]}"; do
    # Get the store path for the built target
    store_path=$(nix path-info .#$target 2>/dev/null | head -n 1 | cut -d' ' -f1)

    if [ -n "$store_path" ]; then
        echo "Processing $target from $store_path"

        case "$target" in
            "tpnote-x86_64-unknown-linux-gnu")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/x86_64-unknown-linux-gnu/
                    echo "Copied native Linux binary"
                fi
                ;;
            "tpnote-x86_64-unknown-linux-musl")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/musl-unknown-linux-gnu/
                    echo "Copied musl Linux binary"
                fi
                ;;
            "tpnote-x86_64-pc-windows-gnu")
                if [ -f "$store_path/bin/tpnote.exe" ]; then
                    cp "$store_path/bin/tpnote.exe" build/bin/x86_64-pc-windows-gnu/tpnote.exe
                    echo "Copied Windows binary"
                elif [ -f "$store_path/tpnote.exe" ]; then
                    cp "$store_path/tpnote.exe" build/bin/x86_64-pc-windows-gnu/tpnote.exe
                    echo "Copied Windows binary"
                fi
                ;;
            "tpnote-armv7-unknown-linux-gnueabihf")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/armv7-unknown-linux-gnueabihf/
                    echo "Copied Raspberry Pi 32-bit binary"
                fi
                ;;
            "tpnote-aarch64-unknown-linux-gnu")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/aarch64-unknown-linux-gnu/
                    echo "Copied Raspberry Pi 64-bit binary"
                fi
                ;;
            "tpnote-x86_64-apple-darwin")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/x86_64-apple-darwin/
                    echo "Copied macOS Intel binary"
                fi
                ;;
            "tpnote-aarch64-apple-darwin")
                if [ -f "$store_path/bin/tpnote" ]; then
                    cp "$store_path/bin/tpnote" build/bin/aarch64-apple-darwin/
                    echo "Copied macOS ARM binary"
                fi
                ;;
            "tpnote-deb")
                # Find and copy the .deb file
                deb_file=$(find "$store_path" -name "*.deb" -type f 2>/dev/null | head -n 1)
                if [ -n "$deb_file" ]; then
                    cp "$deb_file" build/package/x86_64-unknown-linux-gnu/
                    echo "Copied Debian package"
                fi
                ;;
        esac
    else
        echo "Warning: Could not find store path for $target"
    fi
done

echo "All targets built and organized successfully!"
echo "Summary:"
echo "- Successfully built: ${#successful_builds[@]} targets"
echo "- Failed builds: ${#failed_targets[@]} targets"
if [ ${#failed_targets[@]} -gt 0 ]; then
    echo "Failed targets: ${failed_targets[*]}"
fi
