#!/bin/sh -e

# Libreboot documentation build script: index
#
# This script borrows from gendocs.sh, part of gnulib, the GNU
# Portability Library (https://www.gnu.org/software/gnulib/). The
# objective is to generate a html document which indexes the libreboot
# documentation in its various formats.
#
# This script does not assume a certain set of formats have been
# generated. It generates the index based on which files are located
# in $man_dir.

# The relevant portion of gendocs.sh:
#
# curdate=`$SETLANG date '+%B %d, %Y'`
# sed \
#  -e "s!%%TITLE%%!$MANUAL_TITLE!g" \
#  -e "s!%%EMAIL%%!$EMAIL!g" \
#  -e "s!%%PACKAGE%%!$PACKAGE!g" \
#  -e "s!%%DATE%%!$curdate!g" \
#  -e "s!%%HTML_MONO_SIZE%%!$html_mono_size!g" \
#  -e "s!%%HTML_MONO_GZ_SIZE%%!$html_mono_gz_size!g" \
#  -e "s!%%HTML_NODE_TGZ_SIZE%%!$html_node_tgz_size!g" \
#  -e "s!%%HTML_SECTION_TGZ_SIZE%%!$html_section_tgz_size!g" \
#  -e "s!%%HTML_CHAPTER_TGZ_SIZE%%!$html_chapter_tgz_size!g" \
#  -e "s!%%INFO_TGZ_SIZE%%!$info_tgz_size!g" \
#  -e "s!%%DVI_GZ_SIZE%%!$dvi_gz_size!g" \
#  -e "s!%%PDF_SIZE%%!$pdf_size!g" \
#  -e "s!%%ASCII_SIZE%%!$ascii_size!g" \
#  -e "s!%%ASCII_GZ_SIZE%%!$ascii_gz_size!g" \
#  -e "s!%%TEXI_TGZ_SIZE%%!$texi_tgz_size!g" \
#  -e "s!%%DOCBOOK_HTML_NODE_TGZ_SIZE%%!$html_node_db_tgz_size!g" \
#  -e "s!%%DOCBOOK_ASCII_SIZE%%!$docbook_ascii_size!g" \
#  -e "s!%%DOCBOOK_PDF_SIZE%%!$docbook_pdf_size!g" \
#  -e "s!%%DOCBOOK_XML_SIZE%%!$docbook_xml_size!g" \
#  -e "s!%%DOCBOOK_XML_GZ_SIZE%%!$docbook_xml_gz_size!g" \
#  -e "s,%%SCRIPTURL%%,$scripturl,g" \
#  -e "s!%%SCRIPTNAME%%!$prog!g" \
#  -e "$CONDS" \
# $GENDOCS_TEMPLATE_DIR/gendocs_template >"$outdir/index.html"

# TODO: Implement total size for directories (see e.g.
# http://www.gnu.org/software/findutils/manual/find.html)

man_dir="docs/manual/"
template_dir="docs/templates/"
pkg_title="GNU Libreboot manual"
pkg_email="example@libreboot.org" #sample
pkg_name="libreboot"
curdate=`date '+%B %d, %Y'`

# FUNCTION COPIED FROM gendocs.sh
# Function to return size of $1 in something resembling kilobytes.
calcsize()
{
  size=`ls -ksl $1 | awk '{print $1}'`
  echo $size
}
# END FUNCTION COPIED FROM gendocs.sh

concat_index()
{
    if [ -e $man_dir$1 ]; then
	if [ -d $man_dir$1 ]; then
	    echo "<li><a href=\"$1/index.html\">$2</a>$3</li>" >> "${man_dir}index.html"
	else
	    echo "<li><a href=\"$1\">$2</a>$3</li>" >> "${man_dir}index.html"
	fi
    fi
}

echo "Building index..."

sed \
  -e "s!%%TITLE%%!$pkg_title!g" \
  -e "s!%%PACKAGE%%!$pkg_name!g" \
  -e "s!%%DATE%%!$curdate!g" \
${template_dir}gendocs_template_header >"${man_dir}index.html"

for i in $(ls $man_dir); do
    [ -d $man_dir$i ] || size=`calcsize "${man_dir}${i}"`
    case $i in
	${pkg_name}.dvi.gz)
	    dvi_gz_size=$size;;
	${pkg_name}_html-by-node.tar.gz)
	    html_node_tgz_size=$size;;
	${pkg_name}_html-by-section.tar.gz)
	    html_section_tgz_size=$size;;
	${pkg_name}_html-one-page.html)
	    html_mono_size=$size;;
	${pkg_name}_html-one-page.html.gz)
	    html_mono_gz_size=$size;;
	${pkg_name}.info.gz)
	    info_gz_size=$size;;
	${pkg_name}.pdf)
	    pdf_size=$size;;
	${pkg_name}.pdf.gz)
	    pdf_gz_size=$size;;
	${pkg_name}.ps)
	    ps_size=$size;;
	${pkg_name}.ps.gz)
	    ps_gz_size=$size;;
	${pkg_name}.txt)
	    plaintext_size=$size;;
	${pkg_name}.txt.gz)
	    plaintext_gz_size=$size;;
	${pkg_name}.texi.gz)
	    texinfo_gz_size=$size;;
	*)
	    :;; # if a directory, do nothing (see TODO)
    esac
done

concat_index "${pkg_name}_html-one-page.html" "HTML (${html_mono_size}K bytes)" " - entirely on one web page."
concat_index "${pkg_name}_html-by-node" "HTML" " - with one web page per node."
concat_index "${pkg_name}_html-by-section" "HTML" " - with one web page per section."
concat_index "${pkg_name}_html-one-page.html.gz" "HTML compressed (${html_mono_gz_size}K gzipped characters)" " - entirely on one web page."
concat_index "${pkg_name}_html-by-node.tar.gz" "HTML (${html_node_tgz_size}K gzipped tar file)" " - with one web page per node."
concat_index "${pkg_name}_html-by-section.tar.gz" "HTML (${html_section_tgz_size}K gzipped tar file)" " - with one web page per section."
concat_index "${pkg_name}.info.gz" "Info document (${info_gz_size}K bytes gzipped)."
concat_index "${pkg_name}.txt" "ASCII text (${plaintext_size}K bytes)."
concat_index "${pkg_name}.txt.gz" "ASCII text (${plaintext_gz_size}K bytes gzipped)."
concat_index "${pkg_name}.dvi.gz" "TeX dvi file (${dvi_gz_size}K bytes gzipped)."
concat_index "${pkg_name}.pdf" "PDF file (${pdf_size}K bytes)."
concat_index "${pkg_name}.pdf.gz" "PDF file compressed (${pdf_gz_size}K bytes gzipped)."
concat_index "${pkg_name}.ps" "Postscript file (${ps_size}K bytes)."
concat_index "${pkg_name}.ps.gz" "Postscript file compressed (${ps_gz_size}K bytes gzipped)."
concat_index "${pkg_name}.texi.gz" "Texinfo source (${texinfo_gz_size}K bytes gzipped)."

sed -e "s!%%EMAIL%%!$pkg_email!g" ${template_dir}gendocs_template_footer >>"$man_dir/index.html"

echo "Done."