#!/bin/bash
#------------------------------------------------------------------------------
# Repro for:  SDP-583
set -u

# Usage:
# ./repro.sh [-f] 2>&1 | tee repro.log
#
# Scenario: Shelved CL Disappeared

export P4CONFIG=.p4config
export P4ENVIRO=/dev/null/.p4enviro
declare Version=1.0.0
declare -i ErrorCount=0
declare AppHome="$PWD"
declare H1="=============================================================================="

# Micro functions.
function msg () { echo -e "${1:-Hi}"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "$ErrorCount"; }
function cmd () { msg "${2:-Executing command: $1}"; $1; return $?; }

declare -i shiftArgs=0
declare -i Force=0
declare -i Scenario=1
declare ScenarioTitle="Shelved CL Disappeared"

#------------------------------------------------------------------------------
# Command Line Args
set +u; while [[ $# -gt 0 ]]; do
   case $1 in
      (-f) Force=1;;
      (-*) bail "Usage error: Unknown option ($1).";;
      (*) bail "Usage error: Unknown parameter ($1).";;
   esac

   # Shift (modify $#) the appropriate number of times.
   shift; while [[ $shiftArgs -gt 0 ]]; do
      [[ $# -eq 0 ]] && bail "Bad usage."
      shiftArgs=$shiftArgs-1
      shift
   done
done
set -u

ReproDir=/tmp/repro

msg "Started ${0##*/} v$Version at $(date)."

msg "ReproDir=$ReproDir"
[[ -d "$ReproDir" && "$Force" -eq 1 ]] && /bin/rm -rf "$ReproDir"
[[ -d "$ReproDir" ]] && bail "Old repro dir [$ReproDir] exists."

mkdir "$ReproDir" || bail "Could not do: mkdir \"$ReproDir\""
cd "$ReproDir" || bail "Could not do: cd \"$ReproDir\""

msg "==============================================================================\nScenario $Scenario: $ScenarioTitle\n"
msg "\nPreliminary info: Show versions of p4/p4d on the PATH:"
cmd "p4 -V"
cmd "p4d -V"

msg "\nPreliminary setup: Spin up local repo."
cmd "mkdir $ReproDir"
cd "$ReproDir" || bail "Could not do: cd \"$ReproDir\""
msg "Operating in: $PWD"
cmd "p4 init -C0 -n"

msg "\\nAdd some files."
echo -e "// ONE_H\n#ifndef ONE_H\n#define ONE_H 1\n\n//Stuff goes here\n\n#endif //ONE_H\n" > One.h
cmd "p4 status"
msg "Reconcile and submit."
p4 rec && p4 submit -d "Added One.h."

cmd "p4 edit One.h"
echo -e "New Content" >> One.h
p4 --field Description=Edit change -o | p4 change -i
CL=$(p4 -ztag -F %change% changes -s pending -m1)
echo CL=$CL
cmd "p4 shelve -c $CL" || errmsg "Failed to shelve CL @$CL."
cmd "p4 revert //..." || errmsg "Revert failed."
msg "The change exists now:"
cmd "p4 -s verify -q -S @=$CL"
cmd "p4 -s submit -e $CL" || errmsg "Submit of shelved CL @$CL failed."

msg "Now, try the verify with @= with the CL now submitted."
cmd "p4 -s verify -q -S @=$CL" && errmsg "Well, this is unexpected! The '@=' syntax worked even though the shelved CL was submitted."
msg "Now, try the verify with @= with a totally bogus CL that doesn't exist yet."
cmd "p4 -s verify -q -S @=$((CL+1))" && errmsg "Well, this is unexpected! The '@=' syntax worked even with a non-existent CL number."

if [[ "$ErrorCount" -eq 0 ]]; then
   msg "${H1}\nTest completed with no errors. Results are valid."
else
   errmsg "${H1}\nTest completed with $ErrorCount errors. Results may be invalid."
fi

exit "$ErrorCount"
