#!/bin/sh
#
# $NetBSD: doc-compress,v 1.3 2006/01/19 17:22:26 jlam Exp $
#
# This script is derived from software contributed to The NetBSD Foundation
# by Alistair Crooks.
#
# This script compresses or decompresses files listed in standard input.
# It handles symlinks by recreating the symlinks to point to the
# compressed or uncompressed targets.
#

######################################################################
#
# NAME
#	doc-compress -- handle compression of PLIST entries
#
# SYNOPSIS
#	doc-compress [-v] [-z] prefix
#
# DESCRIPTION
#	doc-compress handles compression of files passed in via standard
#	input.  The file paths must be relative to the specified prefix.
#	doc-compress handles symlinks to compressed files intelligently by
#	symlinking to the compressed target if compression is desired, and
#	similarly for decompression.
#
# OPTIONS
#	The following command line arguments are supported.
#
#	-v	Output the action taken for each file (compressing,
#		decompressing, or symlinking) to standard output.
#
#	-z	Compress the files.  By default, the files are decompressed.
#
# ENVIRONMENT
#	MANZ	This variable controls the default action taken.  If "yes",
#		then this is equivalent to specifying the "-z" option.
#
#	PKG_VERBOSE
#		This controls the default verbosity of the output.  If
#		non-empty, then this is equivalent to specifying the "-v"
#		option.
#
######################################################################

: ${ECHO=echo}
: ${EXPR=expr}
: ${GZIP_CMD=gzip}
: ${GUNZIP_CMD=gunzip}
: ${LN=ln}
: ${LS=ls}
: ${RM=rm}
: ${TEST=test}

self="${0##*/}"

usage() {
	${ECHO} 1>&2 "usage: $self [-v] [-z] prefix"
}

compress=no
verbose=no
prefix=/nonexistent

case "$MANZ" in
[yY][eE][sS])	compress=yes ;;
esac
case "$PKG_VERBOSE" in
"")	;;
*)	verbose=yes ;;
esac

# Process optional arguments
while ${TEST} $# -gt 0; do
	case "$1" in
	-v)	verbose=yes; shift ;;
	-z)	compress=yes; shift ;;
	--)	shift; break ;;
	-*)	${ECHO} 1>&2 "$self: unknown option -- ${1#-}"
		usage
		exit 1
		;;
	*)	break ;;
	esac
done

${TEST} $# -gt 0 || { usage; exit 1; }

# Process required arguments
prefix="$1"

while read file; do
	file="${file%.gz}"
	path="$prefix/$file"
	pathgz="$path.gz"
	case "$compress" in
	yes)
		# If compressed pages were requested and we find an
		# uncompressed page, then compress it, but if it was
		# a symlink, then remove it and create a "compressed"
		# symlink by symlinking to the compressed target.
		#
		if ${TEST} -h "$path"; then
			target=`${LS} -l $path`
			target="${target##*-> }"
			${RM} -f $pathgz
			${LN} -s $target.gz $pathgz
			${RM} -f $path
			${TEST} "$verbose" = no ||
				${ECHO} "Symlinking:    $file"
		elif ${TEST} -f "$path"; then
			${GZIP_CMD} -nf $path
			${TEST} "$verbose" = no ||
				${ECHO} "Compressing:   $file"
		fi
		;;
	no)
		# If uncompressed pages were requested and we find a
		# compressed page, then decompress it, but if it was
		# a symlink, then remove it and create an "uncompressed"
		# symlink by symlinking to the uncompressed target.
		#
		if ${TEST} -h "$pathgz"; then
			target=`${LS} -l $pathgz`
			target="${target##*-> }"
			target="${target%.gz}"
			${RM} -f $path
			${LN} -s $target $path
			${RM} -f $pathgz
			${TEST} "$verbose" = no ||
				${ECHO} "Symlinking:    $file.gz"
		elif ${TEST} -f "$pathgz"; then
			${GUNZIP_CMD} -f $pathgz
			${TEST} "$verbose" = no ||
				${ECHO} "Decompressing: $file.gz"
		fi
		;;
	esac
done