#!{{ bash_bin }}

# DO NOT RUN THIS SCRIPT ON PRODUCTION INSTANCE
# DaTA WILL BE ERASED

set -e

echo "###################################################################################"
echo "#                                                                                 #"
echo "#  Warning: DO NOT RUN THIS SCRIPT ON PRODUCTION INSTANCE DaTA WILL BE ERASED !!! #"
echo "#                                                                                 #"
echo "###################################################################################"
echo -e "\nWill start in 10 seconds, cancel execution if you didn't want to run this script."

sleep 10

postgres_executable="{{ postgress_script }}"
redis_executable="{{ redis_script }}"
git_backup_directory="{{ gitlab_backup_dir }}"
redis_pid_file="{{ redis_pid_file }}"
postgres_pid_file="{{ postgres_pid_file }}"

bin_location="{{ bin_directory }}"
git_location="{{ git_location }}"
go_work_bin="{{ go_work_bin }}"
etc_location="{{ etc_directory }}"
gitlab_work="{{ gitlab_work_location }}"
promise_check="{{ promise_lab_location }}"
puma_script="{{ puma_script }}"
puma_pid_file="{{ puma_pid_file }}"
sidekiq_script="{{ sidekiq_script }}"
var_location="{{ var_directory }}"
gitaly_script="{{ gitaly_script }}"
gitaly_pid_file="{{ gitaly_pid_file }}"

# export GIT_EXEC_PATH=$git_location/libexec/git-core/

check_process () {
  pid_file=$1
  pname=$2
  if [ -e "$pid_file" ]; then
    pid=$(head -n 1 $pid_file) > /dev/null 2>&1
    if kill -0 "$pid"; then
      echo "$pname is already running with pid $pid. Aborting."
      exit 1
    fi
  fi
}

kill_process () {
  pid=$1
  R=0
  kill -0 "$pid" > /dev/null 2>&1 || R=$?
  if [ $R -eq 0 ]; then
    kill -TERM $pid
  fi
}

check_process $postgres_pid_file "Postgres"
check_process $redis_pid_file "Redis"
check_process $puma_pid_file "Puma"
check_process $gitaly_pid_file "Gitaly"

if [ -f "$postgres_pid_file" ]; then
  rm $postgres_pid_file
fi

# cleanup /var/backup and old repositories folders,
# restoration will created them at every run
echo "Cleanup gitlab backup and old repositories folders..."
rm -rf $var_location/backup/*
rm -rf $var_location/repositories.*

echo "Starting Postgres..."
$postgres_executable &
postgres_pid=$!
trap "echo 'kill $postgres_pid" EXIT TERM INT

echo "Starting Redis server..."
$redis_executable &
redis_pid=$!
trap "kill $postgres_pid $redis_pid" EXIT TERM INT
echo "Starting gitaly service..."
$gitaly_script &
gitaly_pid=$!
trap "kill $gitaly_pid $postgres_pid $redis_pid" EXIT TERM INT
echo "Starting Puma"
$puma_script &
puma_pid=$!
trap "kill $gitaly_pid $postgres_pid $redis_pid $puma_pid" EXIT TERM INT
# wait until Puma is started and pid file exists
sleep 200
if [ -s "$puma_pid_file" ]; then
  puma_pid=$(head -n 1 $puma_pid_file) > /dev/null 2>&1
  trap "kill $postgres_pid $redis_pid $puma_pid" EXIT TERM INT
fi
echo "[OK]"
echo "Restoring gitlab data..."

# XXX - workaround until this problem is fixed on runner1
sed -ie "s/connection.execute('TRUNCATE schema_migrations')\s*$/connection.execute('TRUNCATE schema_migrations') if connection.table_exists? 'schema_migrations'/g" $gitlab_work/lib/tasks/gitlab/db.rake

cd $git_backup_directory
PATH=$bin_location:$go_work_bin:$git_location/bin:$PATH gitlab-backup restore -vupok -go HEAD

echo "Checking gitlab promises..."
echo "[info] Not all promises are checked!"
$promise_check/gitlab-app

echo "Check gitlab-shell promise..."
$promise_check/gitlab-shell

#echo "starting Sidekiq to check sidekiq promise..."
#$sidekiq_script &
#sidekiq_pid=$!
#trap "kill $sidekiq_pid" EXIT TERM INT
#$promise_check/sidekiq

kill_process $postgres_pid
kill_process $redis_pid
kill_process $puma_pid
kill_process $gitaly_pid

RESTORE_EXIT_CODE=$?

if [ $RESTORE_EXIT_CODE -eq 0 ]; then
  echo 'Backup restoration successfully completed.'
else
  echo 'Backup restoration failed.'
fi

exit $RESTORE_EXIT_CODE