#!/bin/sh

HOST=
TOUCH=
UNTOUCH=
_USER=$USER
_SSH=$(which ssh)

#
# Description
#   Utility that makes it easy to push installed EA3 on sandbox (via make setup) to a specified
#   remote host for testing on a real WHM environment.
#
# Usage
#   ./rsync-to-remote.sh -h <hostname> [ -u <user> ] [ -e "ssh -p 1232" ] [ -t|-T ]
#
# -h <hostname>
#   REQUIRED - specify remote hostname to interact with
#
# -e "ssh -options..."
#   OPTIONAL; specifies ssh command to use for rsync and the touch/rm of /var/cpanel/easy_no_targz_yaml
#   and /var/cpanel/easy_skip_cpanelsync; may be used to point to an identity file or specify port, e.g.
#
#     ./rsync-to-remote.sh -h <hostname> -e "ssh -p 123 -i /path/to/private_key"
#
# -u <username>
#   OPTIONAL; specify username on <hostname>
#
# -t 
#   OPTIONAL; *overridden by -T*; will touch /var/cpanel/easy_no_targz_yaml and
#   /var/cpanel/easy_skip_cpanelsync on <hostname> 
#
# -T
#   OPTIONAL; *overrides -t*; will remove /var/cpanel/easy_no_targz_yaml and /var/cpanel/easy_skip_cpanelsync
#   on <hostname>
#

while getopts ":e:h:tTu:" opt; do
  case $opt in
    e)
      RSYNC_SSH="-e \"$OPTARG\""
      ;;
    h)
      HOST=$OPTARG
      ;;
    t)
      TOUCH=1
      ;;
    T)
      UNTOUCH=1
      ;;
    u)
      _USER=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

if [ "x$HOST" == "x" ]; then
  echo "please specify a host with,  '-h <host>'"
  exit 1
fi

if [ ! -x `which rsync` ]; then
 echo fatal - rsync not found in \"$PATH\"
 exit 1
fi

# touch easy_no_targz_yaml and easy_skip_cpanelsync on target host
if [ "$UNTOUCH" == 1 ]; then
  echo removing touch files on $HOST, then exiting
  $_SSH $_USER@$HOST 'rm -f /var/cpanel/easy_no_targz_yaml /var/cpanel/easy_skip_cpanelsync'
  exit
elif [ "$TOUCH" == 1 ]; then
  echo adding touch files on $HOST, the rsync\'ing
  $_SSH $_USER@$HOST 'touch /var/cpanel/easy_no_targz_yaml /var/cpanel/easy_skip_cpanelsync'
fi
rsync -avz $_SSH /var/cpanel/easy $_USER@$HOST:/var/cpanel/ && \
rsync -avz $_SSH /var/cpanel/perl/easy $_USER@$HOST:/var/cpanel/perl
