#!@POSIXSHELL@
#
# Make an signed tar archive of the current directory.
trap '/bin/rm -fr /var/tmp/swign.$$; exit 1' 1 2 15

# Copyright (C) 2003,2004,2005,2006 Jim Lowe
#
# COPYING TERMS AND CONDITIONS:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

SHELL="@POSIXSHELL@"
SWBISLIBEXECDIR="/@SWBISLIBEXECDIR@/"
SWBIS_RELEASE=@SWBIS_RELEASE@
export GTAR="@SWBISGTAR@"

version() {
	echo "swign (swbis) version ${SWBIS_RELEASE}"
	echo "Copyright (C) 2003,2004,2005,2006 Jim Lowe"
	echo "This software is distributed under the terms of the GNU General Public License"
	echo "and comes with NO WARRANTY to the extent permitted by law."
	echo "See the file named COPYING for details."
	exit 0;
}

cmdusage() {
	echo "Usage : swign [options] @-"
	echo "        swign [options] @."
	echo "        swign -S [options]"
	echo "        swign --help"
}

look_for_file_permissions() {
	# grep -v '[ \t]*#' | sed -e 's/  */ /g' |
	grep -v '^[ \t]*#' |
	sed -e 's/  */ /g' -e 's/#.*//' |
	grep file_permissions |
	tail -1 |
	sed -e 's/file_permissions//' -e 's/-u 000/-Z 000/'
	#
	# Above translate the -u 000  to  -Z 000  in order avoid the
	# collision with the use of -u
	#
}

missing_which() {
	pgm="$1"
	xxname=`which $pgm 2>/dev/null`
	test -f "$xxname" -o -h "$xxname"
	case "$?" in
		0)
			echo "$xxname"
			return 0
			;;
		*)
			echo "Missing $pgm" 1>&2
			echo "/"
			return 1
			;;
	esac
}

usage() {
	echo "Usage: swign [options] @-    # Write the archive to stdout"
	echo "  or:  swign [options] @.    # Just load ./catalog, write nothing"
	echo "  or:  swign [options] -S    # same as @."
	echo "Example:   swign  -s myPSF -u \"MyGPG Name\" @-"
	echo "Load a signed metadata catalog file into the current directory and then"
	echo "using GNU tar and the distribution file list write the archive to stdout."
	echo "This script will remove and replace a file in \".\" named ./catalog/."
	echo "This script assumes GNU tar is installed."
	echo "     --help show this help to stdout."
	echo "     --psf|--show-psf  Write the internal PSF to stdout and exit."
	echo " -S, --sign-this  Write ./catalog/ into '.' as a signature of '.' and exit,"
	echo "                  writing nothing to stdout."
	echo "                  Same as using the @. target syntax."
	echo " -s, --source=PSF  specify a psf file."
	echo " -N, --nosign  do not sign the package."
	echo " -T, --show-names-only   Just shows some info, writes nothing and exit."
	echo " -t, --run-sanity-test   Writes archive to ../<name>.swigntest.tar.gz instead of"
	echo "                        stdout and runs some sanity tests."
	echo " -D, --with-checksig FILE  Include checksig script sourced at FILE"
	echo " -o, --owner OWNER."
	echo " -g, --group GROUP."
	echo " -u, --local-user name    gpg local user name."
	echo " -h, --homedir DIR        gpg home directory."
	echo " -x format    where format is ustar|gnu|gnutar|oldgnu"
	echo "                    ustar is the default"
	echo "                    gnutar is same as oldgnu, oldgnu is preferred"
	echo "                    gnu is only supported for GNU tar 1.15.x"
	echo "                    oldgnu is only supported for GNU tar 1.13.25"
	echo "  --no-psf-detect   Do not attempt to make the ownership options"
	echo "                    consistent with the supplied PSF."
	echo "Date: 2006-02-08.  Copying Terms: GNU GPL."
	echo "Identification: swign: Implementation extension administration program of swbis."
	echo 
	echo "Report bugs to <bug-swbis@gnu.org>"
}

