#!/usr/local/bin/bash
#/ Usage: ghe-restore-git-hooks-cluster <host>
#/ Restore custom Git hooks data from an rsync snapshot
#/
#/ Note: This command typically isn't called directly. It's invoked by
#/ ghe-restore when the cluster strategy is used.
set -e

# Bring in the backup configuration
. $( dirname "${BASH_SOURCE[0]}" )/ghe-backup-config

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

bm_start "$(basename $0)"

# Grab host arg
GHE_HOSTNAME="$1"

# Perform a host-check and establish GHE_REMOTE_XXX variables.
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}

# Generate SSH config for forwarding
config=""

# Split host:port into parts
port=$(ssh_port_part "$GHE_HOSTNAME")
host=$(ssh_host_part "$GHE_HOSTNAME")

# Add user / -l option
user="${host%@*}"
[ "$user" = "$host" ] && user="admin"

hostnames=$(ghe_cluster_online_nodes "git-server")
for hostname in $hostnames; do
  config="$config
Host $hostname
  ServerAliveInterval 60
  ProxyCommand ssh -q $GHE_EXTRA_SSH_OPTS -p $port $user@$host nc.openbsd %h %p
  StrictHostKeyChecking=no
"
done

config_file=$(mktemp -t cluster-backup-restore-XXXXXX)
echo "$config" > "$config_file"

opts="$GHE_EXTRA_SSH_OPTS -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no"

cleanup() {
  for pid in $(jobs -p); do
    kill -KILL $pid > /dev/null 2>&1 || true
  done
  rm -f $config_file
}
trap 'cleanup' INT TERM EXIT

if [ -d "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/git-hooks/environments/tarballs" ]; then
  tarballs=$(cd $GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/git-hooks/environments/tarballs && find . -type f)
  hostname=$(echo $hostnames | awk '{ print $1; }')

  if [ -n "$hostname" ]; then
    ghe-rsync -avH --delete \
    -e "ssh -q $opts -p $port -F $config_file -l $user" \
    --rsync-path="sudo -u git rsync" \
    "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/git-hooks/environments/tarballs/" \
    "$hostname:$GHE_REMOTE_DATA_USER_DIR/git-hooks/environments/tarballs" 1>&3

    for tarball in $tarballs; do
      env_id=$(echo $tarball | cut -d '/' -f 2)
      ssh -q $opts -p $port -F $config_file -l $user $hostname "/bin/bash -c 'export PATH=\$PATH:/usr/local/share/enterprise && ghe-hook-env-update $env_id $GHE_REMOTE_DATA_USER_DIR/git-hooks/environments/tarballs/$tarball'" 1>&3 2>&3
    done
  fi
fi

if [ -d "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/git-hooks/repos" ]; then
  for hostname in $hostnames; do
    ghe-rsync -avH --delete \
    -e "ssh -q $opts -p $port -F $config_file -l $user" \
    --rsync-path="sudo -u git rsync" \
    "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/git-hooks/repos/" \
    "$hostname:$GHE_REMOTE_DATA_USER_DIR/git-hooks/repos" 1>&3 &
  done

  for pid in $(jobs -p); do
    wait $pid
    ret_code=$?
    if [ "$ret_code" != "0" ]; then
      echo "$pid exited $ret_code"
      exit $ret_code
    fi
  done
fi

bm_end "$(basename $0)"
