#!/bin/bash
set -u
# Battle School SDP Push
declare -i ShowProgress=1
declare -i UpdateCount=0
declare -i ErrorCount=0
declare -i Debug=${DEBUG:-0}
declare RunHost=bos-helix-01
declare ThisHost=${HOSTNAME%%.*}
declare Cmd=
declare CmdLog=
function msg () { echo -e "$*"; }
function msgn () { echo -e -n "$*"; }
function dbg () { [[ "$Debug" -eq 0 ]] || msg "DEBUG: $*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Erorr}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Erorr}"; exit "$ErrorCount"; }
[[ "$ThisHost" == "$RunHost" ]] ||\
bail "Run from $RunHost, not $ThisHost."
msgn "Updating SDP files in Battle School Lab."
[[ "$ShowProgress" -eq 1 ]] && msgn "..."
CmdLog=$(mktemp)
for h in helix-0{2..5}; do
for d in /p4/sdp /p4/common; do
UpdateCount+=1
Cmd="rsync -a $d/ $h:$d"
dbg "Running: $Cmd"
if eval $Cmd > "$CmdLog" 2>&1; then
dbg "Output:\\n$(cat "$CmdLog")"
[[ "$ShowProgress" -eq 1 ]] && msgn "."
else
errmsg "Error running [$Cmd]. Output:\\n$(cat "$CmdLog")"
[[ "$ShowProgress" -eq 1 ]] && msgn "X"
fi
done
UpdateCount+=1
Cmd="rsync /p4/sdp/Version $h:/p4/sdp/Version"
dbg "Running: $Cmd"
if eval $Cmd > "$CmdLog" 2>&1; then
dbg "Output:\\n$(cat "$CmdLog")"
[[ "$ShowProgress" -eq 1 ]] && msgn "."
else
errmsg "Error running [$Cmd]. Output:\\n$(cat "$CmdLog")"
[[ "$ShowProgress" -eq 1 ]] && msgn "X"
fi
done
rm -f "$CmdLog"
if [[ "$ErrorCount" -eq 0 ]]; then
msg "\\nOK: Pushed updates ($UpdateCount)."
else
msg "\\nSee above; $ErrorCount errors were encountered doing $UpdateCount updates."
fi
exit "$ErrorCount"