check_files() {
	package_dir="$1"
	files="$2"

	csum=`${CAT} "$files" | ${SED} -e 's@/$@@' | ${SORT} | ${MD5SUM}`
	fsum=`(cd ..; ${TAR} cf - "${package_dir}" | ${TAR} tf - | ${SED} -e 's@/$@@' | ${SORT} | ${MD5SUM})`
	if [ "$csum" = "$fsum" ]; then
		return 0
	else
		return 1
	fi
}

get_package_dirname() {

	base1=`pwd`
	package_dir=`basename "$base1"`
	PACKAGEDIRNAME=`echo "$package_dir" | sed -e 's@-.*@@'`	
	PACKAGEDIRNAME_PAT="${PACKAGEDIRNAME}[^/]*"

	base1=`pwd`
	
	package_path=`echo "$base1" |
		sed -e s@'\(.*'"${PACKAGEDIRNAME_PAT}"'\).*@\1@'` 

	package_dir=`echo "$base1" |
		sed -e s@'.*/\('"${PACKAGEDIRNAME_PAT}"'\)@\1@' |
		sed -e s@'\('"${PACKAGEDIRNAME_PAT}"'\).*@\1@'`


	echo "$package_dir" | grep / 1>/dev/null
	case $? in
		0)
		echo "${premg}Try running from within the package directory." 1>&2
		echo "${premg}script failed loc = 1" 1>&2
		${RM} -fr "$TMPDIR"
		exit 1
		;;
	esac

	echo "$package_dir" | grep . 1>/dev/null
	case $? in
		1)
		echo "${premg}Try running from within the package directory." 1>&2
		echo "${premg}script failed loc = 2" 1>&2
		${RM} -fr "$TMPDIR"
		exit 1
		;;
	esac
	echo "$package_dir"
}

replace_keywords() {
	package="$1"
	name="${package%-*}"
	ver="${package##*-}"

	sed \
		-e 's/%__tag/'"$name"/ \
		-e 's/%__revision/'"$ver"/
}

thisPSF() {
	checksigname="$1"
	package="$2"

	name="${package%-*}"
	ver="${package##*-}"
	checksigkeyword="checksig <"

	if [ "$1" = "" ]; then
		CHECKSIGNAME=""
		checksigkeyword=""
	else
		CHECKSIGNAME="$checksigname"
	fi

	if [ ! "$no_file_permissions" -a "$opt_owner" -a "$opt_group" ]; then
		FILEPERMS="file_permissions -o $opt_owner  -g $opt_group"
	else
		FILEPERMS=""
	fi

	cat  <<-//EOF
	distribution
		dfiles dfiles
		${checksigkeyword}$CHECKSIGNAME
	product
		title $name version $ver
		description Source package for $name version $ver
		tag ${name}
		revision ${ver}
		control_directory ""
	fileset
		tag ${name}-sources
		control_directory ""
		$FILEPERMS
		directory .
		file *
		exclude catalog
	//EOF
}

#process_psf "$opt_source" "$package_dir" "$opt_checksig_file"

process_psf() {
	opt_source="$1"
	package_dir="$2"
	opt_checksig_file="$3"

	case "$opt_source" in
		"")
			thisPSF "$opt_checksig_file" "$package_dir" |
			replace_keywords "$package_dir"  >$TMPSOURCEFILE
			opt_source=$TMPSOURCEFILE
			;;
		-)
			cat | replace_keywords "$package_dir" >$TMPSOURCEFILE
			opt_source=$TMPSOURCEFILE
			;;
		*)
			if [ -f "$opt_source" ]; then
				cat "$opt_source" | replace_keywords "$package_dir" >$TMPSOURCEFILE
				opt_source=$TMPSOURCEFILE
			else
				echo "swign: no such file: $opt_source" 1>&2
				exit 1;
			fi
			;;
	esac
}

