#!/bin/bash
###############################################################################
# This script is the command that is executed every run.
# Check the examples in examples/
#
# This script is run in the execution directory (execDir, --exec-dir).
#
# PARAMETERS:
# $1 is the candidate configuration number
# $2 is the instance ID
# $3 is the seed
# $4 is the instance name
# The rest ($* after `shift 4') are parameters to the run
#
# RETURN VALUE:
# This script should print one numerical value: the cost that must be minimized.
# Exit with 0 if no error, with 1 in case of error
###############################################################################
error() {
    echo "`TZ=UTC date`: $0: error: $@"
    exit 1
}

# This parses the arguments given by irace. Do not touch it!
CONFIG_ID=$1
INSTANCE_ID=$2
SEED=$3
INSTANCE=$(realpath $4)
shift 4 || error "Not enough parameters"
CONFIG_PARAMS=$*
# End of parsing

## Find our own location.
# BINDIR=$(dirname "$(readlink -f "$(type -P $0 || echo $0)")")

# EDIT THIS: Path to your executable
USE_DOCKER=false
if [ $USE_DOCKER = true ]
then
    LOCAL_DIR=$(pwd)
    DOCKER_DIR="/tmp"
    COMMIT="57e9d91a"
    EXE="docker container run --rm -t --user=$UID:$UID -v ${LOCAL_DIR}:${DOCKER_DIR}:z lucasguesserts/private:slopp-app-${COMMIT}"
else
    # use locally compiled executable
    EXE="$HOME/git_projects/slopp-heuristic/build/Release/app/allocate"
fi

# EDIT THIS: Specify how parameters are given to your executable
EXE_PARAMS="--maximum_running_time_seconds=10 --skip_type=strict --initial_branching_factor=4 --irace ${CONFIG_PARAMS} ${INSTANCE/$LOCAL_DIR/$DOCKER_DIR}"

if [ ! -x "$(command -v ${EXE})" ]; then
    error "${EXE}: not found or not executable (pwd: $(pwd))"
fi

# # If the program just prints a number, we can use 'exec' to avoid creating
# # another process, but there can be no other commands after exec, only exit.
# exec $EXE ${EXE_PARAMS}
# exit 1

# # These files are saved in execDir. Saving them in /tmp may be faster.
LOG_DIR=logs
mkdir -p ${LOG_DIR}
STDOUT="${LOG_DIR}/${CONFIG_ID}-${INSTANCE_ID}-${SEED}.stdout.log"
STDERR="${LOG_DIR}/${CONFIG_ID}-${INSTANCE_ID}-${SEED}.stderr.log"

# # # Otherwise, save the output to a file, and parse the result from it.  (If
# # # you wish to ignore segmentation faults you can use '{}' around the
# # # command.)
# # # If the command below fails, but you are not sure why, it may be useful to
# # # print it before executing using 'echo', as in:
echo "$EXE ${EXE_PARAMS} 1>${STDOUT} 2>>${STDERR}" >> ${STDERR}
$EXE ${EXE_PARAMS} 1>${STDOUT} 2>>${STDERR}
if [ $? -ne 0 ]; then
    error "Command failed: $EXE ${EXE_PARAMS}"
fi

# This may be used to introduce a delay if there are filesystem issues.
SLEEPTIME=1
while [ ! -s "${STDOUT}" ]; do
    sleep $SLEEPTIME
    let "SLEEPTIME += 1"
done

if [ ! -s "${STDOUT}" ]; then
    error "${STDOUT}: No such file or directory"
fi
# # This is an example of reading a number from the output.  It assumes that the
# # objective value is the first number in the first column of the last line of
# # the output.
COST=$(cat ${STDOUT})
echo "$COST"
exit 0
# # # Comment the following line if you wish to preserve temporary files
# # rm -f "${STDOUT}" "${STDERR}"
# exit 0

