#!/bin/bash

OPTIND=1

forced=false
while getopts ":f" opt
do
  case $opt in
    f) forced=true ;;
    esac
done

shift $(($OPTIND - 1))

# sanity checks
if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]
then
  echo "Call with <example name> (<experiment path>)."
  echo "If you omit <experiment path>, a subfolder with the name <example name> will be used."
  echo "Use -f to force removal of existing experiment folder."
  echo "Examples available: "$(find ${KPP_HOME}/case_studies/* -maxdepth 1 -type d -exec basename {} \;)
  exit 1
fi

example_req=$1
[ $# -eq 2 ] && path_req=$2 || path_req=$example_req

# check if BOXMOX is correctly set up
validate_BOXMOX_installation || { echo "Validating BOXMOX installation failed"; exit 1; }

example_dir=${KPP_HOME}/case_studies/${example_req}

# check if example exists
if [ ! -d ${example_dir} ]
then
  echo "Example BOXMOX experiment ${example_req} does not exist."
  exit 1
fi

mechUsed=$(cat ${example_dir}/mech_used)

$forced && forcetag="-f" || forcetag=""
new_BOXMOX_experiment ${forcetag} ${mechUsed} $path_req || { exit 1; }

cp ${example_dir}/* ${path_req}/
rm ${path_req}/mech_used
echo 1.8 > ${path_req}/VERSION
