#!/bin/bash

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

export JULIA_PKG_SERVER_REGISTRY_PREFERENCE=eager

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

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

if [[ $HOSTNAME == "ufryzen" || $HOSTNAME == "framework" ]]; then
    GCT="--gcthreads=8,1"
    # export NO_MTK=true
    export USE_V9=true
else
    GCT="--gcthreads=4,1"
fi

if [[ $branch != "" ]]; then
    if test -f "bin/kps-image-${julia_major}-${branch}.so"; then
        echo "Found system image!"
        julia -J  bin/kps-image-${julia_major}-${branch}.so -t 1 $GCT --project -i -e 'using KiteModels'
    else
        julia $GCT --project  -i # -e 'using KiteModels'
    fi
else
    if test -f "bin/kps-image-${julia_major}.so"; then
        echo "Found system image!"
        julia -J  bin/kps-image-${julia_major}.so -t 1 $GCT --project -i -e 'using KiteModels'
    else
        julia $GCT --project -i # -e 'using KiteModels'
    fi
fi 
