
BASEDIR=$(dirname "$0")

helpFunction()
{
   echo ""
   echo "Usage: $0 -s script"
   echo -e "\t-s Type of script ( checkstop | checkdestroy | checkerror | movedestroyed | initdb )"
   exit 1 # Exit script after printing help
}

helpFunctionCheck()
{
   echo ""
   echo "Usage: $0 -s script [-t type -r realortest]"
   echo -e "\t-s Type of script ( checkstop | checkdestroy | checkerror )"
   echo -e "\t-t Type of script ( warning | exec )"
   echo -e "\t-r Indicate whether is for test or real execution ( test | real )"
   exit 1 # Exit script after printing help
}

helpFunctionCheck2()
{
   echo ""
   echo "Usage: $0 -s checkerror [-r realortest]"
   echo -e "\t-r Indicate whether is for test or real execution ( test | real )"
   exit 1 # Exit script after printing help
}

helpFunctionCheck3()
{
   echo ""
   echo "Usage: $0 -s movedestroyed [-r isodate]"
   echo -e "\t-r An ISODATE (ie: '2020-31-12T00:00:00.000Z') to movedestroyed backwards"
   exit 1 # Exit script after printing help
}

while getopts "s:t:p:r:" opt
do
   case "$opt" in
      s ) script="$OPTARG" ;;
      t ) type="$OPTARG" ;;
      r ) realortest="$OPTARG" ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done

if [ -z "$script" ]
then
   echo "Script not recognised";
   helpFunction
fi

if [ "$script" = "checkstop" ] || [ "$script" = "checkdestroy" ]
then
   if [ -z "$type" ] || [ -z "$realortest" ]
   then
      echo "Some or all of the parameters are empty";
      helpFunctionCheck
   else
      if [ "$script" = "checkstop" ] 
      then
         $BASEDIR/checkstop.sh $type $realortest
      fi

      if [ "$script" = "checkdestroy" ] 
      then
         $BASEDIR/checkdestroy.sh $type $realortest
      fi
   fi
elif [ "$script" = "initdb" ] 
then
   if [ -z "$MONGO_URI" ]
   then
         echo "\$MONGO_URI is empty"
         exit
   else
         echo "\$MONGO_URI=$MONGO_URI"
         node $BASEDIR/../jobs/db-init.js
   fi 
elif [ "$script" = "checkerror" ] 
then
   if [ -z "$realortest" ]
   then
      echo "Some or all of the parameters are empty";
      helpFunctionCheck2
   else
      node $BASEDIR/../jobs/error5.js $realortest
   fi  
elif [ "$script" = "movedestroyed" ] 
then
   if [ -z "$realortest" ]
   then
      echo "Some or all of the parameters are empty";
      helpFunctionCheck3
   else
      node $BASEDIR/../jobs/move_destroyed.js $realortest
   fi  
   
fi

