#!/bin/bash

trap clean_up SIGHUP SIGINT SIGTERM

#function to help with the usage
function _print_syntax() {
  me=`basename "$0"`
  echo "Usage: $me [-d <dir> -l <dir> -w <num> -r <server>]"
  echo -e "\t-d <dir>\tDirectory of source code. If not provided /vagrant ist used"
  echo -e "\t-l <dir>\tDirectory where the logfiles should be put. If not set STDERR and STDOUT will be printed to console"
  echo -e "\t-h <string>\tSet hostname of server. (default: backend)"
}

#reset OPTIND
OPTIND=1

#preset GIT_HOME
GIT_HOME="/obs"

#preset HOST
export DEVHOST="backend"

#get options and check if there is a space in the dir arguments
while getopts "l:d:h:?" opt; do
  case "$opt" in
  l)
    REDIR_LOG=$OPTARG
    if [[ "$REDIR_LOG" =~ [[:space:]] ]]; then
      echo "Directory may not contain whitespaces"
      exit
    fi
    ;;
  d)
    GIT_HOME=$OPTARG
    if [[ "$GIT_HOME" =~ [[:space:]] ]]; then
      echo "Directory may not contain whitespaces"
      exit
    fi
    ;;
  h)
    export DEVHOST=$OPTARG
    if [[ "$DEVHOST" =~ [[:space:]] ]]; then
      echo "Hostname may not contain whitespaces"
      exit
    fi
    ;;
  \?)
    _print_syntax
    exit 0
    ;;
  esac
done

#REDIR_LOG points to the directory where the logfiles should be created. If the directory does not
#exist it will be created. APPEND_ARR[*] is filled with the output redirection for each service.
if [ -n "$REDIR_LOG" ]; then
  if [ ! -d "$REDIR_LOG" ]; then
    echo "$REDIR_LOG does not exist. Will try to create it"
    mkdir -p "$REDIR_LOG" || { echo "Failure in creating directory:"; print_error; exit; }
  fi
  APPEND_ARR[0]=">$REDIR_LOG/bs_srcserver.log 2>&1"
  APPEND_ARR[1]=">$REDIR_LOG/bs_repserver.log 2>&1"
  APPEND_ARR[2]=">$REDIR_LOG/bs_sched_i586.log 2>&1"
  APPEND_ARR[3]=">$REDIR_LOG/bs_sched_x86_64.log 2>&1"
  APPEND_ARR[4]=">$REDIR_LOG/bs_dispatch.log 2>&1"
  APPEND_ARR[5]=">$REDIR_LOG/bs_publish.log 2>&1"
  APPEND_ARR[6]=">$REDIR_LOG/bs_service.log 2>&1"
  APPEND_ARR[7]=">$REDIR_LOG/bs_signer.log 2>&1"
  APPEND_ARR[8]=">$REDIR_LOG/signd.log 2>&1"
  APPEND_ARR[9]=">$REDIR_LOG/bs_servicedispatch.log 2>&1"
fi

#check if GIT_HOME exists. If not it does not make any sense to continue.
if [ ! -d "$GIT_HOME" ]; then
  echo "There seems to be something wrong. Directory $GIT_HOME not found."
  echo "Please check if you are pointing to the right directory."
  exit 1
fi

#create BSConfig.pm and change hostname to localhost
if [ ! -f $GIT_HOME/src/backend/BSConfig.pm ]; then
  cp $GIT_HOME/src/backend/BSConfig.pm.template $GIT_HOME/src/backend/BSConfig.pm
fi

