#!/bin/sh
#
# Print versions and names of checked-out CVS files.  It's up to
# the user to make sure they are in good state if necessary, for example
# that they contain no local modifications if this is a manifest version
# list for a reproducable set of files.
#
# Copyright (c) Hewlett-Packard (Paul Bame <bame@debian.org>)
# $Id: cvs-versions,v 1.2 2002/08/16 19:43:14 bame Exp $
#

format=-f
while [ $# != 0 ]
do
    case $1 in
        -f|-v|-c|-C) format=$1; shift 1;;
	-*) echo "Usage: $0 [-v|-f|-c] [files/directories]" >&2
	    echo -e "\t-f\tfilename first (default)" >&2
	    echo -e "\t-v\tversion# first" >&2
	    echo -e "\t-c\tcvs update commands" >&2
	    echo -e "\t-C\tcvs update commands (globbed)" >&2
	    exit 2;;
	*) break;
    esac
done

# If no files/dirs specified, set to current
[ $# = 0 ] && set -- .

if [ X$format = X-C ]
then
    TMPDIR=/tmp/_cv_$$
    trap "rm -fr $TMPDIR;exit" 0 1 2 3 15
    mkdir -p $TMPDIR
fi

Format() {
    case "$format" in
	-v) echo -e "$1\t$2";;
	-f) echo -e "$2\t$1";;
	-c) echo -e "cvs up -r$1 $2";;
	-C) echo -e "$2" >> $TMPDIR/$ver;;
    esac
}

while [ $# != 0 ]
do
    if [ -f $1 ]
    then
        dir=${1%/*}; [ "$dir" = $1 ] && dir=.
	f=${1##*/}; [ -z "$f" ] && f=$1
	while IFS=/ read nothing fname ver other
	do
	    [ "$fname" = $f ] && Format $ver $1
	done < $dir/CVS/Entries
    else
	find "$1" -name CVS -a -type d -print |
	    while read cvsdir
	    do
		dir=${cvsdir%/CVS}
		while IFS=/ read nothing fname ver other
		do
		    [ -z "$nothing" ] || continue
		    Format $ver $dir/$fname
		done < $cvsdir/Entries
	    done
    fi
    shift 1
done

if [ X$format = X-C ]
then
    for ver in $(ls $TMPDIR)
    do
        xargs -s 1024 echo cvs up -r$ver < $TMPDIR/$ver
    done
fi
