#!/bin/bash
#                       /usr/local/bin/help2man
# https://crystalfaeries.net/posix/bin/help2man
# celeste:crystalfaery HELP2MAN 2020-06-17 18:41:12+00:00
# generate local man pages for local scripts in /usr/local/bin
# with no arguments do all of /usr/local/bin scripts
# with one argument do that 1 /usr/local/bin script
# with two arguments do same as 1 argument, and use 2nd argument as source of code (no spaces)
let help=8	# number of this line - 1
cd /usr/local/bin || exit -1
case $# in
0)	# generate man pages for all scripts in /usr/local/bin
	echo "Enter ONLY "^D" in response to any sudo password request!" 1>&2
	echo "Do NOT authorize any sudos in other terminals while this runs!" 1>&2
	sudo -k	# expire any authorization for sudo priveleges
	for f in $( find . -type f \! -name '.??*' \! -name '*~' \! -name '*-' \! -size 0 | sort )
	do  case "$(basename ${f})" in
	HEADER.html | README.html | index.html | bbs.py | bookmarksbydate | chrontouch | data_source | dar2dvd | darlimit.fey | darlimit.pixy | df | di | rsnapshotlimit.faerie | rsnapshotlimit.fey | rsnapshotlimit.pixy )
		echo "$0: ${f} skipped" 1>&2
		;;
	*)	$0 "$( basename ${f} )"	# recurse to do this script
		;;
	    esac
	done
	find /usr/local/bin -name '.*.help2man.err' -size 0 -exec rm {} \;					# cleanup errors
	du   /usr/local/bin/.*.help2man.err | sort -nr > $HOME/crystalfaeries.net/src/.help2man.errs.txt	# notify the user
	exit $?
	;;
1)  case "${1}" in
    -v | --version)
	head -n 4 $0 | tail -n 1
	exit
	;;
    -h | --help)
	head -n $help $0
	exit
	;;
    *)	# generate man page for local script in /usr/local/bin
	/usr/bin/help2man --no-discard-stderr --name=${1} --section=1 --source="celeste:crystalfaery"	\
	--manual=${1} --output=/usr/local/man/man1/${1}.1 --no-info ${1} 2> .${1}.help2man.err
	exit $?
	;;
    esac
	;;
2)	# generate man page for local script in /usr/local/bin which came from elsewhere
	/usr/bin/help2man --no-discard-stderr --name=${1} --section=1 --source="${2}"			\
	--manual=${1} --output=/usr/local/man/man1/${1}.1 --info-page=${1} ${1} 2> .${1}.help2man.err
	exit $?
	;;
*)	# Say What?
	echo "$0 does not comprehend more than 2 arguments." 1>&2
	exit 1
	;;
esac
