#!/usr/bin/env bash
#/ Usage: ghe-restore-mssql <host>
#/ Restore MSSQL backup to a GitHub Actions service instance.
#/
#/ Note: This script typically isn't called directly. It's invoked by the ghe-restore command.
set -e

# Bring in the backup configuration
# shellcheck source=share/github-backup-utils/ghe-backup-config
. "$( dirname "${BASH_SOURCE[0]}" )/ghe-backup-config"

# Show usage and bail with no arguments
[ -z "$*" ] && print_usage

# Check if the import tool is available in this version
import_tool_available() {
  if [ -z "$GHE_TEST_REMOTE_VERSION" ]; then
    ghe_ssh_mssql "test -e /usr/local/bin/ghe-import-mssql"
  else
    # Always return available for test
    return 0
  fi
}

ghe_ssh_mssql() {
  if [ "$GHE_BACKUP_STRATEGY" = "cluster" ]; then
    ghe-ssh "${opts[@]}" "${ssh_config_file_opt[@]}" "$GHE_MSSQL_PRIMARY_HOST" "$@"
  else
    ghe-ssh "$GHE_HOSTNAME" "$@"
  fi
}

cleanup() {
  rm -rf $tempdir
}
trap 'cleanup' EXIT INT

# Grab host arg
GHE_HOSTNAME="$1"

# use the mssql primary host if GHES cluster configuration contains a mssql-master or use the ghe server if the mssql-master is not available.
GHE_MSSQL_PRIMARY_NODE="$(ghe-ssh "$GHE_HOSTNAME" -- "ghe-config cluster.mssql-master" || true)"
GHE_MSSQL_PRIMARY_HOST="$(ghe-ssh "$GHE_HOSTNAME" -- "ghe-config cluster.$GHE_MSSQL_PRIMARY_NODE.hostname" || true)"

if [ -z "$GHE_MSSQL_PRIMARY_HOST" ]; then
  GHE_MSSQL_PRIMARY_HOST="$GHE_HOSTNAME"
fi

tempdir=$(mktemp -d -t backup-utils-backup-XXXXXX)
ssh_config_file_opt=
opts=

# get server hostnames under cluster
if [ "$GHE_BACKUP_STRATEGY" = "cluster" ]; then
  ssh_config_file="$tempdir/ssh_config"
  ssh_config_file_opt="-F $ssh_config_file"
  opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
  ghe-ssh-config "$GHE_HOSTNAME" "$GHE_MSSQL_PRIMARY_HOST" > "$ssh_config_file"
fi

if ! import_tool_available; then
  ghe_verbose "ghe-import-mssql is not available"
  exit 1
fi

# Perform a host-check and establish the remote version in GHE_REMOTE_VERSION.
ghe_remote_version_required "$GHE_HOSTNAME"

# The snapshot to restore should be set by the ghe-restore command but this lets
# us run this script directly.
: ${GHE_RESTORE_SNAPSHOT:=current}

# The directory holding the snapshot to restore
snapshot_dir_mssql="$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/mssql"

# Transfer backup files from appliance to backup host
appliance_dir="$GHE_REMOTE_DATA_USER_DIR/mssql/backups"
echo "set -o pipefail; sudo rm -rf $appliance_dir; sudo mkdir -p $appliance_dir" | ghe_ssh_mssql /bin/bash
for b in "$snapshot_dir_mssql"/*
do
  [[ -e "$b" ]] || break

  filename="${b##*/}"
  ghe_verbose "Transferring $filename to appliance host"
  cat $snapshot_dir_mssql/$filename | ghe_ssh_mssql "sudo tee -a $appliance_dir/$filename >/dev/null 2>&1"
done

# Change owner to mssql:mssql to ready for restore
ghe_ssh_mssql "sudo chown -R mssql:mssql $appliance_dir"

# Invoke restore command
bm_start "$(basename $0)"
ghe_ssh_mssql -- "ghe-import-mssql" < "/dev/null" 1>&3
bm_end "$(basename $0)"
