#!/bin/bash
# Get Job Header
# Usage: gjh.sh <job>
# where <job> can be specifie as the job name, like HI-86 or simply 86.
declare ThisScript="${0##*/}"
declare JobBaseURL="https://swarm.workshop.perforce.com/projects/perforce-software-sdp/jobs"
declare Job=
declare JobURL=
declare JobHeader=
declare JobKey="HI"
declare JobType=
declare TmpDir=
if [[ $# -lt 1 ]]; then
echo -e "\\nUsage: $ThisScript <Job>\\n\\ne.g.\\n\\t$ThisScript ${JobKey}-86\\n\\n${JobKey}-86 can be shortened to just 86."
exit 1
fi
TmpDir=$(mktemp -d)
cd "$TmpDir"
for Job in $*; do
[[ "$Job" =~ ^[0-9]+$ ]] && Job="${JobKey}-$Job"
JobURL="$JobBaseURL/$Job"
curl -s -k -O "$JobURL"
JobHeader=$(grep 'first-line' $Job|head -1|sed -E -e 's/^.*first-line">//g' -e 's#</span.*$##g' -e 's#<a href=".*">##g' -e 's#</a>##g' -e "s:':':g" -e "s:":\":g" -e "s:<:<:g" -e "s:>:>:g")
JobType=$(grep -E -A1 'Type.*</dt>' $Job | tail -1 | sed -E -e 's/^.*first-line">//g' -e 's#<.*$##g')
echo "$Job ($JobType): $JobHeader"
done
cd "$OLDPWD" || exit 1
rm -rf "$TmpDir"