#!/bin/sh
############################################################ IDENT(1)
#
# $Title: Script to list perforce checkpoints $
#
############################################################ CONFIGURATION
#
# Local perforce server settings
# NB: Taken from rc.conf(5) on FreeBSD
#
P4D_ROOT=$( sysrc -n p4d_root 2> /dev/null )
#
# Sensible defaults (i.e., Linux)
#
: ${P4D_ROOT:=/perforce}
#
# Checkpoint specifics (case-sensitive)
#
PREFIX=checkpoint
UMATCH='[^.]+'
ZMATCH='\.[Gg][Zz]'
############################################################ GLOBALS
pgm="${0##*/}" # Program basename
#
# Global exit status
#
SUCCESS=0
FAILURE=1
#
# Command-line options
#
SHOW_ALL= # -a
SHOW_U= # -u
SHOW_Z= # -z
ROOTDIR= # -R dir
############################################################ FUNCTIONS
usage()
{
exec >&2
local optfmt="\t%-8s %s\n"
printf "Usage: %s [-u|-z] { -a | NUM ... }\n" "$pgm"
printf "OPTIONS:\n"
printf "$optfmt" "NUM" "Ceckpoint number as-in \`checkpoint.NUM[.gz]'."
printf "$optfmt" "-a" "Show all checkpoints."
printf "$optfmt" "-R dir" "List dir checkpoints (default $P4D_ROOT)."
printf "$optfmt" "-u" "Show only uncompressed checkpoints."
printf "$optfmt" "-z" "Show only compressed checkpoints (*.gz)."
exit $FAILURE
}
############################################################ MAIN
#
# Command-line options
#
while getopts aR:uz flag; do
case "$flag" in
a) SHOW_ALL=1 ;;
R) ROOTDIR="$OPTARG" ;;
u) SHOW_U=1 SHOW_Z= ;;
z) SHOW_Z=1 SHOW_U= ;;
*) usage
esac
done
shift $(( $OPTIND - 1 ))
#
# Validate command-line arguments
#
[ $# -gt 0 -o "$SHOW_ALL" ] || usage
P4D_ROOT="${ROOTDIR:-$P4D_ROOT}" # set after last-call to usage()
# NOTREACHED unless either `-a' (list all) or `NUM ...' is given
#
# All actions should be performed within the p4d_root (e.g., /perforce)
#
cd "$P4D_ROOT" || exit
#
# If given `-a' list all checkpoints, otherwise list only certain numbers
#
if [ "$SHOW_ALL" ]; then
# Handle `-u' versus `-z' for listing only nongz/gz (respectively)
regex="($UMATCH)($ZMATCH)?"
[ "$SHOW_U" ] && regex="($UMATCH)"
[ "$SHOW_Z" ] && regex="($UMATCH)($ZMATCH)"
else
nums=
for num in "$@"; do
nums="$regex|$num"
done
# Handle `-u' versus `-z' for listing only nongz/gz (respectively)
regex="(${nums#|})($ZMATCH)?"
[ "$SHOW_Z" ] && regex="(${nums#|})($ZMATCH)"
[ "$SHOW_U" ] && regex="(${nums#|})"
fi
#
# List checkpoints (filtered by awk(1))
#
ls -1 "$PREFIX".* |
awk -v regex="^$PREFIX\\.$regex$" '$0 ~ regex && ++N END {exit !N}'
################################################################################
# END
################################################################################
#
# $Copyright: 2015 Devin Teske. All rights reserved. $
#
# $Header: //guest/freebsdfrau/p4t/libexec/list_checkpoint#1 $
#
################################################################################