#!/usr/bin/env bash
#
# Generate asciidoc manpage markup from Cogito script header.
# Copyright (c) Jonas Fonseca, 2005
#
# Takes a path to a Cogito script. Prints the manpage to stdout.

PACKAGE=${CGPACKAGE:-cogito}

command=$1

if [ ! -e "$command" ]; then
	echo "$command does not exist" >&2
	exit
fi

COMMAND=$(basename $command)

# FIXME: Show the SHA1 of the command below the TITLELINE.

# Generate the 'CG-COMMAND(1)' title from cg-command
TITLE="$COMMAND(1)"
# Asciidocs wants the title line ('====') to be as wide as the title.
TITLELINE=$(echo "$TITLE" | sed 's/[0-9a-zA-Z()-]/=/g')

# Get `USAGE="cg-command args..."` line and make it `cg-command args...`
SYNOPSIS=$(sed -n '/^USAGE=/,0s/^USAGE="\(.*\)"/\1/p' < $command)

# Extract the script header.
HEADER=$(sed -n '3,/^$/s/^# *//p' < $command)

# Some scripts have copyright lines followed by 'Based on script by ...' lines.
# Include them so they are also put in the COPYRIGHT section.
COPYRIGHT=$(echo "$HEADER" | sed -n '/^Copyright (c)/,/^$/p' \
			   | sed 's/(c)/(C)/')

# First line of the header contains the caption. Normalize it by lowercasing the
# start and stripping any punctuation.
CAPTION=$(echo "$HEADER" | head -n 1 | tr '[A-Z]' '[a-z]' | sed 's/\.$//')

# Get remaining sections and carefully insert links to cogito commands when they
# were referenced as "`cg-command`". This way references from cg-* combos in
# code listings will be ignored.
BODY=$(echo "$HEADER" | sed '0,/^$/d' \
		      | sed 's/`\(cg-[a-z-]\+\)`/gitlink:\1[1]/g;s/^\(-.*\)::.*/\1::/')

DESCRIPTION=
OPTIONS=
MISC=

section=$(echo "$BODY" | sed -n '1,/^[-][-]*[-]$/p')
section_lines=$(echo "$section" | wc -l)
lines=$(echo "$BODY" | wc -l)

if [ $section_lines = $lines ]; then
	DESCRIPTION="$BODY"

else
	section_end=$(($section_lines - 2))
	DESCRIPTION=$(echo "$BODY" | sed -n "1,${section_end}p")
	BODY=$(echo "$BODY" | sed -n "$((section_lines - 1)),\$p")

	if [ "$(echo "$BODY" | head -n 1)" = "OPTIONS" ]; then
		BODY=$(echo "$BODY" | sed -n '3,$p')
		section=$(echo "$BODY" | sed -n "1,/^[-~][-~]*[-~]\$/p")
		section_lines=$(echo "$section" | wc -l)
		lines=$(echo "$BODY" | wc -l)

		if [ $section_lines = $lines ]; then
			OPTIONS="$BODY"
		else
			section_end=$(($section_lines - 2))
			OPTIONS=$(echo "$BODY" | sed -n "1,${section_end}p")
			MISC=$(echo "$BODY" | sed -n "${section_end},\$p")
		fi

	else
		MISC="$BODY"
	fi
fi

# cg(1) does not answer to the help options in the same way as the
# rest of the commands
if [ "$COMMAND" = "cg" ]; then
	HELP_OPTIONS="
-h, --help::
	Print overview of Cogito commands. Same as gitlink:cg-help[1]."
else
	HELP_OPTIONS="
-h, --help::
	Print usage summary.

--long-help::
	Print user manual. The same as found in gitlink:$COMMAND[1]."
fi

cat <<__END__
$TITLE
$TITLELINE

NAME
----
$COMMAND - $CAPTION

SYNOPSIS
--------
$SYNOPSIS

DESCRIPTION
-----------
$DESCRIPTION

OPTIONS
-------

--
__END__

# Only indent the first paragraph of multi-paragraph list items.
multipara=
echo "$OPTIONS" | while read line; do
	case "$line" in
	*::)
		multipara=
		;;
	"")
		multipara=t
		;;
	*)
		[ "$multipara" ] || line="	$line"
	esac

	echo "$line"
done

cat <<__END__

$HELP_OPTIONS
--

$MISC

COPYRIGHT
---------
$COPYRIGHT

SEE ALSO
--------
$COMMAND is part of gitlink:${PACKAGE}[7],
a toolkit for managing gitlink:git[7] trees.
__END__
