#!/bin/bash
#
#  Transfers copy of radar replay to this system

declare host="${1:-hel-fire-tlp-ci.rd.vaisala.com}"
declare dst="${2:-/srv/radar-replay}"

declare needPackages=""
if ! tmux -V &>/dev/null; then
  needPackages="${needPackages} tmux"
fi

if ! rsync --version &>/dev/null; then
  needPackages="${needPackages} rsync"
fi

if [ "${needPackages}" != "" ]; then
  declare cmd="yum -y install${needPackages}"
  if [ "${EUID}" != "0" ]; then
    cmd="sudo ${cmd}"
  fi
  echo -e "Need to install${needPackages} using:\n\n  ${cmd}\n"
  if ! eval ${cmd}; then
    echo "Failed to install necessary packages"
    exit 1
  fi
fi


cmd="rsync -avh --exclude site-data rsync://hel-fire-tlp-ci.rd.vaisala.com/radar-replay ${dst}"
echo "Running: ${cmd}"

if eval ${cmd}; then
  echo -e "Transfer successful, to start a simulator, try one of the following:

$(ls ${dst}/*-replay.sh)
"
  exit 0;
fi

echo "***ERROR*** Transfer failed!"
exit 1
