#!/bin/bash
set -e

upload=''
islive=''
clearup=''
component='not_set'
loglevel='info'
sys_config='not_set'
run_config='not_set'
rundate=$(date '+%Y%m%d') # default today


SHORT=p:s:c:d:l:h
LONG=component:,sys_config:,run_config:,rundate:,noupload::,islive::,clearup::,loglevel::,help
OPTS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
echo $OPTS
eval set -- "$OPTS"

while :
do
  case "$1" in
    -p | --component )
      component="$2"
      shift 2
      ;;
    -s | --sys_config )
      sys_config="$2"
      shift 2
      ;;
    -c | --run_config )
      run_config="$2"
      shift 2
      ;;
    -d | --rundate )
      rundate="$2"
      shift 2
      ;;
    -l | --loglevel )
      loglevel="$2"
      shift 2;
      ;;
    --noupload )
      upload="--noupload"
      shift 2;
      ;;
    --islive )
      islive="--islive"
      shift 2;
      ;;
    --clearup )
      clearup="--clearup"
      shift 2;
      ;;
    -h | --help)
      "This is a launch script, example usage: runDockerEWS.sh -p Deposition -c -c /<path>/config_EastAfrica_fc_live.json -s 20220808"
      exit 2
      ;;
    --)
      shift;
      break
      ;;
    *)
      echo "Unexpected option: $1"
      ;;
  esac
done

if [ "$component" = "Environment" ]; then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Environment_Processor.sh"
elif [ "$component" = "Deposition" ];then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Deposition_Processor.sh"
elif [ "$component" = "Survey" ];then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Survey_Processor.sh"
elif [ "$component" = "Advisory" ];then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Advisory_Processor.sh"
elif [ "$component" = "Scraper" ];then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Environment_Processor.sh"
elif [ "$component" = "Epidemiology" ];then
    component_script="/storage/app/EWS_prod/code/coordinator/scripts/run_Epidemiology_Processor.sh"
elif [ "$component" = "MetResample" ];then
    component_script="/storage/app/EWS_prod/code/met_resampling/scripts/run_MetResample_Processor.sh"
elif [ "$component" = "MetPlotting" ];then
    component_script="/storage/app/EWS_prod/code/met_plotting/ews/scripts/run/run_met_plotting_Processor.sh"
else
    printf "component '%s' not recognised" "$component"
fi

function run_coordinator() {
    printf "component: %s\nsys_config: %s\nrun_config: %s\nrundate: %s\n" "$configs" "$sys_config" "$run_config" "$rundate"
    printf "optional args:\n%s\n" "$upload $islive $clearup"
    config_base=$(basename $run_config)
    printf "%s\n" $config_base
    docker run \
    --rm \
    --name ews_runner_${component}_${rundate}_${config_base} \
    -v "/storage/app/EWS_prod/code:/storage/app/EWS_prod/code" \
    -v "/storage/app/EWS_prod/regions:/storage/app/EWS_prod/regions" \
    -v "/storage/app/EWS_prod/envs/credentials:/storage/app/EWS_prod/envs/credentials" \
    -v "/storage/sftp:/storage/sftp" \
    -v "/storage/webdata:/storage/moved" \
    -p "587:587" \
    -w "/storage/app/EWS_prod/code" \
    lb584/ews_coordinator \
    "$component_script" \
    -sc "$sys_config" \
    -c "$run_config" \
    -d "$rundate" \
    -l "$loglevel" \
    $upload \
    $islive \
    $clearup

}

run_coordinator