make_archive_stream() {
	(
	cd .. 
	if [ "$opt_owner" -a "$opt_group" ]; then
		# TAROWNEROPT="--owner=\"$opt_owner\" --group=\"$opt_group\""
		TAROWNEROPT="--owner=$opt_owner --group=$opt_group"
	else
		TAROWNEROPT=""
	fi
	case "$opt_format" in
		ustar|ustar0)
			${TAR} cvf - ${GNUTAR_FORMAT_OPTIONS} ${GNUTAR_POSIX_OPTION} ${GNUTAR_NUMERIC_OPTION} -b1 \
			--files-from=- --no-recursion $TAROWNEROPT 2>$TMPLIST
			;;
		gnutar|oldgnu|gnu)
			${TAR} cvf - ${GNUTAR_FORMAT_OPTIONS} ${GNUTAR_POSIX_OPTION} ${GNUTAR_NUMERIC_OPTION} -b1 \
			--files-from=- --no-recursion $TAROWNEROPT 2>$TMPLIST
			;;
		bsdpax3)
			if [ ! -f "$PAX" ]; then
				echo "swign: pax is missing." 1>&2
				echo;
			else
				${PAX} -w -v -d -b 512 2>$TMPLIST
			fi
			;;
		*)
			>$TMPLIST
			echo "swign: invalid format spec" 1>&2
			exit 1
			;;
	esac
	)
}

check_for_gnu_tar()
{
	gnu_tar_version="`$TAR --version 2>&1 | $GREP GNU | $HEAD -1 | $AWK '{print $NF}'`"
	case "$gnu_tar_version" in 
		2.[0-9].*)
			echo 1>/dev/null
		;;
#		1.14*)
#			echo "swign: GNU tar version 1.13.17, 1.13.25 or 1.15.x is required for this test" 1>&2
#			echo "swign: You have GNU tar version [$gnu_tar_version]" 1>&2
#			echo "swign: swbis does not suppport [$gnu_tar_version]" 1>&2
#			echo "swign: Exiting with status 1" 1>&2
#			return 1
#		;;
		1.14|1.14.*|1.15*|1.13.17|1.13.25)
			echo 1>/dev/null
		;;
		*)
		echo "GNU tar version 1.13.17,25 or 1.15.x is required" 1>&2
		echo "You have tar version $gnu_tar_version" 1>&2
		return 1
		;;
	esac
	return 0
}

make_format_option()
{
	case "$gnu_tar_version" in 
		1.14|1.14.*|1.15|1.15.*)
			case "$opt_format" in
				gnu)
					GNUTAR_FORMAT_OPTIONS="--format=gnu"
					SWPACKAGE_FORMAT_OPTIONS="--format=gnu"
					;;
				gnutar|oldgnu)
					echo "swign: oldgnu format not supported by swign for GNU tar $gnu_tar_version" 1>&2 
					return 1
					GNUTAR_FORMAT_OPTIONS="--format=oldgnu"
					SWPACKAGE_FORMAT_OPTIONS="--format=oldgnu"
					;;
				ustar)
					GNUTAR_FORMAT_OPTIONS="--format=ustar"
					SWPACKAGE_FORMAT_OPTIONS="--format=ustar"
					;;
 				ustar0)
 					echo "swign: ustar0 format not supported by swign for GNU tar $gnu_tar_version" 1>&2 
 					return 1
 					;;
			esac
		;;
		1.13.*)
			case "$opt_format" in
				gnu)
					echo "swign: gnu format not supported by swign for GNU tar $gnu_tar_version" 1>&2 
					return 1
					;;
				gnutar|oldgnu)
					GNUTAR_FORMAT_OPTIONS=""
					SWPACKAGE_FORMAT_OPTIONS="--format=oldgnu"
					;;
				ustar|ustar0)
					GNUTAR_FORMAT_OPTIONS="--posix"
					SWPACKAGE_FORMAT_OPTIONS="--format=ustar0"
					;;
			esac
		;;
		*)
			GNUTAR_FORMAT_OPTIONS=""
			echo "swign: internal error in make_format_options" 1>&2
			return 1
		;;
	esac
	return 0
}

#
#  Main starts here 
#

if [ "$1" = "--help" ]; then
	usage; exit 0;
fi

case "$GTAR" in "") GTAR=tar; ;; esac

