#!/bin/bash
# Written by Jerry Cooperstein
# Updates from Behan Webster

set -e
set -u

COURSE="$(basename "$(pwd)")"
LENGTH=${1:-short}
BLURB="blurb.HTML"
LOGO="common/texmf/tex/latex/LFCW/logonew.png"
LOGOCODED=./logocoded

BARE=${BARE:-}
NOBLURB=${NOBLURB:-}
NOCSS=${NOCSS:-}
NOLEGEND=${NOLEGEND:-}
NOLOGO=${NOLOGO:-}
NOPDF=${NOPDF:-}
TIDY=${TIDY:-}
TOCONLY=${TOCONLY:-}

#######################################################
if [[ -n $TOCONLY ]]; then
    BARE=1
    NOBLURB=1
    NOLEGEND=1
    NOLOGO=1
    NOPDF=1
fi

#######################################################
# Common patterns for both outlines
build_outline() {
    pdfgrep '^.*$' "$1" \
        | awk '/^Contents/{flag=1;next}/Please Note/{flag=0}flag' \
        | awk 'BEGIN{flag=1}/List of Figures/{flag=0}flag' \
        | awk 'BEGIN{flag=1}/List of Tables/{flag=0}flag' \
        | grep -E '[0-9]$' \
        | sed -r -e 's;[ .]+[0-9]+$;;'
}

#######################################################
# Outline html with chapters only
build_short_html() {
    sed -r \
        -e 's;^ *[0-9]+ +(.*);    <li class="lft-chapter-title">\1</li>;' \
        -e '/^ +[0-9]+\.[0-9]+ +/d' \
        -e '1s;^;<div class="lft-toc">\n  <ol class="lft-chapters">\n;' \
        -e 's;^Appendices$;  </ol>\n  <div class="lft-appendices-wrapper">\n    <h2 class="lft-appendices-title">Appendices</h2>\n    <ol class="lft-appendices" type="A">;' \
        -e 's;^ *[A-Z]+ +(.*);      <li class="lft-appendix-title">\1</li>;' \
        -e '$a\    </ol>\n  </div>\n</div>' \
        -e '/^ +[A-Z]+\.[0-9]+ +(.*)/d'
}

#######################################################
# Outline html with chapters and sections
build_long_html() {
    sed -r \
        -e 's;^ *1 +(.*);    <li class="lft-chapter"><span class="lft-chapter-title">\1</span>\n      <ul>;' \
        -e 's;^ *[0-9]+ +(.*);      </ul>\n    </li>\n    <li class="lft-chapter"><span class="lft-chapter-title">\1</span>\n      <ul>;' \
        -e 's;^ +[0-9]+\.1 +(.*);        <li class="lft-section-title">\1</li>;' \
        -e 's;^ +[0-9]+\.[0-9]+ +(.*);        <li class="lft-section-title">\1</li>;' \
        -e '1s;^;<div class="lft-toc">\n  <ol class="lft-chapters">\n;' \
        -e 's;^Appendices$;      </ul>\n    </li>\n  </ol>\n  <div class="lft-appendices-wrapper">\n    <h2 class="appendix-title">Appendices</h2>\n    <ol class="lft-appendices" type="A">;' \
        -e 's;^ *A +(.*);      <li class="lft-appendices"><span class="lft-appendix-title">\1</span>\n        <ul>;' \
        -e 's;^ *[B-Z]+ +(.*);        </ul>\n      </li>\n      <li class="lft-appendix"><span class="lft-appendix-title">\1</span>\n        <ul>;' \
        -e 's;^ +[A-Z]+\.[0-9]+ +(.*);          <li class="lft-appendix-section-title">\1</li>;' \
        -e '$a\        </ul>\n      </li>\n    </ol>\n  </div>\n</div>'
}

#######################################################
# ARGS: TOC LENGTH
print_outline() {
    build_outline "$1" | tee "$1.outline" | "build_${2}_html"
}

#######################################################
get_title() {
    local COURSE=$1
    grep 'coursetitle' "$COURSE.tex" \
        | sed \
        -e 's/\\newcommand{\\coursetitle}{//g' \
        -e 's/}//g'
}

#######################################################
pprint_logo() {
    if [[ -z $NOLOGO ]] ; then
        cat $LOGOCODED
    fi
}

print_logo() {
    if [[ -z $NOLOGO ]] ; then
	echo "h1:after {
	content: url('data:inode/symlink;base64,"$(base64 --wrap=0 $LOGO)"');
	display: block;
	margin: 20px 0 0 0 ;
        }" > $LOGOCODED
	echo "" >> $LOGOCODED
        cat $LOGOCODED
    fi
    rm $LOGOCODED
}

#######################################################
print_css() {
    if [[ -z $NOCSS ]] ; then
        cat <<EOF
    <style type="text/css">
      body { font-size: 80%; }
      h2 { color: #a52a2a; }
      .lft-chapter-title { font-weight: bold; }
$(print_logo)
    </style>
EOF
    fi
}

#######################################################
print_header() {
    local COURSE=$1 TITLE
    if [[ -z $BARE ]]; then
        TITLE="$COURSE $(get_title "$COURSE")"

        cat <<EOF
<!DOCTYPE html>
<html>
  <head>
$(print_css)
    <title>$TITLE</title>
  </head>
  <body>
    <h1>$TITLE</h1>
EOF
    else
        print_css
    fi
}

#######################################################
print_footer() {
    if [[ -z $BARE ]]; then
        cat <<EOF
  </body>
</html>
EOF
    fi
}

#######################################################
# ARGS: blurb.HTML
print_blurb() {
    if [[ -z $NOBLURB && -f $1 ]] ; then
        echo '<div class="blurb">'
        cat "$1"
        echo '</div>'
    fi
}

#######################################################
print_legend() {
    if [[ -z $NOLEGEND ]]; then
        cat <<EOF
    <div class="lft-legend">
      <p><b>**</b>
        These sections may be considered in part or
        in whole as optional.  They contain either background
        reference material, specialized topics, or advanced
        subjects. The instructor may choose to cover or not
        cover them depending on classroom experience and
        time constraints.
      </p>
    </div>
EOF
    fi
}

#######################################################
# ARGS: TOC LENGTH
make_outline() {
    local COURSE=$1 LENGTH=$2 BLURB=$3

    print_header "$COURSE"
    print_blurb "$BLURB"
    print_outline "$COURSE.pdf" "$LENGTH"
    print_legend
    print_footer
}

#######################################################
OUTLINE_HTML="$COURSE-$LENGTH-outline.html"
if [[ -z $BARE && -n $TIDY && -x /usr/bin/tidy ]] ; then
    make_outline "$COURSE" "$LENGTH" "$BLURB" \
        | /usr/bin/tidy -ashtml -clean -indent -quiet \
        >"$OUTLINE_HTML" || true
else
    #[[ -x /usr/bin/tidy ]] || echo "FYI: If you install the tidy package I can parse the html for correctness" >&2
    make_outline "$COURSE" "$LENGTH" "$BLURB" >"$OUTLINE_HTML"
fi
echo "I made: $OUTLINE_HTML"

#######################################################
if [[ -z $NOPDF ]] ; then
    BLURB_PDF="${OUTLINE_HTML/html/pdf}"
    google-chrome-stable --headless --print-to-pdf="$BLURB_PDF" "$OUTLINE_HTML" 2>/dev/null
    echo "I made: $BLURB_PDF"
fi
