#!/bin/bash
set -u
declare -i ErrorCount=0
declare User=${1:-}
declare UserSpecFile=
declare NewLine="\t//depot/swarmReview"
declare TmpFile=
declare SDPInstance=${SDP_INSTANCE:-1}
function msg () { echo -e "$*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "$ErrorCount"; }
[[ -n "$User" ]] ||\
bail "Missing required parameter <p4_user>.\\n\\nUsage: opt_user_into_reviews.sh <p4_user>"
#shellcheck disable=SC1091
source /p4/common/bin/p4_vars "$SDPInstance"
UserSpecFile="/tmp/$User.user.p4s"
TmpFile=$(mktemp)
p4 user -o "$User" |grep -v ^# > "$UserSpecFile" ||\
bail "Could not generate user spec file: $UserSpecFile"
if grep -q ^Reviews: "$UserSpecFile"; then
if grep -q 'swarmReview' "$UserSpecFile"; then
msg "Skipping user $User; already has swarmReview enabled."
else
awk -v NewLine="$NewLine" '{
print; # Print the current line
if ($0 == "Reviews:") print NewLine; # If line contains "Reviews:", print new line after.
}' "$UserSpecFile" > "$TmpFile" && mv "$TmpFile" "$UserSpecFile" # Process the file and replace it
cat "$UserSpecFile"
p4 -s user -f -i < "$UserSpecFile" ||\
bail "Failed to update user with this spec: $(cat "$UserSpecFile")"
fi
else
msg "User $User does not appear to have any reviews. Skipping."
exit 0
fi
exit "$ErrorCount"