#!/bin/bash
#------------------------------------------------------------------------------
set -u
#==============================================================================
# Declarations and Environment
# Allow override of P4U_HOME, which is set only when testing P4U scripts.
export P4U_HOME=${P4U_HOME:-/p4/common/bin}
export P4U_LIB=${P4U_LIB:-/p4/common/lib}
export P4U_ENV=$P4U_LIB/p4u_env.sh
export P4U_LOG=Unset
# Environment isolation. For stability and security reasons, prepend
# PATH to include dirs where known-good scripts exist.
# known/tested PATH and, by implication, executables on the PATH.
export PATH=$P4U_HOME:$PATH:~/bin:.
export P4CONFIG=BogusValueNonExistentFile
[[ -r "$P4U_ENV" ]] || {
echo -e "\nError: Cannot load environment from: $P4U_ENV.\n\n"
exit 1
}
declare BASH_LIBS=$P4U_ENV
BASH_LIBS+=" $P4U_LIB/libcore.sh"
BASH_LIBS+=" $P4U_LIB/libp4u.sh"
for bash_lib in $BASH_LIBS; do
source $bash_lib ||\
{ echo -e "FATAL: Failed to load bash lib [$bash_lib]."; exit 1; }
done
declare Version=1.0.1
declare -i SilentMode=0
declare -i OverallReturnStatus=0
declare WorkshopCBDBranch=main
export CBD_HOME=$P4U_HOME/cbd
export VERBOSITY=3
#==============================================================================
# Local Functions
#------------------------------------------------------------------------------
# Function: terminate
function terminate
{
# Disable signal trapping.
trap - EXIT SIGINT SIGTERM
# Don't litter.
cleanTrash
vvmsg "$THISSCRIPT: EXITCODE: $OverallReturnStatus"
# Stop logging.
[[ "${P4U_LOG}" == off ]] || stoplog
# With the trap removed, exit.
exit $OverallReturnStatus
}
#------------------------------------------------------------------------------
# Function: usage (required function)
#
# Input:
# $1 - style, either -h (for short form) or -man (for man-page like format).
#------------------------------------------------------------------------------
function usage
{
declare style=${1:--h}
echo "USAGE for $THISSCRIPT v$Version:
$THISSCRIPT [-d <install_dir>] [-b <branch>>] [-L <log>] [-si] [-v<n>] [-D]
or
$THISSCRIPT [-h|-man|-V]
"
if [[ $style == -man ]]; then
echo -e "
DESCRIPTION:
This script clones a branch of the Component Based Development (CBD)
scripts from The Workshop, a publicly available Perforce server.
See: https://swarm.workshop.perforce.com/projects/perforce-software-cbd
OPTIONS:
-d <install_dir>
Specify the installation directory, \$CBD_HOME. The default is:
$CBD_HOME
-b <branch>
Specify that CBD scripts are to be acquired from the specified
branch (e.g. main, dev, ntx64) for the CBD project in The Workshop.
GENERAL OPTIONS:
-v<n> Set verbosity 1-5 (-v1 = quiet, -v5 = highest).
-L <log>
Specify the path to a log file, or the special value 'off' to disable
logging. By default, all output (stdout and stderr) goes to:
$(dirname ${P4U_LOG}).
NOTE: This script is self-logging. That is, output displayed on the screen
is simultaneously captured in the log file. Do not run this script with
redirection operators like '> log' or '2>&1', and do not use 'tee.'
-si Operate silently. All output (stdout and stderr) is redirected to the log
only; no output appears on the terminal. This cannot be used with
'-L off'.
-D Set extreme debugging verbosity.
HELP OPTIONS:
-h Display short help message
-man Display man-style help message
-V Dispay version info for this script and its libraries.
EXAMPLES:
For typical usage, no arguments are needed:
get_workshop_cbd
./test_cbd.sh
"
fi
exit 1
}
#------------------------------------------------------------------------------
# Function: get_workshop_cbd
# Install CBD to $CBD_HOME dir, which must be defined.
# Install from specified branch from The Workshop; default is 'main'.
#------------------------------------------------------------------------------
function get_workshop_cbd()
{
vvmsg "CALL get_workshop_cbd($*)"
declare branch=${1:-main}
export CBD_HOME=${CBD_HOME:-Unset}
# This should never happen ...
[[ $CBD_HOME == Unset ]] && bail "CBD_HOME is not defined."
# Deploy from a Perforce server. The user indicated should be a
# non-password user. Only read permissions are needed.
declare p4port=workshop.perforce.com:1666
declare p4user=ftp
declare remoteSpec=perforce_software-cbd_${branch}
declare getCmd="/p4/1/bin/p4_1 -s -u $p4user clone -p $p4port -r $remoteSpec"
[[ -d "$CBD_HOME" ]] && \
bail "The target install directory [$CBD_HOME] already exists."
runCmd "/bin/mkdir -p $CBD_HOME" \
"Making CBD_HOME dir [$CBD_HOME]." ||\
bail "Failed to create dir \$CBD_HOME [$CBD_HOME]."
cd "$CBD_HOME" ||\
bail "Failed to cd to \$CBD_HOME [$CBD_HOME]."
msg "Setting P4CONFIG=.p4config for cloning."
export P4CONFIG=.p4config
runCmd "$getCmd" \
"Cloning branch $branch of Workshop CBD.\nCBD_HOME=$CBD_HOME" ||\
bail "Failed to clone CBD from the Workshop."
runCmd "/p4/1/bin/p4_1 -s sync -f .p4ignore" \
"Force syncing CBD variant of .p4ignore, overriding default from 'p4 clone'." ||
warnmsg "Failed to force sync CBD variant of P4IGNORE file."
return 1
}
#==============================================================================
# Command Line Processing
declare -i shiftArgs=0
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-d) export CBD_HOME=$2; shiftArgs=1;;
(-b) WorkshopCBDBranch=$2; shiftArgs=1;;
(-h) usage -h;;
(-man) usage -man;;
(-V) show_versions; exit 1;;
(-v1) export VERBOSITY=1;;
(-v2) export VERBOSITY=2;;
(-v3) export VERBOSITY=3;;
(-v4) export VERBOSITY=4;;
(-v5) export VERBOSITY=5;;
(-L) export P4U_LOG=$2; shiftArgs=1;;
(-si) SilentMode=1;;
(-n) export NO_OP=1;;
(-D) set -x;; # Debug; use 'set -x' mode.
(*) usageError "Unknown arg ($1).";;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ $shiftArgs -gt 0 ]]; do
[[ $# -eq 0 ]] && usageError "Bad usage."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
#==============================================================================
# Command Line Verification
[[ $SilentMode -eq 1 && $P4U_LOG == off ]] && \
usageError "Cannot use '-si' with '-L off'."
[[ $P4U_LOG == Unset ]] && \
export P4U_LOG="/tmp/get_workshop_cbd.$(date +'%Y%m%d-%H%M%S').log"
#==============================================================================
# Main Program
trap terminate EXIT SIGINT SIGTERM
if [[ "${P4U_LOG}" != off ]]; then
LogDir="$(dirname $P4U_LOG)"
if [[ ! -d $LogDir ]]; then
/bin/mkdir -p "$LogDir" || bail "Couldn't create log dir [$LogDir]."
fi
touch "${P4U_LOG}" || bail "Couldn't touch log file [${P4U_LOG}]."
# Redirect stdout and stderr to a log file.
if [[ $SilentMode -eq 0 ]]; then
exec > >(tee ${P4U_LOG})
exec 2>&1
else
exec >${P4U_LOG}
exec 2>&1
fi
initlog
fi
get_workshop_cbd $WorkshopCBDBranch
if [[ $OverallReturnStatus -eq 0 ]]; then
msg "${H}\nAll processing completed successfully.\n"
else
msg "${H}\nProcessing completed, but with errors. Scan above output carefully.\n"
fi
# Illustrate using $SECONDS to display runtime of a script.
msg "That took $(($SECONDS/3600)) hours $(($SECONDS%3600/60)) minutes $(($SECONDS%60)) seconds.\n"
# See the terminate() function, which is really where this script exits.
exit $OverallReturnStatus