MD5SUM=`which md5sum 2>/dev/null`
DIFF=`which diff` || exit 1
TEE=`missing_which tee` || exit 1
TAR=`missing_which $GTAR` || exit 1
GZIP=`missing_which gzip` || exit 1
SED=`missing_which sed` || exit 1
SORT=`missing_which sort` || exit 1
RM=`missing_which rm` || exit 1
CAT=`missing_which cat` || exit 1
GREP=`missing_which grep` || exit 1
FIND=`missing_which find` || exit 1
MKDIR=`missing_which mkdir` || exit 1
AWK=`missing_which awk` || exit 1
ID=`missing_which id` || exit 1
PAX=`missing_which pax 2>/dev/null`
HEAD=`missing_which head` || exit 1
OPATH="$PATH"
PATH="${SWBISLIBEXECDIR}/swbis:$OPATH"
MTIMETOUCH=`missing_which mtimetouch` || exit 1
EPOCHTIME=`missing_which epochtime` || exit 1
GETOPT=`missing_which getopt` || exit 1
PATH="$OPATH"
OPENSSL=`which openssl 2>/dev/null`

if [ ! -f "$MD5SUM" ]; then
	if [ -f "$OPENSSL" ]; then
		MD5SUM="openssl md5"	
	else
		echo "swign: missing md5sum and openssl, no way to run this program: $0." 1>&2
		echo "$0 exiting with status 1" 1>&2
		exit 1
	fi
fi

check_for_gnu_tar
case $? in
	0)
		;;
	*)
		echo "swign: suitable GNU tar not found" 1>&2
		echo "swign: Try looking for gtar" 1>&2
		TAR=`missing_which gtar` || exit 1
		check_for_gnu_tar || exit 1
		;;	
esac

GNUTAR_NUMERIC_OPTION=""
opt_source=""
opt_testmode=""
opt_showmode=""
opt_signthis=""
opt_checksig=""
opt_localuser=""
opt_homedir=""
opt_psf=""
opt_nosign="-Wnoop"
opt_format="ustar"
copt_owner_set=""
copt_group_set=""
no_file_permissions=""

o_opt_owner=`${ID} | ${AWK} 'BEGIN { FS=" " } { gsub(/^.*\(/, "",  $1); gsub(/\).*/, "",  $1); print $1}'`
o_opt_group=`${ID} | ${AWK} 'BEGIN { FS=" " } { gsub(/^.*\(/, "",  $2); gsub(/\).*/, "",  $2); print $2}'`

odir_opt_owner=`ls -ld . | ${AWK} 'BEGIN { FS=" " } { print $3}'`
odir_opt_group=`ls -ld . | ${AWK} 'BEGIN { FS=" " } { print $4}'`

opt_owner="$o_opt_owner"
opt_group="$o_opt_group"
c_opt_owner="$o_opt_owner"
c_opt_group="$o_opt_group"

${GETOPT} -T 1>/dev/null
case "$?" in
	4)
		;;
	*)
		echo "swign: This script requires enhanced getopt." 1>&2
		exit 1
		;;
esac

TEMP=`${GETOPT} -o VHtTSD:PPu:h:x:o:g:Ns:QQZ: \
--long version,help,run-sanity-test,show-names-only,sign-this,with-checksig:,psf,show-psf,local-user:,homedir:,format:,owner:,group:,nosign,source:no-psf-detect,no-psf,reset-permissions: \
     -n 'swign' -- "$@"`
# echo "$TEMP" 1>&2 

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

TMPDIR=/var/tmp/swign.$$
TMPSOURCEFILE=/var/tmp/swign.$$/PSF
(umask 077; ${MKDIR} "$TMPDIR" 2>/dev/null)

#
# Determine package name which is determined from the
# current directory.
#
package_dir=`get_package_dirname`
case "$package_dir" in
	.|..|?|??)
		echo "swign: package directory name not valid: $package_dir" 1>&2
		${RM} -fr "$TMPDIR"
		exit 1
		;;
	"")
		echo "swign: package directory name not valid: $package_dir" 1>&2
		${RM} -fr "$TMPDIR"
		exit 1
		;;
	*)
		;;
esac

