#!/usr/bin/env bash

# Copyright (C) 2007 Juan Manuel Borges Caño

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

H2I_AUTHOR="Juan Manuel Borges Caño"
H2I_DESCRIPTION="Generate a simple info manual."
H2I="$(basename $0)"
H2I_BUGREPORT="juanmabc3@gmail.com"
H2I_DATADIR="/usr/share/help2info"
H2I_VERSION="0.0.5"

function h2i_usage
{
	printf "Usage: $H2I [OPTION]... EXECUTABLE\n"
	printf "$H2I_DESCRIPTION\n"
	printf "\n"
 	printf "Mandatory arguments for long options are mandatory for short options too.\n"
	printf "  -d, --dircategory\tset the info directory category\n"
	printf "  -n, --programname\tset full program name\n"
	printf "  -h, --help\t\tshows a help message\n"
	printf "  -v, --version\t\tshows the program version\n"
	printf "\n"
	printf "Report bugs to <$H2I_BUGREPORT>\n"
	exit 0;
}

function h2i_version
{
	printf "$H2I_VERSION\n"
	exit 0
}

TEMP=$(getopt -o "d:n:hv" \
	      -l "dircategory:,programname:,help,version" -n "$H2I" -- "$@")
eval set -- "$TEMP"
while [[ -n "$1" ]]
do
	case "$1" in
		-"d" | --"dircategory") H2I_DIRCATEGORY="$2"; shift 2;;
		-"n" | --"programname") H2I_PROGRAM_NAME="$2"; shift 2;;
		-"h" | --"help") h2i_usage; shift;;
		-"v" | --"version") h2i_version; shift;;
		--) shift; break;;
		*) printf "Internal error!\n"; exit 1;;
	esac
done

# capture output
H2I_OUTPUT=$("$1" --help)

# get usage
# Usage:
#   or:
function h2i_parse_usage
{
	O=$IFS;IFS=$'\n'
	for line in $(echo "$1" | egrep -i '^usage:|^  or:')
	do
		echo "$line"
	done
	IFS=$O
}
H2I_USAGE=$(h2i_parse_usage "$H2I_OUTPUT")

# get description
# comes after usage and ends with a blank line
function h2i_parse_description
{
	array=($(echo "$1" | egrep -ni '^usage:|^  or:' | cut -d':' -f1 | paste -s -d' '))
	for element in ${array[*]}
	do
		let H2I_LNEXT=$element+1
		if ! echo "$1" | sed -n ${H2I_LNEXT}p | egrep -qi '^usage:|^  or:'
			then break;
		fi
	done
	H2I_START=$H2I_LNEXT
	H2I_END=$(echo "$1" | grep -nm 1 '^$' | cut -d':' -f1)
	echo "$1" | sed -n ${H2I_START},${H2I_END}p
}
H2I_DESCRIPTION=$(h2i_parse_description "$H2I_OUTPUT")

# get options (starts after the description with the mandatory line for long and short options and ends with the bugreport comment, last line if present)
function h2i_parse_options
{
	let H2I_START=$(echo "$1" | grep -nm 1 '^$' | cut -d':' -f1)+1
	H2I_LAST_LINE=$(echo "$1" | tail -1)
       	if echo "$H2I_LAST_LINE" | grep -qi 'report'
	then
		H2I_BUGREPORT=$(echo "$H2I_LAST_LINE" | cut -d'<' -f2 | cut -d'>' -f1)
		let H2I_END=$(echo "$1" | wc -l)-1
	else
		H2I_END=$(echo "$1" | wc -l)
	fi
	echo "$1" | sed -n ${H2I_START},${H2I_END}p
}
H2I_OPTIONS=$(h2i_parse_options "$H2I_OUTPUT")

# get year
H2I_YEAR=$(LANG=C date '+%Y')

# get author
if [[ -z $H2I_AUTHOR ]]
	then H2I_AUTHOR=$(grep $(whoami) /etc/passwd | cut -d':' -f5 | cut -d',' -f1)
fi
if [[ -z $H2I_AUTHOR ]]
	then H2I_AUTHOR="Insert author name here"
fi

# get dircategory
if [[ -z $H2I_DIRCATEGORY ]]
then
	H2I_DIRCATEGORY="Insert dir category here"
fi

# get program name
if [[ -z $H2I_PROGRAM_NAME ]]
then
	H2I_PROGRAM_NAME="$1"
fi

# get examples
H2I_EXAMPLES="Insert your samples here"


# convert options to info

function h2i_parse_args
{
	printf "@item "
	for arg in $1
	do
		if [[ -n $H2I_ARGS_ENDED ]]
		then echo -n "$arg "
		else
			if(echo "$arg" | grep -qe '^-\w\+,')
			then echo -n "$arg "
			else echo "$arg"; H2I_ARGS_ENDED="true"
			fi
		fi
	done
	unset -v H2I_ARGS_ENDED ; echo
}

