#!/bin/bash
set -u

# Initialize install_sdp.sh test suite storage dirs for '-local' installs:
# /root/downloads
# /root/p4_binaries (may already exist, populated by bootstrap_test_machine.sh)

#==============================================================================
# Declarations and Environment

declare ThisScript=${0##*/}

declare -i ErrorCount=0
declare ThisArch=
declare Binary=
declare BinRel=r26.1
declare BinArch=
declare SampleDepotURL="https://ftp.perforce.com/perforce/tools/sampledepot.tar.gz"
declare FTPURLBase=
declare FTPURL=
declare Log=
declare LogLink=
declare H1="=============================================================================="

#==============================================================================
# Local Functions

function msg () { echo -e "$*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "$ErrorCount"; }

#==============================================================================
# Main Program

Log="${ThisScript%.sh}.$(date +'%Y-%m-%d-%H%M%S').log"
LogLink="${ThisScript%.sh}.log"

touch "$Log" || bail "Could not initialize log with: touch \"$Log\""

exec > >(tee "$Log")
exec 2>&1

if [[ -e "$LogLink" ]]; then
   if [[ -L "$LogLink" ]]; then
      rm -f "$LogLink"
   else
      # If the name that should be a symlink is not a symlink, move it aside before
      # creating the symlink.
      mv -f "$LogLink" "${LogLink%.log}.old.log"
   fi
fi

# Point LogLink symlink to current log.
ln -f -s "$Log" "$LogLink"

msg "${H1}\\nLog is: $Log\\n"

if [[ -d /root/downloads ]]; then
   bail "Directory already exists unexpectedly: /root/downloads.  To ensure a clean start, do: rm -rf /root/downloads"
fi

# Handing of BinArch here is oversimplified, but good enough for now.  The
# SDP get_p4_binaries.sh script has more sophisticted and nuanced handling.
ThisArch="$(uname -m)"
if [[ "$ThisArch" == "x86_64" ]]; then
   BinArch="bin.linux26x86_64"
elif [[ "$ThisArch" == "aarch64" ]]; then
   BinArch="bin.linux26aarch64"
else
   errmsg "Unhandled arch ($ThisArch); using bin.linux26x86_64"
   BinArch="bin.linux26x86_64"
fi

FTPURLBase="https://ftp.perforce.com/perforce/$BinRel/$BinArch"

if [[ -d /root/p4_binaries ]]; then
   msg "Skipping /root/p4_binaries: already exists (likely populated by bootstrap_test_machine.sh)."
else
   mkdir /root/p4_binaries || bail "Could not do: mkdir /root/p4_binaries"

   cd /root/p4_binaries || bail "Could not do: cd /root/p4_binaries"
   msg "Operating in $PWD"

   for Binary in p4 p4d p4broker p4p; do
      FTPURL="$FTPURLBase/$Binary"
	  msg "Acquiring: $FTPURL"
      curl -O "$FTPURL" || errmsg "Error calling: curl -O $FTPURL"
      chmod +x $Binary
      ./$Binary -V|grep Rev || errmsg "Could not determine version for binary /root/p4_binaries/$Binary."
   done
fi

mkdir /root/downloads || bail "Could not do: mkdir /root/downloads"

cd /root/downloads || bail "Could not do: cd /root/downloads"

curl -O "$SampleDepotURL" ||\
   errmsg "Error calling: curl -O $SampleDepotURL"

if tar -tzf ${SampleDepotURL##*/}; then
   msg "Verified: Sample Depot tarball is a tarball."
else
   errmsg "Sample Depot tarball is garbage."
fi

if [[ "$ErrorCount" -eq 0 ]]; then
   msg "\\nRoot Dirs initialized OK."
else
   errmsg "$ErrorCount errors encountered initializing root dirs."
fi

msg "\\nLog is: $Log\\n${H1}"

exit "$ErrorCount"
