dnl CF_INSTALL_MAN version: 6 updated: 2024/04/01 04:21:53
dnl --------------
dnl Call this to generate a script "install-man" which uses the detected
dnl manpage-format to compress the resulting manpage.  Providing this as a
dnl macro simplifies including the script in different programs.
dnl
dnl The generated script assumes that the program can be renamed, using two
dnl symbols in the manpage, e.g.,
dnl		.ds N Yacc
dnl     .ds n yacc
dnl which are later used as \*N and \*n, as well as a special case in the
dnl synopsis to work around limitations of makewhatis.
dnl
dnl Script parameters:
dnl	$1 = input file
dnl	$2 = output filename
dnl
dnl or (uninstall):
dnl	$1 = output filename
AC_DEFUN([CF_INSTALL_MAN],
[
AC_REQUIRE([AC_ARG_PROGRAM])
AC_REQUIRE([AC_PROG_INSTALL])
AC_REQUIRE([AC_LN_S])
AC_REQUIRE([CF_MANPAGE_FORMAT])
cf_install_man=install-man
ac_config_files="$ac_config_files $cf_install_man"
cf_install_man=$cf_install_man.in
echo [$]as_me: creating $cf_install_man
cat >[$]cf_install_man <<"CF_EOF"
#!/bin/sh
# generated by CF_INSTALL_MAN

LANG=C;     export LANG
LC_ALL=C;   export LC_ALL
LC_CTYPE=C; export LC_CTYPE
LANGUAGE=C; export LANGUAGE

INSTALL="@INSTALL@"
INSTALL_DATA="@INSTALL_DATA@"

usage() {
	cat <<EOF
Usage: install-man [[-l]] [[source]] target
EOF
	exit 1
}

failed() {
	echo "?? [$]*" >&2
	exit 1
}

test [$]# != 0 || usage
OPTS=
case "x[$]1" in
(x-l)
	OPTS="link"
	shift
	;;
(x-*)
	usage
	;;
esac

source=
if test [$]# = 2 ; then
	source=[$]1; shift
	target=[$]1
elif test [$]# = 1 ; then
	test -n "$OPTS" && usage
	target=[$]1
else
	usage
fi

origin_name=`echo "$source" |sed -e 's%^.*/%%' -e 's%\..*%%'`
actual_name=`echo "$origin_name" |sed '@program_transform_name@'`
leading_cap=`echo "$actual_name" | sed -e 's%^\(.\).*$%\1%' | tr a-z A-Z``echo "$actual_name" | sed -e 's%^.%%'`
capitalized=`echo "$actual_name" | tr a-z A-Z`

cf_tmpdir=`mktemp -d`
trap 'rm -rf "$cf_tmpdir"; exit 1' 1 2 3 15
trap 'rm -rf "$cf_tmpdir"; exit 0' 0

if test -n "$source" ; then
	suffix=
	test -n "@cf_manpage_so_strip" && suffix=".@cf_manpage_so_strip@"
	if test "x$OPTS" = xlink ; then
		source_dir=`echo "$source" | sed -e "s%/[[^/]]*$%%"`
		target_dir=`echo "$target" | sed -e "s%/[[^/]]*$%%"`
		sourcelink="${source}${suffix}" 
		targetfile="${target}${suffix}" 
		targetlink="${target_dir}/${sourcelink}"
		if test ! -d "$target_dir" ; then
			failed "target directory does not exist: $target_dir"
		elif test ! -f "$targetfile" ; then
			failed "target file does not exist: $targetfile"
		elif test "$source" != "$source_dir" ; then
			failed "unexpected directory for source-link: $source_dir"
		fi
		test -f "$targetlink" && failed "already exists $targetlink"
		( cd "$target_dir" && @LN_S@ "`echo "$targetfile" | sed -e 's%^.*/%%'`" "$sourcelink" )
		test -f "$targetlink" || failed "cannot create $targetlink"
		target="$targetlink"
	else
		echo "** installing $source to $target"
		interim="$cf_tmpdir"/"`basename $source`"
		if test "x$origin_name" != "x$actual_name" ; then
			sed \
				-e "/^.ds N/s%N.*%N $leading_cap%" \
				-e "/^.ds n/s%n.*%n $actual_name%" \
				-e "/^\.TH/s%[[ ]][[ ]]*[[^ ]][[^ ]]*% $capitalized%" \
				-e "/^\.SH[[ ]][[ ]]*NAME/,/[[ ]]\\\\-[[ ]]/s%^\\\\\\*[[Nn]]%$actual_name%" \
				"$source" >"$interim" || exit 1
			diff -c "$source" "$interim"
		else
			cp "$source" "$interim" || exit 1
		fi
		if test -n "@cf_manpage_compress@" ; then
			@cf_manpage_compress@ @cf_manpage_coptions@ "$interim"
			source="${interim}${suffix}"
		fi
		if test -d "$target" ; then
			target="$target"/"$source"
		else
			test -n "@cf_manpage_compress@" && target="${target}.@cf_manpage_so_strip@"
		fi
		$INSTALL_DATA "$source" "$target" || exit 1
	fi
	echo "...installed $target"
else
	echo "** removing $target"
	test -n "@cf_manpage_compress@" && target="${target}.@cf_manpage_so_strip@"
	if test -f "$target" ; then
		rm -f "$target"
		echo "...removed $target"
	else
		echo "...not found"
	fi
fi
exit 0
CF_EOF
])dnl