perl -pi -e 's/our \$bsserviceuser.*/our \$bsserviceuser="obsrun";/' $GIT_HOME/src/backend/BSConfig.pm
perl -pi -e 's/my \$hostname.*/my \$hostname=\"$ENV{'DEVHOST'}\";/' $GIT_HOME/src/backend/BSConfig.pm
perl -pi -e 's/\$ipaccess/\$removed_by_start_development_backend /' $GIT_HOME/src/backend/BSConfig.pm
perl -pi -e 's/.*our \$gpg_standard_key.*/our \$gpg_standard_key="\/etc\/ourkeyfile.asc";/' $GIT_HOME/src/backend/BSConfig.pm
perl -pi -e 's/.*our \$sign .*/our \$sign="\/usr\/bin\/sign";/' $GIT_HOME/src/backend/BSConfig.pm

#start backend services (the minimum needed) with two arch(i586/x86_64) schedulers and one worker
echo "Starting bs_srcserver"
COMMAND_STRING="$GIT_HOME/src/backend/bs_srcserver ${APPEND_ARR[0]} &"
eval $COMMAND_STRING
sleep 4
echo "Starting bs_repserver"
COMMAND_STRING="$GIT_HOME/src/backend/bs_repserver ${APPEND_ARR[1]} &"
eval $COMMAND_STRING
sleep 2
echo "Starting bs_sched i586"
COMMAND_STRING="$GIT_HOME/src/backend/bs_sched i586 ${APPEND_ARR[2]} &"
eval $COMMAND_STRING
echo "Starting bs_sched x86_64"
COMMAND_STRING="$GIT_HOME/src/backend/bs_sched x86_64 ${APPEND_ARR[3]} &"
eval $COMMAND_STRING
echo "Starting bs_dispatch"
COMMAND_STRING="$GIT_HOME/src/backend/bs_dispatch ${APPEND_ARR[4]} &"
eval $COMMAND_STRING
echo "Starting bs_publish"
COMMAND_STRING="$GIT_HOME/src/backend/bs_publish ${APPEND_ARR[5]} &"
eval $COMMAND_STRING
echo "Starting bs_service"
COMMAND_STRING="$GIT_HOME/src/backend/bs_service ${APPEND_ARR[6]} &"
eval $COMMAND_STRING
echo "Starting bs_signer"
COMMAND_STRING="$GIT_HOME/src/backend/bs_signer ${APPEND_ARR[7]} &"
eval $COMMAND_STRING
echo "Starting signd"
COMMAND_STRING="/usr/sbin/signd ${APPEND_ARR[8]} &"
eval $COMMAND_STRING
echo "Starting bs_servicedispatch"
COMMAND_STRING="$GIT_HOME/src/backend/bs_servicedispatch ${APPEND_ARR[9]} &"
eval $COMMAND_STRING

#Cleanup function to terminate all backend services
function clean_up {
  echo -e "\ncleaning up and exit"
  echo -e "Terminating Services"
  "$GIT_HOME"/src/backend/bs_srcserver --stop
  echo -e "Terminated SRC Server"
  "$GIT_HOME"/src/backend/bs_repserver --stop
  echo -e "Terminated REP Server"
  "$GIT_HOME"/src/backend/bs_sched --stop i586
  "$GIT_HOME"/src/backend/bs_sched --stop x86_64
  echo -e "Terminated Schedulers"
  "$GIT_HOME"/src/backend/bs_dispatch --stop
  echo -e "Terminated Dispatcher"
  "$GIT_HOME"/src/backend/bs_publish --stop
  echo -e "Terminated Publisher"
  "$GIT_HOME"/src/backend/bs_service --stop
  echo -e "Terminated Publisher"
  "$GIT_HOME"/src/backend/bs_signer --stop
  echo -e "Terminated Signer"
  killall signd
  gpgconf --kill gpg-agent
  echo -e "Terminated Signd"
  "$GIT_HOME"/src/backend/bs_servicedispatch --stop
  echo -e "Terminated service dispatch"
  exit;
}

if [ -n "$REDIR_LOG" ]; then
  echo "Logfiles will be written to $REDIR_LOG"
  echo "Each service has it's own logfile"
fi
echo "If you want to terminate the backend, just hit Ctrl-C"

wait
