#! /usr/bin/env bash
JuliaScriptEnvironment="$HOME/.julia/environments/JuliaScript"
if [ ! -d "$JuliaScriptEnvironment" ]; then
    # echo creating JuliaScript julia environment at "$JuliaScriptEnvironment"
    # TODO activate production, remove development version
    if [ -z "$JULIASCRIPT_PKG_DEVELOP" ]; then
        julia --project="$JuliaScriptEnvironment" -e 'import Pkg; Pkg.add("JuliaScript")'
    else
        julia --project="$JuliaScriptEnvironment" -e 'import Pkg; Pkg.develop(path=expanduser("~/.julia/dev/JuliaScript"))'
    fi
fi

# support special packagecompile command for running packagecompiler
FLAG_PACKAGECOMPILE=""
if [ "$1" == "packagecompile" ] || [ "$1" == "packagecompiler" ] ; then
    FLAG_PACKAGECOMPILE="true"
    shift  # skip first argument
fi

script_name="$(basename $1)"
trimsuffix=".jl"
if [[ $script_name == *"$trimsuffix" ]]; then
    script_name="${script_name%"$trimsuffix"}"
fi

export JULIASCRIPT_CACHE_HASH=($(sha256sum "$1"))
export JULIASCRIPT_CACHE_NAME="script_${script_name}_$JULIASCRIPT_CACHE_HASH"
export JULIASCRIPT_CACHE_PATH="$HOME/.julia/scripts_cache/$JULIASCRIPT_CACHE_NAME"
JULIASCRIPT_COMPILED_SYSIMAGE="$JULIASCRIPT_CACHE_PATH/sysimage.so"

if [ ! -z "$FLAG_PACKAGECOMPILE" ]; then
    # environment variables are enough, no args needed
    julia -q --project="$JuliaScriptEnvironment" -e "import JuliaScript; JuliaScript.create_sysimage()"

elif [ ! -d "$JULIASCRIPT_CACHE_PATH" ]; then
    # if no precompilation project exists, we create one
    # no shift, as JuliaScript needs to know the script path
    julia -q --project="$JuliaScriptEnvironment" -e "import JuliaScript; JuliaScript.julia_main()" -- "$@"

    # the added precompilation will automatically be precompiled on the next run but for a better user experience we do the precompilation already here
    # precompilation needs the correct packages to be on the load-path, hence we need to start another julia process      
    nohup julia -q --project="$JULIASCRIPT_CACHE_PATH" -e "import var\"$JULIASCRIPT_CACHE_NAME\"" </dev/null &>/dev/null &

    # It may make sense to also trigger packagecompile in the background every time a script is created the first time.
    # (a tiny example script needed less then 5 minutes for packagecompile to finish, so this could make sense).
    # Hidden behind an environment variable flag
    if [ ! -z "$JULIASCRIPT_PACKAGECOMPILE_ALWAYS" ]; then
        nohup julia -q --project="$JuliaScriptEnvironment" -e "import JuliaScript; JuliaScript.create_sysimage()" </dev/null &>/dev/null &
    fi

elif [ -f "$JULIASCRIPT_COMPILED_SYSIMAGE" ]; then
    # use packagecompiled sysimage if it exists
    shift  # skip first argument (the file path)
    julia -q --sysimage="$JULIASCRIPT_COMPILED_SYSIMAGE" -e "var\"$JULIASCRIPT_CACHE_NAME\".julia_main()" -- "$@"

else
    # we do this in bash so that only a single julia instance needs to be started (starting julia can take some time)
    shift  # skip first argument (the file path)
    julia -q --project="$JULIASCRIPT_CACHE_PATH" -e "import $JULIASCRIPT_CACHE_NAME; $JULIASCRIPT_CACHE_NAME.julia_main()" -- "$@"
fi