function h2i_options
{
	O=$IFS;IFS=$'\n'
	for line in $1
	do
		IFS=$O
		if [[ -n $H2I_OPTIONS_ENDED ]]
		then
			line=$(echo "$line" | sed -e 's,[ \t]\+, ,g')
			echo "$line"
		else
			if(echo "$line" | grep -qe '^  -\w\+')
			then h2i_parse_args "$line"
			else
				if(echo "$line" | grep -qe '^      --\w\+')
				then h2i_parse_args "$line"
				else
					if(echo "$line" | grep -q '^\w\+')
					then 
						if [[ -z $H2I_OPTIONS_STARTED ]]
						then echo "$line"; echo "@table @code"; H2I_OPTIONS_STARTED="true"
						else
							if [[ -z $H2I_OPTIONS_ENDED ]]
							then echo "@end table"; echo; H2I_OPTIONS_ENDED="true"
							fi
							if(echo "$line" | grep -qi "options:")
							then
								line=$(echo "$line" | sed -e 's,[ \t]\+, ,g')
								echo $line
								echo "@table @code"; unset -v H2I_OPTIONS_ENDED ;
							else
								if [[ -z $H2I_OPTIONS_INFO_STARTED ]]
								then echo "@verbatim"; H2I_OPTIONS_INFO_STARTED="true"
								fi
							       	line=$(echo "$line" | sed -e 's,[ \t]\+, ,g');echo "$line"
							fi
						fi
					else 
						line=$(echo "$line" | sed -e 's,[ \t]\+, ,g'); echo "$line";
					fi
				fi
			fi
		fi
		O=$IFS; IFS=$'\n'
	done
	if [[ -n $H2I_OPTIONS_INFO_STARTED ]]; then echo "@end verbatim"; else printf "@end table"; fi
	IFS=$O
}
H2I_OPTIONS_TEXI=$(h2i_options "$H2I_OPTIONS")

# convert bugreport addres to info
H2I_INFOBUGREPORT=$(echo "$H2I_BUGREPORT" | sed -re 's,(\w@),\1@,g')

# generate the texi file
cat >"$1.texi" <<EOF
\input texinfo	@c -*-texinfo-*-
@comment It was generated by help2info $H2I_VERSION.
@comment %**start of header
@setfilename $1.info
@include version.texi
@settitle $1 @value{VERSION}
@syncodeindex pg cp
@comment %**end of header
@copying
This manual is for $H2I_PROGRAM_NAME
(version @value{VERSION}, @value{UPDATED}),
which is a program that $H2I_DESCRIPTION

Copyright @copyright{} $H2I_YEAR $H2I_AUTHOR

@quotation
Permission is granted to copy, distribute, and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no
Invarian Sections, with the Front-Cove Texts bein ``A Manual,''
and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled ``GNU Free Documentation
License.''

(a) The Back-Cover Text is: ``You have freedom to copy and modify
this Manual, like GNU software.''
@end quotation
@end copying
@dircategory $H2I_DIRCATEGORY
@direntry
* $H2I_PROGRAM_NAME: ($1). $H2I_DESCRIPTION
@end direntry

@titlepage
@title $H2I_PROGRAM_NAME
@subtitle for version @value{VERSION}, @value{UPDATED}
@author $H2I_AUTHOR (@email{$H2I_INFOBUGREPORT})
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage

@contents

@ifnottex
@node Top
@top $H2I_PROGRAM_NAME
@insertcopying
@end ifnottex

@menu
* Introduction:: Introduction.
* Invoking $1::	Invocation.
* Examples:: Some samples of use.
* Reporting Bugs:: Reporting bugs
* Copying This Manual:: The license for this documentation.
@end menu

@node Introduction 
@chapter Introduction
@pindex introduction
@cindex introduction
$H2I_DESCRIPTION

@node Invoking $1
@chapter Invoking $1
@pindex invoking
@cindex invoking @command{$1}
@verbatim
$H2I_USAGE
@end verbatim

@command{$1} may be invoked with the following command-line options:

$H2I_OPTIONS_TEXI

@node Examples
@chapter Examples
@pindex examples
@cindex examples
$H2I_EXAMPLES

@node Reporting Bugs
@chapter Reporting Bugs
@pindex bugs
@cindex reporting bugs
Email bug reports to @email{$H2I_INFOBUGREPORT}. Be sure to include the word @dfn{$1} somewhere in the @code{Subject:} field. Also, please include the output of @samp{$1 --version} in the body of your report if at all possible.


@node Copying This Manual

@appendix Copying This Manual
@menu
* GNU Free Documentation License::	License for copying this manual.
@end menu

@include fdl.texi

@bye
EOF

#copy the fdl.texi file
cp "$H2I_DATADIR/fdl.texi" .
