#!/bin/bash

# Copyright (C) 2009-2010 Nokia Corporation. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


# HTML-specific functions

function create_html() {
    if [ -z "${CSS}" ] || [ ! -f ${CSS} ] ; then
	echo "!!! CSS file \`${CSS/${ORIG_PATH}\//}' not found, stop."
	exit 42
    fi
    
    css_path="${HTML_PATH}/$( basename ${TEX%.tex} ).css"

    cp ${CSS} ${css_path}

    sed -i 's/^\\listof/%\\listof/g' ${TEX}

    execute_latex

    # This is ugly
    cd ${DOC_PATH}

    echo -n " - Running latex2html, "
    [ ${HIGHLIGHT} == ${NO} ] && echo -n "not "
    echo -n "highlighting source..."

    if [ ! -f "${SCRIPT_PATH}/.latex2html-init" ] ; then
	latexinitfile=$( mktemp )
	echo '$USE_STRAIGHT_QUOTES=1' > ${latexinitfile}
    else
	latexinitfile="{SCRIPT_PATH}/.latex2html-init"
    fi

    highlight=""
    [ ${HIGHLIGHT} == ${YES} ] && highlight="--highlight"

    latex2html \
        ${highlight} \
        -init_file ${latexinitfile} \
        -local_icons \
        -show_section_numbers \
        -split 3 \
        -toc_depth 5 \
        -no_navigation \
        -no_footnode \
        -rootdir ${HTML_PATH} \
        -info '' \
        -address '' $( basename ${TEX} ) \
        2>&1 | tee "/tmp/latex2html.log" \
	| tail -n 1 | grep -q "Done"

    err=${?}

    mv "/tmp/latex2html.log" ${LOG_PATH}

    cd ${ORIG_PATH}

    if [ ${err} -ne 0 ] ; then
	echo
	echo "!!! Oops. Something went wrong. Please check the logs in"
        echo "    ${LOG_PATH}"
	echo
	grep -i "error" ${LOG_PATH}/*.log \
	    | sed -r 's/^/!!! /' \
	    | sed -r 's/(.{70})(.)/\1\n    \2/'
	echo
	exit ${err}
    fi

    echo "done."

    sed -i 's/^%\\listof/\\listof/g' ${TEX}
}


function add_tarball_link() {
    size=$( du -ch "${REL_PATH}/${NAME}/${NAME}.tar.gz" \
        | grep total | grep -oEe "[[:digit:]]*[\.]*[[:digit:]]*[(K|M)]" \
        | sed 's/\t//' )
    sed -i "/<!-- marker for sed -->/ c \
        <p>This course as a single gzipped tarball: 
           <a href=\"../${NAME}.tar.gz\">${size}</a></p>" \
               ${HTML_PATH}/index.html
}



function mangle_html() {
    for i in $( ls ${HTML_PATH}/*.html ) ; do

# remove silly subsections part
        sed -i \
            '/<A NAME=\"CHILD_LINKS\"><STRONG>Subsections<\/STRONG><\/A>/ d' ${i}
	
	
# add margins	
	sed -i '
          /<UL CLASS=\"ChildLinks\">/ i \
<div class=\"margins\">\
' ${i}

# remove latex2html table width guesses
        sed -i 's/VALIGN=\"TOP\" WIDTH=[0..9]{0,}/VALIGN=\"TOP\"/g' ${i}

# remove empty divs
        sed -i '
          /^<DIV ALIGN=\"[A-Z]*\">$/ {
            N
            N
            /\n<\/DIV>/ {
              s/[<]DIV ALIGN=["][A-Z]*["][>]\n\n[<][/]DIV[>]//
            }
          }' ${i}

# remove a certain case of forcing a linebreak before a paragraph
        sed -ui '
          /^<BR>/ {
            N
            N
            /\n<P>/ {
              s/<BR>\n\n//
            }
          }' ${i}

# add summary attribute with content of caption to tables
# FIXTHIS: only works for some tables
        sed -i '
          /<TABLE>/ N
          /\n<CAPTION/ {
            N
            s/^\(.*\)<TABLE>\(\n.*\n\)\(.*\)\(<\/CAPTION>.*\)/\1<TABLE summary="\3">\2\3\4/
          }' ${i}

# remove class="verbatim" as it is not needed
        sed -i 's/<PRE( )+CLASS=\"verbatim\">/<PRE>/' ${i}


# Remove extra <BR>'s and newlines in grayboxes
        sed -rni '
          : seek_box_start
            /<div class="graybox">/ {
              : seek_box_end
                /<\/div>/ {                
                  s/^/\n<!-- Graybox -->\n/
                  s/$/\n<!-- End Graybox -->\n/
                  s/<BR>//g
                  s/\n\n/\n/g
                  b end
                }
                $ b end
                N
                b seek_box_end
            }
            p
            $ b end
            n
            b seek_box_start
          : end
            N
            p' ${i}

# Remove extra <BR>'s and newlines in noteboxes
        sed -rni '
          : seek_box_start
            /<div class="notebox">/ {
              : seek_box_end
                /<\/div>/ {
                  s/^/\n<!-- Notebox -->\n/
                  s/$/\n<!-- End Notebox -->\n/
                  s/<BR>//g
                  s/N\.B\.\n\n/N.B.\n/
                  s/\n\n/\n/g
                  s/>\n/> /g
                  b end
                }
                $ b end
                N
                b seek_box_end
            }
            p
            $ b end
            n
            b seek_box_start
          : end
            N
            p' ${i}

    done

## close div "margins"
    sed -i "/<!--End of Table of Child-Links-->/ i \
<\/div> \
" ${HTML_INDEX}
}


cleanup_index() {

    [ -f ${HTML_INDEX} ] || (
	echo "${HTML_INDEX} not found!!"
        # echo "PWD: ${PWD}"
        # echo "ls $( dirname ${HTML_INDEX} )"
	# ls $( dirname ${HTML_INDEX} )
	exit 42
	)
    index_full="${HTML_INDEX%.html}_full.html"
    mv ${HTML_INDEX} ${index_full}    

    sed -i 's/<\/UL>/\<\/UL>\n/g' ${index_full}

    sed -rn '
          # Seek UL
          : seek
            $ b end
            p
            n
            /<UL/ {
              i \
                <!-- up to 1 -->
              b first
            }
            b seek


          # First level
          : first
            $ b end
            p
            n
            /<\/UL/ {
              i \
                <!-- down to 0 -->
              b seek
            }
            /<UL/ {
              i \
                <!-- up to 2 -->          
              b second
            }
            b first


          # Second level
          : second
            $ b end
            n
            /<\/UL/ {
              n
              i \
                <!-- down to 1 -->
              b first
            }
            /<UL/ {
              i \
                <!-- up to 3 -->          
              b third
            }
            b second


          # Third level
          : third
            $ b end
            /<\/UL/ {
              n
              i \
                <!-- down to 2 -->
              b second
            }
            /<UL/ {
              i \
                <!-- up to 4 -->          
              b fourth
            }
            b third


          # Fourth level
          : fourth
            $ b end
            n
            /<\/UL/ {
              i \
                <!-- down to 3 -->
              b third
            }
            /<UL/ {
              i \
                <!-- up to 5 -->
              b fifth
            }
            b fourth


          # Fifth level
          : fifth
            $ b end
            n
            /<\/UL/ {
              i \
                <!-- down to 4 -->
              b fourth
            }
            /<UL/ {
              i \
                <!-- up to 6 -->
              b sixth
            }
            b fifth


          # Sixth level
          : sixth
            $ b end
            n
            /<\/UL/ {
              i \
                <!-- down to 5 -->
              b fifth
            }
            /<UL/ {
              i \
                <b>Can not handle over six levels!</b>
            }
            b sixth

         
          # End
          : end
            N
            p' ${index_full} > ${HTML_INDEX}
}


function find_css() {
    echo " * Looking for CSS"
    tex=$( basename ${TEX%.tex} )
    candidates="${DOC_ORIG_PATH}/html/${tex}.css"
    candidates="${candidates} ${DOC_ORIG_PATH}/html/maemo-documentation.css"
    for candidate in ${candidates} ; do
	echo -n "  - \`$( basename ${candidate} )'... "
	if [ -e ${candidate} ] ; then
	    CSS=${candidate}
	    echo "found."
	    return
	else
	    echo "not found."
	fi
    done
}


function sweep_crud() {
    for file in \
	contents.pl \
	images.pl \
	internals.pl \
	labels.pl \
	sections.pl \
	images.aux \
	images.bbl \
	images.out \
	images.tex ; do
	rm -f "${HTML_PATH}/${file}"
    done 

    # Remove Contents
#    for node in ${HTML_PATH}/node*.html ; do
#	if ( grep -Eq '^<TITLE>Contents<\/TITLE>' ${node} ) && \
#	    ( grep -Eq '^<META NAME="description" CONTENT="Contents">' ${node} ) ;
#	    then rm ${node}
#	    nodebase=$( basename ${node} )
#	    for idx in ${HTML_PATH}/index*.html ; do
#		sed -i "/HREF=\"${nodebase}\"/d" ${idx}
#	    done
#	fi
#    done
   
    mv ${HTML_PATH}/{"WARNINGS","images.log"} ${LOG_PATH} 2>/dev/null
}   


function disable_toc() {
    for tex in $( find ${WORK_PATH} | egrep '\.tex$' ) ; do
	sed -ri 's/^\\tableofcontents/%\\tableofcontents/g' ${tex}
    done
}



## Main

. $( dirname ${0} )/maemodoc-common

set +u
if [ ${COMMON_INCLUDED} != ${YES} ] ; then
    echo "${0}: $( dirname ${0} )/maemodoc-common not found."
    exit 42
fi
set -u

allow_unset
[ ${#} -lt 1 ] && usage

parse_args ${*}
restore_nounset

check_paths

echo "* Setting up the work environment"

make_temp

disable_toc

get_skeleton_list

find_css

parse_title

get_chapter_list

prune_chapters

HTML_PATH="${REL_PATH}/${TITLE}/html"
HTML_INDEX="${HTML_PATH}/index.html"

make_dirs html

echo "* Generating HTML, this can take several minutes"

create_html

check_snippets

echo "* Polishing HTML"

cleanup_index

mangle_html

sweep_crud

if [ ${NO_CLEANUP} == ${YES} ] ; then
    echo "* Skipping cleanup"
else
    echo "* Cleaning up"
    rm --one-file-system -Rf ${WORK_PATH}
fi

echo "* Done."