did_file_permissions_rescan=""
while true ; do
	case "$1" in
		-t|--run-sanity-test) opt_testmode="x"; shift ;;
		-T|--show-names-only) opt_showmode="x"; shift ;;
		-S|--sign-this) opt_signthis="x"; shift ;;
		-Q|--no-psf-detect) did_file_permissions_rescan=x; shift ;;
		-P|--psf|--show-psf) opt_psf="x"; shift ;;
		-u|--local-user)  shift ; opt_localuser="$1"; shift;;
		-N|--nosign)  opt_nosign="-Wnosign"; shift;;
		-o|--owner)  shift ; copt_owner_set=x; opt_owner="$1"; c_opt_owner="$opt_owner"; shift;;
		-Z|--reset-permissions)  shift; copt_owner_set=""; copt_group_set=""; shift;;
		-g|--group)  shift ; copt_group_set=x; opt_group="$1"; c_opt_group="$opt_group"; shift;;
		-s|--source)  shift ; opt_source="$1"; shift;;
		-x|--format)  shift ; opt_format="$1"; shift;;
		-h|--homedir)  shift ; opt_homedir="$1"; shift;;
		-D|--with-checksig) opt_checksig="x"; shift ; opt_checksig_file="$1"; shift;;
		-H|--help) usage; exit 0; shift ;;
		-V|--version) version; exit 0 ;;
		--) shift ;
			#
			# Read the PSF to determine the ownerships policy
			# in order to eliminate the requirement the user
			# correctly specify it on the command line.
			#
			if [ "$did_file_permissions_rescan" ]; then
				break
			fi
			if [ "$copt_owner_set" -o "$copt_group_set" ]; then
				#
				# If the -o or -g option was given then dont do this scan of the
				# PSF file.
				#
				break
			fi
			did_file_permissions_rescan="x"

			#
			# The PSF file name is in "$opt_source"
			#
			process_psf "$opt_source" "$package_dir" "$opt_checksig_file"
 
			file_perms_from_psf=`cat "$opt_source" | look_for_file_permissions`
			TEMP=`${GETOPT} -o o:g:Z: --long owner:,group:,reset-permissions: -n 'swign' -- $file_perms_from_psf "$@"`
			if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
			eval set -- "$TEMP"
			;;
		*) echo "$0 Internal error" ; exit 1 ;;
	esac
done

# Finished processing optios

c_opt_owner=${opt_owner:-$o_opt_owner}
c_opt_group=${opt_group:-$o_opt_group}

if [ "$copt_owner_set" -a  ! "$opt_owner" ]; then
	no_file_permissions="true"
	opt_owner="$odir_opt_owner"
	c_opt_owner="$odir_opt_owner"
fi

if [ "$copt_group_set" -a  ! "$opt_group" ]; then
	no_file_permissions="true"
	opt_group="$odir_opt_group"
	c_opt_group="$odir_opt_group"
fi

if [ "$no_file_permissions" ]; then
	c_opt_owner=""
	c_opt_group=""
fi

case "$opt_psf" in
	"")
		;;
	*)
	#
	# Show the PSF to stdout and exit
	#
	case "$opt_source" in
	"")
		thisPSF "$opt_checksig_file" "$package_dir" 
		;;
	-)
		cat
		;;
	*)
		if [ -f "$opt_source" ]; then
			cat "$opt_source"
		else
			echo "swign: no such file: $opt_source" 1>&2
			cat /dev/null
			exit 1;
		fi
		;;
	esac
	${RM} -fr "$TMPDIR"
	exit 0
	;;
esac

echo "$opt_owner" | egrep -v -e '^[0-9][0-9]*$' 1>/dev/null
owner_is_numeric=$?
echo "$opt_group" | egrep -v -e '^[0-9][0-9]*$' 1>/dev/null
group_is_numeric=$?

if [ $owner_is_numeric -eq 1 -o $group_is_numeric -eq 1 ]; then
	GNUTAR_NUMERIC_OPTION="--numeric-owner"
fi

SWPACKAGE=`missing_which swpackage` || exit 1
SWVERIFY=`missing_which swverify` || exit 1

if [ -z "$SWPACKAGE" -o -z "$SWVERIFY" ]; then
	echo "Missing utilities swverify or swpackage" 1>&2
	echo "swpackage = [$SWPACKAGE]" 1>&2
	echo "swverify = [$SWVERIFY]" 1>&2
	exit 1
fi

