#!/bin/bash

usage="$(basename "$0") [-h] [-i file] [-d dir] [-o filename] -- package leo3 to a starexec archive

where:
    -h  show this help text
    -i  set the path to the Leo-III jar archive (default: ../bin/leo3.jar)
    -d  set the output directory where the archive will be created (default: .)
    -o  set the output file name, without extension (default: 'leo3_starexec')
    -e  include specified executables of external provers (default: none)"

outdir=$(realpath ".")
outfile="leo3_starexec"
infile=$(realpath "../bin/leo3.jar")
externals=()
starexec_run_default="#!/bin/bash

java -Xss128m -Xmx2g -Xms1g -jar ../leo3.jar \$1 -t \$STAREXEC_WALLCLOCK_LIMIT -p"
description=$(git rev-parse HEAD) 

while getopts ':hi:o:d:e:' option; do
  case "$option" in
    h) echo "$usage"
       exit
       ;;
    d) outdir=$(realpath "$OPTARG")
       ;;
    o) outfile="$OPTARG"
       ;;
    i) infile=$(realpath "$OPTARG")
       ;;
    e) externals+=($(realpath "$OPTARG"))
       ;;
    :) printf "missing argument for -%s\n" "$OPTARG" >&2
       echo "$usage" >&2
       exit 1
       ;;
   \?) printf "illegal option: -%s\n" "$OPTARG" >&2
       echo "$usage" >&2
       exit 1
       ;;
  esac
done

if [ ! -f "$infile" ]; then
    printf "ERROR: Leo-III jar not found at '$infile'. Be sure to build Leo-III first using 'make' or adjust the given file path.\n" >&2
    echo "$usage" >&2
    exit 1
fi
if [ ! -w "$outdir" ]; then
    printf "ERROR: Output directory '$outdir' is not writable. Check that it actually exists and is writable.\n" >&2
    exit 1
fi 

tmpdir=$(mktemp -d)
mkdir "$tmpdir/bin"
echo "$starexec_run_default" > "$tmpdir/bin/starexec_run_default"
echo "$description" > "$tmpdir/starexec_description.txt"
chmod +x "$tmpdir/bin/starexec_run_default"
infilepath=$(dirname "$infile")
infilefilename=$(basename "$infile")
allext=""
rm -f "$outdir/$outfile.tar.gz"
tar -cf "$tmpdir/$outfile.tar" -C "$infilepath" "$infilefilename"
tar -rf "$tmpdir/$outfile.tar" -C "$tmpdir/" "bin/starexec_run_default"
tar -rf "$tmpdir/$outfile.tar" -C "$tmpdir/" "starexec_description.txt"
#### exec for each ext prover
for prover in "${externals[@]}"
do
	proverpath=$(dirname "$prover")
  proverfilename=$(basename "$prover")
  extarg="--atp $proverfilename=../externals/$proverfilename"
  allext="$allext $extarg"
  tar -rf "$tmpdir/$outfile.tar" --transform 's,^,externals/,' -C "$proverpath" "$proverfilename"
  echo "#!/bin/bash

java -Xss128m -Xmx2g -Xms1g -jar ../leo3.jar \$1 -t \$STAREXEC_WALLCLOCK_LIMIT -p $extarg" > "$tmpdir/bin/starexec_run_$proverfilename"
  chmod +x "$tmpdir/bin/starexec_run_$proverfilename"
  tar -rf "$tmpdir/$outfile.tar" -C "$tmpdir/" "bin/starexec_run_$proverfilename"
done
#### exec for all ext provers at all
if [ ! -z "$allext" ]
then
  echo "#!/bin/bash

java -Xss128m -Xmx2g -Xms1g -jar ../leo3.jar \$1 -t \$STAREXEC_WALLCLOCK_LIMIT -p $allext" > "$tmpdir/bin/starexec_run_all"
  chmod +x "$tmpdir/bin/starexec_run_all"
  tar -rf "$tmpdir/$outfile.tar" -C "$tmpdir/" "bin/starexec_run_all"
fi

cat "$tmpdir/$outfile.tar" | gzip > "$outdir/$outfile.tar.gz"
rm -r "$tmpdir"
