#!/bin/bash -eu
# SPDX-FileCopyrightText: 2025 Uwe Fechner
# SPDX-License-Identifier: MIT

if [[ $(basename $(pwd)) == "bin" ]]; then
    cd ..
fi

# Detect Julia version
julia_version=$(julia --version | awk '{print($3)}')
julia_major=${julia_version:0:3}
if [[ $julia_major == "1.1" ]]; then
    julia_major=${julia_version:0:4} 
fi

echo "Detected Julia version: $julia_version (major: $julia_major)"

# Detect current git branch
if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1 ; then
    branch=$(git rev-parse --abbrev-ref HEAD | sed 's/\//-/g')
else
    branch=""
fi

# Delete any existing custom system image for this Julia version and branch
if [[ $branch != "" ]]; then
    SOFILE="bin/kps-image-${julia_major}-${branch}.so"
else
    SOFILE="bin/kps-image-${julia_major}.so"
fi
if test -f "$SOFILE"; then
    echo "Deleting old system image: $SOFILE"
    rm -f "$SOFILE"
fi

# Only accept Julia 1.11 and 1.12
if [[ $julia_major != "1.11" ]] && [[ $julia_major != "1.12" ]]; then
    echo "Error: Julia $julia_major is not supported. Only Julia 1.11 and 1.12 are supported."
    echo "You can install Julia 1.11 with the following commands:"
    echo "juliaup add 1.11"
    echo "juliaup default 1.11"
    exit 1
fi

# Copy the appropriate default manifest
if [[ $julia_major == "1.11" ]]; then
    if [ -f "Manifest-v1.11.toml.default" ]; then
        cp Manifest-v1.11.toml.default Manifest-v1.11.toml
        echo "Copied Manifest-v1.11.toml.default to Manifest-v1.11.toml"
    else
        echo "Warning: Manifest-v1.11.toml.default not found"
        exit 1
    fi
else
    # Default to 1.12 for newer versions
    if [ -f "Manifest-v1.12.toml.default" ]; then
        cp Manifest-v1.12.toml.default Manifest-v1.12.toml
        echo "Copied Manifest-v1.12.toml.default to Manifest-v1.12.toml"
    else
        echo "Warning: Manifest-v1.12.toml.default not found"
        exit 1
    fi
fi

# Instantiate the project
echo "Instantiating project..."
julia --project -e '
using Pkg
try
    Pkg.instantiate()
catch e
    @warn "Pkg.instantiate() failed, attempting fresh resolve..." exception=e
    proj_dir = dirname(Base.active_project())
    # Remove all manifest files to force a clean resolve
    for f in readdir(proj_dir)
        if startswith(f, "Manifest") && endswith(f, ".toml") && !endswith(f, ".default")
            rm(joinpath(proj_dir, f), force=true)
        end
    end
    Pkg.resolve()
    Pkg.instantiate()
end
'
echo "Precompiling main project..."
julia --project -e 'using Pkg; Pkg.precompile()'

# Instantiate the examples and test projects
# Julia 1.12+ uses workspaces and shares the main manifest, so sub-project
# manifests are only needed for Julia 1.11.
if [[ $julia_major == "1.11" ]]; then
    echo "Instantiating examples and test project for Julia $julia_major..."
    # Seed test and examples manifests from the main manifest so that
    # shared dependencies use the same package versions.
    # Pkg.resolve() would add the extra deps needed by test/examples.
    rm -f examples/Manifest*.toml test/Manifest*.toml
    cp "Manifest-v${julia_major}.toml" "examples/Manifest-v${julia_major}.toml"
    cp "Manifest-v${julia_major}.toml" "test/Manifest-v${julia_major}.toml"
    julia --project=examples -e 'using Pkg; Pkg.resolve(); Pkg.instantiate()'
    echo "Precompiling examples project..."
    julia --project=examples -e 'using Pkg; Pkg.precompile()'
    julia --project=test -e 'using Pkg; Pkg.resolve(); Pkg.instantiate()'
    echo "Precompiling test project..."
    julia --project=test -e 'using Pkg; Pkg.precompile()'

    # Rename the manifest file to version-specific name
    if [ -f "examples/Manifest.toml" ]; then
        mv examples/Manifest.toml examples/Manifest-v${julia_major}.toml
        echo "Renamed examples/Manifest.toml to examples/Manifest-v${julia_major}.toml"
    fi
    if [ -f "test/Manifest.toml" ]; then
        mv test/Manifest.toml test/Manifest-v${julia_major}.toml
        echo "Renamed test/Manifest.toml to test/Manifest-v${julia_major}.toml"
    fi
else
    echo "Julia $julia_major uses workspaces; examples and test share the main manifest."
    echo "Precompiling examples project..."
    julia --project=examples -e 'using Pkg; Pkg.precompile()'
    echo "Precompiling test project..."
    julia --project=test -e 'using Pkg; Pkg.precompile()'
fi

echo "Installation complete!"
echo
echo "You can now launch Julia by typing"
echo "cd .."
echo "./bin/run_julia"
echo "and then get the menu with the examples by typing:"
echo "menu()"