#!/bin/sh -e
# open info-page, man-page or command with help flag
# depends: unbuffer
# optdepends: highlight(dotfiles)
# TODO show tldr page
# TODO extract help in standard format from scripts
paginate="${PAGER:-less} +Gg"
case "$1" in
	(-d) browse=1; shift;;
	(-v) set -o xtrace; shift;;
	("") echo "Provide an object to get help on or check out the following options:
		tldr
		info
		man
		quickref; test-colors; arg-test / arg-notify"; exit 2;;
esac

showinfo() {
	which info >/dev/null 2>&1 &&
		case $EDITOR in
			(*emacs*) info "$@" -w | grep -v "manpages" | grep -q . && $EDITOR --eval "(progn (info \"$1\") (if \"$2\" (Info-index \"$2\")) (delete-other-windows))";;
			(*vim) info "$@" -w | grep -q . && $EDITOR -R -M -c "Info $1" +only;;
			(*) info -f "$1" $(test "$2" && echo "--index-search=$2") 2>/dev/null;;
		esac
}

cmd="$1"
case "$cmd" in
	(fwupdmgr|hunt|rdoc|gh|chordpro|pdfjam|reflector|topgrade|r128gain|7z|kubectl|diffr|docker|jrnl|difft|wiked-diff|qpdf|ninja|qobuz-dl|beet)
		unbuffer "$@" --help | sed 's|^[^ ].*:|\0|' | $paginate;;
	(sqlcmd) sqlcmd -?;;
	(exa) "$0" eza;;
	(caddy|stretchly|go|flutter)
		shift
		"$cmd" help "$@" | $paginate;;
	(doom|sgpt) "$@" --help;; # Paginates itself
	(rsgain) "$@" custom --help;;
	(spectre|plantuml|java) unbuffer "$@" -help | $paginate;;
	(mpw) "$@" -h 2>&1 | $paginate;;
	(bat) { unbuffer "$@" --help && printf "\n\nLANGUAGES:\n" && "$@" --list-languages --color always; } | $paginate;;
	(rails) { "$@" -H && "$@" --help; } | $paginate;;
	(vlc) shift && unbuffer vlc --full-help "$@" | $paginate;;
	(kdeconnect*) shift && kdeconnect-cli --help-all "$@" | $paginate;;
	# Show manpage with tweaks
	(fossil)
		# TODO view fossil ui in terminal
		if test $# -eq 1 && repo="$(locate -b -l 1 "fossil*.fossil")"
		then fossil ui --page=doc/trunk/www/permutedindex.html "$repo"
		else shift; fossil help "$@"
		fi;;
	(pandoc) man pandoc; highlight "Extensions" && pandoc --list-extensions | paste -s -d' ';;
	(swaymsg) test $# -gt 1 && shift && man sway "$@" || man swaymsg;;
	(swaymsg-*) man sway "${1#swaymsg-}";;
	(zsh|zmv) # https://unix.stackexchange.com/questions/19290/zmv-for-zsh-dry-runs-and-man-pages
		for last; do true; done
		# need to install zsh-doc package for info pages
		showinfo zsh $(test "$last" != zsh && echo "$last") ||
			LESS="$LESS$(test "$last" != zsh && echo "+/^ *$last *\\[")" man zshall;;
	(emacs) man emacs;;
	(*) showinfo "$@" || man "$@" || return=$?
		if test "${return:-0}" -gt 0 && which "$cmd" >/dev/null
		then { unbuffer "$@" --help || { test $? == 1 && "$@" -help; } || { test $? == 1 && "$@" -h; }; } 2>&1 | $paginate
			 return=$?
		fi;;
esac
docs="/usr/share/doc/$cmd"
if test -d "$docs"; then
	len() { echo "$1" | wc -c; }
	shortest() {
		read shortest
		while read in
		do test $(len $shortest) -gt $(len $in) && shortest=$in
		done
		echo $shortest
	}
	file="$(find $docs -name index.html | shortest | grep . || find $docs -name $1*.html | shortest)"

	if test -z "$browse"
	then echo "Find more in file://${file:-docs}"
	else firefox "file://$file"
	fi
fi
exit $return