if [ $# -lt 1 -a "$opt_signthis" = "" -a "$opt_testmode" = "" -a "$opt_showmode" = "" ]; then
	cmdusage 1>&2
	exit 1
fi

if [ $# -ge 1 -a "$opt_signthis" ]; then
	cmdusage 1>&2
	exit 1
fi

if [ $# -ge 1 -a "$opt_signthis" = "" ]; then
	case "$1" in
		@.)
			opt_signthis=x
			;;
		@-)
			;;
		@*)
			cmdusage 1>&2
			exit 1
			;;
		*)
			cmdusage 1>&2
			exit 1
			;;		
	esac
fi

if [ "$opt_localuser" ]; then
	AUTHOR="$opt_localuser"
	w_opt_author="-Wgpg-name=${AUTHOR}"
else
	AUTHOR=""
	w_opt_author="-Wgpg-name="
fi

if [ "$opt_homedir" ]; then
	gpg_homedir="$opt_homedir"
else
	if [ -n "$GNUPGHOME" ]; then
		gpg_homedir="$GNUPGHOME"
	else
		gpg_homedir="~/.gnupg"
	fi
fi

DFILES=dfiles
TMPLIST=/var/tmp/swign.$$/tarlist.eraseme
TMPLIST2=/var/tmp/swign.$$/listcheck.eraseme.sorted
TMPTIME=/var/tmp/swign.$$/timeref
#TMPSOURCEFILE=/var/tmp/swign.$$/PSF

if [ -z "$opt_testmode" ]; then
	TMPLIST=/dev/null
fi

(umask 077; ${MKDIR} "$TMPDIR" 2>/dev/null)

#
# These variables have to be consistent.
SWPACKAGE_FORMAT_OPTIONS="--format=$opt_format"
GNUTAR_POSIX_OPTION=""

caltime=`$EPOCHTIME`

#pdir=`pwd`
#REVISION="`echo $pdir | ${SED} -e 's@^.*-\([^-]*\)@\1@'`"
#export REVISION


PACKAGEDIR="$package_dir"
cd ..
if [ ! -d "${PACKAGEDIR}" ]; then
	echo "script failed : dir ${PACKAGEDIR} not found." 1>&2
	${RM} -fr "$TMPDIR"
	exit 1
fi

if [ -h "${PACKAGEDIR}" ]; then
	echo "script failed : ${PACKAGEDIR} is a symlink." 1>&2
	echo "this script does not support this." 1>&2
	${RM} -fr "$TMPDIR"
	exit 1
fi
	
cd "${PACKAGEDIR}"
case $? in
	0)
		;;
	*)
		echo "script error" 1>&2
		${RM} -fr "$TMPDIR"
		exit 3
		;;
esac

if [ "$opt_showmode" ]; then
	echo "Identity: $AUTHOR"
	echo "GPG homedir : $gpg_homedir"
	echo "Package dir : ${PACKAGEDIR}"
	echo "nothing done. exiting with status 0"
	exit 0
fi

#
# remove the old catalog.
#
${RM} -fr catalog
case $? in
	0)
		;;
	*)
		echo "script error /bin/rm failed." 1>&2
		exit 4
		;;
esac

#
# Here, in this usage just install the catalog section, then use swpackage to generate
# the file list and use as input into GNU tar.
#

SUFF=".swigntest"
serial_archive="../${PACKAGEDIR}${SUFF}.tar.gz"
echo "swign: Generating the catalog and installing with tar...   " 1>&2

if [ ! "$no_file_permissions" -a  "$opt_owner" -a "$opt_group" ]; then
	OWNEROPT="-W dir-owner=$opt_owner -W dir-group=$opt_group"
elif [ "$no_file_permissions" ]; then
	OWNEROPT="-W dir-owner= -W dir-group="
else
	OWNEROPT=""
fi

if [ "$c_opt_owner" -a "$c_opt_group" ]; then
	CATALOG_OWNEROPT="-W catalog-owner=$c_opt_owner -W catalog-group=$c_opt_group"
else
	CATALOG_OWNEROPT=""
fi

## echo "OWNEROPT=$OWNEROPT" 1>&2

process_psf "$opt_source" "$package_dir" "$opt_checksig_file"

#
# Set the format options for swpackage and GNU tar
#

