#!/bin/sh
#
# $NetBSD: makesums,v 1.7.2.2 2005/12/15 20:12:54 tron Exp $
#
# Make checksum files for files in ``tardir''.  Usage:
# makesums [-a] [-t tardir] [setname [...]]
#
# If -t is omitted, RELEASEDIR must be set and not empty.
# The ``setname'' arguments comprise a list of files to checksum,
# and may be omitted (in which case ``*.tgz'' is used).
# If -a is given, then the list of sets is ignored, and ``*'' is used.
#
# After shell glob expansion, the list of sets is filtered to remove known
# output file names (of the form *SUM, SHA512 and MD5), non-existent files, and
# subdirectories. If this filtering leaves no files, then no output files are
# produced. Otherwise the resulting list of files are checksummed and five
# output files (BSDSUM, CKSUM, MD5, SHA512, SYSVSUM) are produced.
#

prog=${0##*/}

# set defaults
: ${CKSUM=cksum}
targetdir=$RELEASEDIR
dash_all=no

usage()
{
	cat 1>&2 <<USAGE
Usage: ${prog} [-a] [-t targetdir] [setname [...]]
	-a		checksum all plain files instead of [setname [...]]
	-t targetdir	\$RELEASEDIR		[$targetdir]
	setname [...]	sets to checksum 	[*.tgz]
USAGE
	exit 1
}

# handle args
while getopts at: ch; do
	case ${ch} in
	a)
		dash_all=yes
		;;
	t)	
		targetdir=${OPTARG}
		;;
	*)
		usage
		;;
	esac
done
shift $((${OPTIND} - 1))

if [ -z "$targetdir" ]; then
	echo 1>&2 '$RELEASEDIR must be set or provided with -t'
	exit 1
fi

cd $targetdir
pat="$*"
if [ $dash_all = yes ]; then
	pat='*'
elif [ -z "$pat" ]; then
	pat='*.tgz'
fi
lists=$(find $pat -prune \( -type f -o -type l \) \! -name '*SUM' \! -name MD5 \! -name SHA512 2>/dev/null)
if [ -n "$lists" ]; then
	${CKSUM} -o1 $lists > BSDSUM
	${CKSUM}     $lists > CKSUM
	${CKSUM} -a md5  $lists > MD5
	${CKSUM} -o2 $lists > SYSVSUM
	${CKSUM} -a sha512  $lists > SHA512
fi
