:
#	@(#) repackman 23.3 91/12/12 
#
#	Copyright (C) 1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	The 3.2v4 OS MAN pages are supplied in compress -H format,
#	and the 3.2v4 /usr/bin/man uses uncompress to read .Z pages.
#	However, earlier versions of /usr/bin/man, /usr/bin/xman and
#	/usr/bin/uncompress do not support this format.  If required,
#	use /usr/man/bin/repackman to convert them to packed format,
#	or to convert packed MAN pages to compress -H format (which
#	occupies less disk space), or to unpack and uncompress MAN pages.
#
#	If there are two or three versions of a file e.g. one compressed
#	and one packed, then the older versions are silently removed.
#
#	Note: use of repackman may cause fully installed MAN packages 
#	to be reported by custom or fixperm as partially installed,
#	since the filename extensions are changed.

usage()
{
	echo "usage: repackman -z | -Z | -" >&2
	echo "       -z  converts pages on MANPATH to pack(C) format" >&2
	echo "       -Z  converts pages on MANPATH to compress(C) -H format" >&2
	echo "       -   converts pages on MANPATH to unpacked uncompressed format" >&2
	exit 1
}

case $#$1 in
1-z)	op=pack		;;
1-Z)	op=compress	;;
1-)	op=""		;;
*)	usage		;;
esac

# check that the right version of compress is in place
compress -H </dev/null >/dev/null
[ $? -eq 1 ] && {
	echo "repackman: needs version of compress supporting -H" >&2
	exit 1
}
cwd=`pwd` || {
	echo "repackman: pwd failed" >&2
	exit 1
}

[ -n "$MANPATH" ] || eval `grep MANPATH= /etc/default/man 2>/dev/null`
[ -n "$MANPATH" ] || MANPATH=/usr/man

for mandir in `echo $MANPATH | sed "s/:/ /"`
do
	cd $cwd
	[ -d "$mandir" -a -x "$mandir" -a -r "$mandir" ] || {
		echo "$mandir is not an accessible directory" >&2
		continue
	}
	echo "Blocks Before = `du -s $mandir`"
	cd $mandir
	dirs=`ls -d cat.* man.* 2>/dev/null`

	for dir in $dirs
	do
		cd $cwd
		cd $mandir
		[ -d "$dir" -a -x "$dir" -a -r "$dir" -a -w "$dir" ] || {
			echo "$mandir/$dir is not an accessible directory" >&2
			continue
		}
		cd $dir

		# use ls -t so that we can remove older duplicates
		ls -t | sed 's/\(.*\)/\1 \1/;s/\.[zZ]$//' |
		while read file noext
		do
			# duplicate may have been removed already
			[ -f $file ] || continue

			case $file in
			*.z)	unop="pack"
				rm -f $noext $noext.Z	;;
			*.Z)	unop="compress"
				rm -f $noext $noext.z	;;
			*)	unop=""
				rm -f $noext.z $noext.Z	;;
			esac

			[ "$unop" = "$op" ] && {
				echo "$mandir/$dir/$file unchanged"
				continue
			}

			# pack/unpack behaves badly on interrupt
			trap '' 1 2 3 15
			[ -n "$unop" ] && {
				un$unop $file >/dev/null 2>&1 || {
					echo "$mandir/$dir/$file cannot be un${unop}ed"
					continue
				}
				unop=" un${unop}ed"
			}

			doneop=""
			case $op in
			pack)	  $op $noext >/dev/null 2>&1 &&
					doneop=" ${op}ed"	;;
			compress) $op -H $noext >/dev/null 2>&1 && 
					doneop=" ${op}ed"	;;
			esac

			case "${unop}${doneop}" in
			"")	echo "$mandir/$dir/$file unchanged"	     ;;
			*)	echo "$mandir/$dir/${noext}${unop}${doneop}" ;;
			esac

			trap 'exit 1' 1 2 3 15
			sleep 1	# now let interrupt in
		done
	done

	echo "Blocks After = `du -s $mandir`"
done