make_format_option
case $? in 
	0)
		;;
	*)
		${RM} -fr "$TMPDIR"
		exit 1
		;;
esac
 
${SWPACKAGE} -s "$opt_source" \
		-W sign \
		"$opt_nosign" \
		$OWNEROPT \
		$GNUTAR_NUMERIC_OPTION \
		-W dir-mode=. \
		$CATALOG_OWNEROPT \
		-W digests \
		${SWPACKAGE_FORMAT_OPTIONS} \
		-W files \
		-W create-time="${caltime}" \
		-W dir="${PACKAGEDIR}" \
		"${w_opt_author}" \
		-W gpg-path="${gpg_homedir}" \
		@- \
	| (cd ..; ${TAR} xpf - "${PACKAGEDIR}"/catalog) 1>&2

	case $? in
		0)
			;;
		*)
			echo "tar failed." 1>&2
			${RM} -fr "$TMPDIR"
			exit 1
			;;
	esac

	#
	# And here's the trick to make the signature work:
	# 	touch -t "$create_date" .
	#
	# But since GNU and BSD folks haven't yet agreed on option letters
	# for touch(1), a special program is needed to emulate
	# the command: touch -t "$create_date" . that works for either.
	#
	${MTIMETOUCH} "$caltime" .

	if [ "$opt_signthis" ]; then
		exit 0
	fi

	if [ "$opt_testmode" ]; then
		echo "Generating ${serial_archive} using tar ...     " 1>&2
	fi
	#
	# Now check the file list to make sure no files are missing.
	#
	check_files "${PACKAGEDIR}" catalog/dfiles/files
	case "$?" in
		0)
			;;
		*)
			echo "swign: The swbis utility, swpackage, failed, please report this." 1>&2
			${RM} -fr "$TMPDIR"
			exit 1
			;;
	esac
	cat catalog/dfiles/files | make_archive_stream |
	(
	if [ "$opt_testmode" ]; then
		${GZIP} -n >"${serial_archive}"
		echo "${serial_archive} Written" 1>&2
	else 
		dd bs=512 2>/dev/null
	fi
	)	

if [ -z "$opt_testmode" ]; then
	#
	# Normal Usage, Exit here.
	#
	TMPDIR=/var/tmp/swign.$$
	${RM} -fr "$TMPDIR"
	exit 0
fi

# End.  for default usage.
##############################################################################
# Here if --run-sanity-test 
#
# Done.
# Now do some sanity checks.
#
# Now check that the catalog/<dfiles>/files is correct and agrees with the current directory contents as
# determined by the $FIND command.
#

	printf "swign: Checking file list generated by swpackage ... " 1>&2
	(cd ..;  ${CAT} "${PACKAGEDIR}"/catalog/${DFILES}/files) | ${SED} -e 's@/$@@' | ${SORT} >$TMPLIST2
	(cd ..; ${FIND} "${PACKAGEDIR}") | ${SED} -e 's@/$@@' | ${SORT} | ${DIFF} -u - $TMPLIST2
	case $? in
		0)
			echo "  OK" 1>&2
			;;
		*)
			${RM} -fr "$TMPDIR"
			echo " FAILED." 1>&2
			exit 1
			;;
	esac

	#
	# Now check that the tar stderr file output has all the files in it.
	#
	printf "swign: Checking tar list against current directory content ... " 1>&2
	(cd ..; ${CAT} <$TMPLIST | ${GREP} -v -e '^pax: .*vol ' | ${SED} -e 's@/$@@') | ${SORT} >${TMPLIST}.sorted
	(cd ..; ${FIND} "${PACKAGEDIR}") | ${SORT} |  diff -u - ${TMPLIST}.sorted
	case $? in
		0)
			echo " OK" 1>&2
			;;
		*)
			${RM} -fr "$TMPDIR"
			echo " FAILED" 1>&2
			exit 1
			;;
	esac
	
	echo "swign: Now running swverify" 1>&2

TMPDIR=/var/tmp/swign.$$
${RM} -fr "$TMPDIR"

${SWVERIFY} --checksig ${serial_archive}
ret=$?
echo "The test archive currently resides at: ${serial_archive}" 1>&2
exit $ret
