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

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

manfile="$1"; shift
manfile="$(readlink -ev "$manfile")"

fatal() {
	printf '%s\n' "${0##*/}: $*" >&2
	exit 1
}

get_charset()
{
	local l
	read -r l
	case "$l" in
		'.\" -*- mode: troff; coding: '*'-*-') ;;
		*) return 0 ;;
	esac
	set $l
	printf '%s' "$6"
}

mandir=
for d in `manpath |tr : \ `; do
	if [ -z "${manfile##$d/*}" ]; then
		mandir="$d"
		break
	fi
done

[ -n "$mandir" ] ||
	fatal "$f: man-page not in manpath"

def_charset=
[ ! -s "$mandir/.charset" ] ||
	def_charset="$(cat "$mandir/.charset")"

charset="$(man-source "$manfile" |get_charset)"
[ -n "$charset" ] ||
	charset="$def_charset"

ICONV=/usr/bin/iconv
[ -x "$ICONV" ] ||
	fatal "$ICONV: not found or not executable"

iconv_cmd=cat
[ -z "$charset" ] ||
	iconv_cmd="$ICONV -f $charset -t utf-8"

man-source "$manfile" |
	$iconv_cmd
