#!/bin/bash -eu
update=false
if [[ $# -gt 0 ]]; then
    if [[ $1 != "--update" ]]; then
        echo "Invalid parameter! Use:"
        echo "./create_sys_image.sh"
        echo "or"
        echo "./create_sys_image.sh --update"
        exit 1
    else
        update=true
    fi
fi

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

mkdir -p output

export MPLBACKEND=qt5agg
export JULIA_PKG_SERVER_REGISTRY_PREFERANCE=eager

if [[ $HOSTNAME == "ufryzen" || $HOSTNAME == "framework" ]]; then
    export NO_MTK=true
fi

if ! command -v juliaup &> /dev/null; then
    echo "Please install the Julia installer 'juliaup'!"
    echo "See: https://github.com/JuliaLang/juliaup"
    exit 1
fi

juliaup add 1.10
juliaup default 1.10

PYTHON_PATH=$(which python3)
if [ -x "$PYTHON_PATH" ]; then
    echo "Python is found at $PYTHON_PATH"
    if $PYTHON_PATH -c "import matplotlib" &> /dev/null; then
        echo "Matplotlib found. Using existing installation."
        export PYTHON=$PYTHON_PATH
    else
        echo "Matplotlib is not found."
        read -p "Do you want to install matplotlib with Conda? (y/n): " choice
        case "$choice" in 
          y|Y ) 
            export PYTHON=""
            ;;
          n|N ) 
            echo "Exiting without installing matplotlib."
            exit 1
            ;;
          * ) 
            echo "Invalid choice. Exiting."
            exit 1
            ;;
        esac
    fi
else
    echo "Python is not found."
    exit 1
fi

julia_version=$(julia --version | awk '{print($3)}')

julia_major=${julia_version:0:4} 
branch=$(git rev-parse --abbrev-ref HEAD)
if test -f "kps-image-${julia_major}-${branch}.so"; then
    mv bin/kps-image-${julia_major}-${branch}.so kps-image-${julia_major}-${branch}.so.bak
fi
julia --startup-file=no -e "using Pkg; Pkg.add(\"TestEnv\")"

if [[ $update == true ]]; then
    echo "Updating packages..."
    if test -f "Manifest.toml"; then
        mv Manifest.toml Manifest.toml.bak
    fi
    julia --startup-file=no --pkgimages=no --project -e "using TestEnv; TestEnv.activate(); using Pkg; Pkg.add(\"PyCall\"); Pkg.build(\"PyCall\")"
    if [[ $PYTHON == "" ]]; then
        julia --startup-file=no --pkgimages=no --project -e "using TestEnv; TestEnv.activate(); using Pkg; Pkg.add(\"Conda\"); using Conda; Conda.add(\"matplotlib\"); using ControlPlots"
    fi
    julia --startup-file=no --pkgimages=no --project -e "using Pkg; Pkg.update()"
else
    cp Manifest-1.10.toml.default Manifest.toml
    echo "Using Manifest-1.10.toml.default ..."
fi
julia --project -e "include(\"./test/create_sys_image.jl\");"
mv kps-image_tmp.so bin/kps-image-${julia_major}-${branch}.so
julia --project -e "using Pkg; Pkg.precompile()"
cd src
touch *.jl # make sure all modules get recompiled in the next step
cd ..
julia --project -J bin/kps-image-${julia_major}-${branch}.so -e "using KiteUtils, KiteControllers"
julia --project -J bin/kps-image-${julia_major}-${branch}.so -e "using TestEnv; TestEnv.activate(); using ControlPlots, NativeFileDialog"
julia --project -J bin/kps-image-${julia_major}-${branch}.so -e "using Pkg; Pkg.update()"
