#!/bin/sh

# genlist.sh
# Script to automatically generate a list of LFS hints
# by Olivier Pérès (Olivier Peres if your terminal cannot print accents)
# Modified by Jeroen Coumans to conform to the LFS template
# email: olivier dot peres at laposte dot net
# License: GPL.

# Changes by Anderson Lizardo:
#	[2004-08-03]
#	- Removed some redundant paths (../hints == current dir)
#	- Assume .txt extension for hints, ignoring files like README and LICENSE
#	- Skip hints not conforming to the default format

# Usage: run in the hints/ subdirectory.

#   Limitations
#   ===========
#
# * hint file names must not contain colons, spaces, or any control characters.



# the file to which to write

OUTPUTFILE="list.html"

function output
{
    # echoes its argument to OUTPUTFILE

    echo $1 >> $OUTPUTFILE
}


function htmlclean
{
    # replaces '<' and '>' with the corresponding entities so that they
    # can appear in documents (useful for the authors' email addresses).

    echo `echo $1 | sed -e 's/>/\&gt;/g' -e 's/</\&lt;/g'`
} 


# HINTS = list of all the files in reverse chronological order.
#         (sorted according to their DATE fields, not to the file ages)

HINTS=`grep -H "DATE:" downloads/files/*.txt | sort -fbi -r -k3 -t: | cut -f1 -d:`

# initialise output file

cat ../templates/hints/list-top.html > $OUTPUTFILE # The top of the list.

# write hints

for HINT in $HINTS
do
    DATE=`echo \`grep -h "DATE:" $HINT | cut -f2 -d:\``
    SYNOPSIS=`echo \`grep -h "SYNOPSIS:" $HINT | cut -f2 -d:\``
    AUTHORS=`echo \`grep -h "AUTHOR:" $HINT | \
        cut -f2 -d: | sed 's/$/,/'\` | sed 's/,$//'`
#    LICENSE=`echo \`grep -h "LICENSE:" $HINT | cut -f2 -d:\``

	if [ -z "$DATE" -o -z "$SYNOPSIS" -o -z "$AUTHORS" ]; then continue; fi

    output "<li><h4><a href=\"$HINT\" title=\"$HINT\">$SYNOPSIS</a></h4>"
    output "<dl>"
    output "<dt>Author(s):</dt>"
    output "<dd>`htmlclean "$AUTHORS"`</dd>"
    output "<dt>Date Last Updated:</dt>"
    output "<dd>$DATE</dd>"
#    output "<dt>License:</dt>"
#    output "<dd>$LICENSE</dd>"
    output "</dl>"
    output "</li>"
done


# finalise output file

cat ../templates/hints/list-bottom.html >> $OUTPUTFILE


