#!/bin/bash

# Get configuration and extraction type
# Aren't these also accessible as environment variables?
while getopts c:r: option
do
  case "${option}"
  in
  c) conf_name=${OPTARG};;
  r) repo_path=${OPTARG};;
  esac
done

if [ ${conf_name+x} ]; then
  echo "conf_name: "${conf_name}
else
  echo "conf_name not set. Use -c to set conf_name please."
  exit -1
fi

if [ ${repo_path+x} ]; then
  echo "repo_path: "${repo_path}
else
  echo "repo_path not set. Use -r to set repo_path please."
  exit -1
fi

cd ${repo_path}
#mkdir "./logs"

# Initialize path environment variables
source ~/.bashrc

# TODO: python package should be renamed to "cuimgsearch"
package_name="cufacesearch"

# Start extraction checker
# Can we add an environment variable to potentially skip that?
echo "Start extraction checker"
cmd="python ./"${package_name}"/"${package_name}"/updater/extraction_checker.py"
#args=" -d -c ./conf/generated/conf_extraction_"${conf_name}".json"
args=" -c ./conf/generated/conf_extraction_"${conf_name}".json"
#log="./logs/log_check_"${conf_name}
bash ./scripts/keep_alive_process.sh --cmd="${cmd}" --args="${args}"&

# Start extraction processor
echo "Start extraction processor"
cmd="python ./"${package_name}"/"${package_name}"/updater/extraction_processor.py"
args=" -c ./conf/generated/conf_extraction_"${conf_name}".json"
#log="./logs/log_proc_"${conf_name}
#bash ./scripts/keep_alive_process.sh --cmd="${cmd}" --args="${args}" --log="${log}"
bash ./scripts/keep_alive_process.sh --cmd="${cmd}" --args="${args}"

echo "Extraction processor failed. Restarting docker container..."
exit 1
