#!/bin/sh -efu
# Generated automatically from man-source.in by the
# configure script.
#
#!/bin/sh -efu
#
# Helper to output man page source file. Based on man source code.
#
# Copyright (c) 2008 Alexey Gladkov <gladkov.alexey@gmail.com>
#

if [ "$#" -lt 1 ]; then
	printf 'Usage: %s <file> [<ll>] [<pl>]\n' "${0##*/}" >&2
	exit 1
fi

ll=0
pl=
fn="$1"; shift
[ "$#" -eq 0 ] || { ll="$1"; shift; }
[ "$#" -eq 0 ] || { pl="$1"; shift; }

uncompressors=
read_man_conf() {
	local suf cmd
	while read suf cmd; do
		[ -n "${suf##.*}" -o -z "$cmd" ] ||
			uncompressors="${uncompressors:+$uncompressors
}$suf $cmd"
	done < "/etc/man.conf"

	[ -z "${COMPRESS_EXT-}" -o -z "${DECOMPRESS-}" ] ||
		uncompressors="${uncompressors:+$uncompressors
}$COMPRESS_EXT $DECOMPRESS"
}

# Some HP systems have both man1 and man1.Z
# For man1.Z/file.1 let extp=".Z"
compat_manZ() {
	local d="${filename%/*}"
	if [ -z "${d##*.Z}" ]; then
		printf '%s\n' "$uncompressors" |
		while read suffix cmd; do
			[ "$suffix" = '.Z' ] ||
				continue
			$cmd "$filename"
			exit 1
		done || return 0
	fi
	return 1
}

unpackfile() {
	local filename="$1"

	printf '%s\n' "$uncompressors" |
	while read suffix cmd; do
		if [ -z "${filename##*$suffix}" ]; then
			$cmd "$filename"
			exit 1
		fi
	done || return 0

	! compat_manZ ||
		return 0

	exec cat "$filename"
}

readfile() {
	local l
	while IFS='
' read -r l; do
		expand_so_directive $l ||
			printf '%s\n' "$l"
	done
}

expand_so_directive() {
	if [ "${1-}" = '.so' ]; then
		shift
	elif [ "${1-}${2-}" = '.so' ]; then
		shift 2
	else
		return 1
	fi

	if [ "$#" -eq 0 ]; then
		printf '%s\n' "$fn: .so directive should have argument" >&2
		return 1
	fi

	local filename="$*"
	if [ -f "$filename" ]; then
		readfile < "$filename"
		return 0
	fi

	printf '%s\n' "$uncompressors" |
	while read suffix cmd; do
		if [ -f "${filename}${suffix}" ]; then
			$cmd "${filename}${suffix}" |readfile
			exit 1
		fi
	done || return 0

	! compat_manZ ||
		return 0

	return 1
}

showsource() {
	local l eof= first=
	while [ -z "$eof" ]; do
		IFS='
'		read -r l || eof=1
		if [ -z "$first" ]; then
			case "$l" in
				'.\" -*- mode: troff; coding: '*'-*-')
					printf '%s\n' "$l"
					first=1
					;;
			esac
			if [ "$ll" -gt 0 ]; then
				# We should set line length and title line length.
				# However, a .lt command here fails, only
				#  .ev 1; .lt ...; .ev helps for my version of groff.
				# The LL assignment is needed by the mandoc macros.
				printf '.ll %d.%di\n'    $(($ll/10)) $(($ll%10))
				printf '.nr LL %d.%di\n' $(($ll/10)) $(($ll%10))
				#printf '.lt %d.%di\n'   $(($ll/10)) $(($ll%10))
			fi
			if [ -n "$pl" ]; then
				printf '.pl %.128s\n' "$pl"
			fi
			[ -n "$first" ] ||
				printf '%s\n' "$l"
			first=1
			continue
		fi
		expand_so_directive $l ||
			printf '%s\n' "$l"
	done
	if [ -n "$first" -a "$pl" = "${MAN_VERY_LONG_PAGE-}" ]; then
	        # At end of the nroff source, set the page length to
		# the current position plus 10 lines.  This plus setpl()
		# gives us a single page that just contains the whole
		# man page. (William Webber, wew@cs.rmit.edu.au)
		printf '.\"\n'
		printf '.pl \\n(nlu+10\n'
	fi
}

read_man_conf
unpackfile "$fn" |showsource
