#!/bin/sh
#
# sm2man -- strange markups to man converter
#
# (cl) 1999, 2000 malekith@topnet.pl
#
# you may modify & distribute this under GNU GPL
#

if [ "$1" = "--help" -o "$1" = "-h" ]
then
	cat <<EOF
usage: sm2man [file.sm]

Strane markups:
for writing manpages easier...

\n stands for newline which is needed in given place

<man> name sect-num "date" source "manual" \n
<sect> blah \n -> gets uppper case, .SH BLAH
<b>blah</b>, <i>blah</i> - like html
<r>blah</r> - pargraph indented right
blank line -> paragraph
<p> - ditto
\n<pre>\n -- go figure

list, ex:
	-------------------
	<list> 0.8i
	<li>-h
	to get help
	<li>-f
	force
	-------------------
	
itemization:
	-------------------
	<itemize>
	<item>blah
	blah
	<item>blah
	</itemize>
	-------------------

(cl) 1999, 2000 malekith@topnet.pl
EOF
	exit 1
fi

prog='BEGIN {
	IGNORECASE = 1
	pre = 0
	bold = 0
	italic = 0
	url=0
	print ".\\\""
	print ".\\\" this manpage has been genrated with sm2man gawk script"
	print ".\\\" sm2man by malekith@topnet.pl"
	print ".\\\""
}

/^[[:space:]]*$/ {
	printf ".PP\n"
	$0 = ""
}

/^[[:space:]]*<man>/ {
	sub(/^[[:space:]]*<man>[[:space:]]*/,"")
	printf ".TH %s\n", $0
	$0 = ""
}

/^[[:space:]]*<sect>/ {
	sub(/^[[:space:]]*<sect>[[:space:]]*/,"")
	printf ".SH %s\n", toupper($0)
	$0 = ""
}

/^[[:space:]]*<list>/ {
	sub(/^[[:space:]]*<list>[[:space:]]*/,"")
	printf ".TP %s\n", $0
	$0 = ""
}

/^[[:space:]]*<li>/ {
	sub(/^[[:space:]]*<li>[[:space:]]*/,"")
	printf ".TP\n.B %s\n", $0
	$0 = ""
}

/^[[:space:]]*<itemize>[[:space:]]*$/ {
	printf ".RS 0.3i\n"
	$0 = ""
}

/^[[:space:]]*<\/itemize>[[:space:]]*$/ {
	printf ".RE\n"
	$0 = ""
}

/^[[:space:]]*<item>/ {
	sub(/^[[:space:]]*<item>[[:space:]]*/,"")
	printf ".TP 0.3i\n.B o\n", $0
}

/^[[:space:]]*<pre>[[:space:]]*$/ {
	printf ".nf\n"
	pre = 1
	$0 = ""
}

/^[[:space:]]*<\/pre>[[:space:]]*$/ {
	printf ".fi\n"
	pre = 0
	$0 = ""
}

func oops(a)
{
	printf ("%s:%d: %s\n", FILENAME, FNR, a) > "/dev/stderr"
}

func ptag(tag, after)
{
	if (tag == "<p>") { printf ".PP\n" } 
	else if (tag == "<b>") { bold++ }
	else if (tag == "<i>") { italic++ }
	else if (tag == "</b>") { bold-- }
	else if (tag == "</i>") { italic-- }
	else if (tag == "<r>") { printf ".RS\n" }
	else if (tag == "</r>") { printf ".RE\n" }
	else if (tag == "<url>") { url++; }
	else if (tag == "</url>") { url--; }
	else oops("unknown tag " tag)
}

func putl(a)
{
	if (a ~ /^[[:space:]]*$/)
		return;
	
	if (url)
		printf ".B <%s>\n", a
	else if (bold)
		printf ".B %s\n", a
	else if (italic)
		printf ".I %s\n", a
	else if (a == ".")
		printf "..\n"
	else
		print a
}

{
	a = $0
	gsub("-","\\-",a);
	while (a !~ /^[[:space:]]*$/) {
		if (!pre)
			sub("^[[:space:]]*","",a);
		if (a ~ /</) {
			if (a !~ />/) {
				oops("no matching \">\"");
				break
			}
			bef = substr(a,1,index(a,"<")-1);
			tag = substr(a,index(a,"<"),\
				     index(a,">")-index(a,"<")+1);
			after = substr(a,index(a,">")+1);
			putl(bef)
			ptag(tag,after)
			a = after
		} else  {
			putl(a)
			a="";
		}
	}
}'


if [ "x$1" = "x" ]
then
	gawk "$prog" 
else
	in=$1
	out=`echo $in | sed -e 's/\.sm/\.man/g'`
	if [ $in = $out ]
	then
		out=${in}.1
	fi

	if [ ! -f $in ]
	then
		echo "$in: No such file or directory"
		exit 1
	fi

	gawk "$prog" $in > $out
fi
