################################################################################
# Name:
#   run-cbu-setup
# Description:
#   Pipe this script through sh to install/register the CBU agent and updater.
# Requires parameters:
#   N/A
# Requires environment variables:
#   username - user name for the account we are registering with
#   apikey - API key for the account we are registering with
#   flavor - installation type (e.g., raxcloudserver, dedicated, etc.)
#   datacenter - data center where this cloud server exists
#   agentrepo - (optional) repository host URL (e.g., http://agentrepo.drivesrvr.com)

echo ENTERING SETUP PIPE APP

if [ -z "${agentrepo}" ]; then
  export agentrepo=http://agentrepo.drivesrvr.com
fi
if [ -z "${username}" ] || [ -z "${flavor}" ] || [ -z "${datacenter}" ]; then
  echo "usage: curl ${agentrepo}/run-cbu-setup | sh" >&2
  echo 'The environment variables required by this script are:
    username - user name for the account we are registering with
    apikey - API key for the account we are registering with >> NOTE: Will prompt to enter silently in this script.
    flavor - installation type (e.g., raxcloudserver, dedicated, etc.)
    datacenter - data center code where this cloud server exists
    apihost - (optional) repository host URL (e.g., http://agentrepo.drivesrvr.com)
  ' >&2
else
  if [ -z "${apikey}" ]; then
    sttyorig=`stty -g`
    stty -echo
    echo -n 'Enter API key> '
    read apikey
    stty $sttyorig
  fi

  echo "Running Cloud Backup Setup" >&2
  setupapp=cloudbackup-updater.setup
  osdata=os.data
  updatersetup=${agentrepo}/updater/${setupapp}
  osdataurl=${agentrepo}/updater/${osdata}
  which curl 2>&1 >/dev/null; curlstatus=$?
  which wget 2>&1 >/dev/null; wgetstatus=$?
  if [ ${curlstatus} -eq 0 ]; then
    curl -O ${updatersetup} 2>/dev/null || echo "FATAL ERROR: Unable to download app for CBU" >&2
    curl -O ${osdataurl} 2>/dev/null || echo "FATAL ERROR: Unable to download OS data for CBU" >&2
  elif [ ${wgetstatus} -eq 0 ]; then
    wget ${updatersetup} 2>/dev/null || echo "FATAL ERROR: Unable to download app for CBU" >&2
    wget ${osdataurl} 2>/dev/null || echo "FATAL ERROR: Unable to download OS data for CBU" >&2
  else
    which yum 2>&1 >/dev/null && ( yum -y install curl && curl -O ${updatersetup} 2>/dev/null && curl -O ${osdataurl} 2>/dev/null || echo "FATAL ERROR: unable to download setup for CBU" >&2 )
    which apt 2>&1 >/dev/null && ( apt -y install curl && curl -O ${updatersetup} 2>/dev/null && curl -O ${osdataurl} 2>/dev/null || echo "FATAL ERROR: unable to download setup for CBU" >&2 )
  fi
  if [ -f ${setupapp} -a -f ${osdata} ]; then
    chmod 755 ${setupapp}
    if [ -z "${apihost}" ]; then
      echo RUNNING SETUP APP ./${setupapp} --configure --username ${username} --apikey REDACTED --flavor ${flavor} --datacenter ${datacenter} --force >&2
      ./${setupapp} --configure --username ${username} --apikey ${apikey} --flavor ${flavor} --datacenter ${datacenter} --force
    else
      echo RUNNING SETUP APP ./${setupapp} --configure --username ${username} --apikey REDACTED --flavor ${flavor} --datacenter ${datacenter} --apihost ${apihost} --force >&2
      ./${setupapp} --configure --username ${username} --apikey ${apikey} --flavor ${flavor} --datacenter ${datacenter} --apihost ${apihost} --force
    fi
  else
    echo "FATAL ERROR: Unable to find updater setup script and/or data" >&2
  fi
fi

