:
# Generate alphabetic index files ia.html, ib.html, etc.
#
# Geoff Clare, December 2008
# Minor updates for 2016 edition - change year! AJJ March 2016
# also change handling of topics as xbd has clause numbers

PATH=$(getconf PATH) || exit 1

# all work done in temporary directory
mkdir genalpha.tmp || exit 1
unset CDPATH
cd genalpha.tmp || exit 1

export LC_ALL=C

# make lists of headers, functions, utilities, builtins and topics to include

sed -n '/>NAME<\/h4>/,/>SYNOPSIS<\/h4>/{
    s:^<blockquote>\(.*\)</blockquote>.*:\1:p
}' ../../basedefs/*.h.html > headers

awk '/>NAME<\/h4>/,/>SYNOPSIS<\/h4>/ { in_name = 1 }
    in_name && /^<blockquote>/ {
	line = $0
	while (line !~ /<\/blockquote>/) {
	    getline
	    line = line " " $0
	}
	$0 = line
	sub(/^<blockquote>/, "")
	sub(/<\/blockquote>.*/, "")
	fnc = FILENAME
	sub(/\.html$/, "", fnc)
	sub(/.*\//, "", fnc)
	expr = "(^| )" fnc "(, | )"
	sub(expr, " ")
	if (fnc == "exec") { next }
	if (fnc != "daylight" && \
	    fnc != "environ" && \
	    fnc != "errno" && \
	    fnc != "getdate_err" && \
	    fnc != "optarg" && \
	    fnc != "opterr" && \
	    fnc != "optind" && \
	    fnc != "optopt" && \
	    fnc != "signgam" && \
	    fnc != "stderr" && \
	    fnc != "stdin" && \
	    fnc != "stdout" && \
	    fnc != "timezone" && \
	    fnc != "tzname") {
		fnc = fnc "()"
	}
	sub(/^/, fnc ", ")
	sub(/,  /, ", ")
	sub(/, -/, " -")
	print
    }
    { in_name = 0 }
' ../../functions/*.html > functions

sed -n '/>NAME<\/h4>/,/>SYNOPSIS<\/h4>/{
    s:^<blockquote>\(.*\)</blockquote>.*:\1:p
}' ../../utilities/[a-z]*.html > utilities

sed -n '/>NAME<\/h4>/,/>SYNOPSIS<\/h4>/{
    s:^<blockquote>\(.*\)</blockquote>.*:\1:p
}' ../../utilities/V3_chap02.html > builtins

awk '/<h[23]><a name="tag_.*<\/h[23]>/ {
    sub(/.*<h[23]><a name="/, "")
    sub(/"><\/a>/, " ")
    sub(/<\/h[23]>.*/, "")
    chap = FILENAME
    sub(/\.html$/, "", chap)
    sub(/.*\//, "", chap)
    print chap "#" $0
}' ../../basedefs/V1_chap0[2-9].html ../../basedefs/V1_chap1[012].html > topics


# Now generate each index file in turn

for lower in a b c d e f g h i j k l m n o p q r s t u v w x y z
do (
    upper=$(echo $lower | tr a-z A-Z)
    printf '%s %s %s\n' \
'<!DOCTYPE HTML>
<html lang="en"> 
<head> 
<link type="text/css" rel="stylesheet" href="style.css">
<!-- Copyright 2024 The Open Group, All Rights Reserved --> 
<title>Alphabetical Index -' \
    "$upper" \
'</title>
</head> 
<body bgcolor=white>
<basefont size="3"> <!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 8<br>
IEEE Std 1003.1-2024<br>
Copyright &copy; 2024 The IEEE and The Open Group, All Rights reserved.</font></center>

<!--header end-->
<hr size="2" noshade>'

    printf '%s\n' \
'<P>[
<a href="ia.html">A</A>
|
<a href="ib.html">B</A>
|
<a href="ic.html">C</A>
|
<a href="id.html">D</A>
|
<a href="ie.html">E</A>
|
<a href="if.html">F</A>
|
<a href="ig.html">G</A>
|
<a href="ih.html">H</A>
|
<a href="ii.html">I</A>
|
<a href="ij.html">J</A>
|
<a href="ik.html">K</A>
|
<a href="il.html">L</A>
|
<a href="im.html">M</A>
|
<a href="in.html">N</A>
|
<a href="io.html">O</A>
|
<a href="ip.html">P</A>
|
<a href="iq.html">Q</A>
|
<a href="ir.html">R</A>
|
<a href="is.html">S</A>
|
<a href="it.html">T</A>
|
<a href="iu.html">U</A>
|
<a href="iv.html">V</A>
|
<a href="iw.html">W</A>
|
<a href="ix.html">X</A>
|
<a href="iy.html">Y</A>
|
<a href="iz.html">Z</A>
|<a href="index.html"> Index </a> ] </P>'

    printf '%s%s%s\n' \
'<h3><font  face="Helvetica">
<IMG ALIGN=TOP SRC="../images/index.gif" ALT="">' \
    "$upper" \
'</font></h3>
<ul>'

    (
	sed -e "/^[$upper$lower]/!d" -e 's:\([^ ,]*\)\(.*\):\1 <li type=disc><a href="../basedefs/\1.html">\1</a>\2</li>:' -e 's:\(basedefs/[^.]*\)/:\1_:' headers
	sed -e "/^_*[$upper$lower]/!d" -e 's:\([^( ,]*\)\([()]*\)\(.*\):\1\2 <li type=disc><a href="../functions/\1.html">\1\2</a>\3</li>:' -e 's/^_/!/' functions
	sed -e "/^[$upper$lower]/!d" -e 's:\([^ ,]*\)\(.*\):\1 <li type=disc><a href="../utilities/\1.html">\1</a>\2</li>:' utilities
	sed -e "/^[$upper$lower]/!d" -e 's:\([^ ,]*\)\(.*\):\1 <li type=disc><a href="../utilities/V3_chap02.html#\1">\1</a>\2</li>:' builtins
    ) | sort -f | sed 's/^[^ ]* //'

    printf '%s\n\n%s\n\n%s\n' '</ul>' 'Topics' '<ul>'

	##sed -e "/^[^ ]* [$upper$lower]/!d" -e 's:\([^#]*\)#\([^ ]*\) \(.*\):<li type=disc><a href="../basedefs/\1.html#\2">\3</a></li>:' topics | sort -f -t\> -k 3
	## modify for xbd with clause numbers
	sed -e "/^.*<\/a>[$upper$lower]/!d" -e 's:\([^#]*\)#\([^ ]*>\) \([ 0-9\.]*<\/a>\)\(.*\):<li type=disc><a href="../basedefs/\1.html#\2\4</a></li>:' topics | sort -f -t\> -k 3

    printf '%s\n' '</ul>' '</body>' '</html>'

) > ../i$lower.html
done

exit 0
# clean up
cd .. && rm -rf genalpha.tmp 
