#!/bin/bash
# Copyright (C) 2012-2013  Stephan Kreutzer, Claas-Fridjof Lisowski
#
# This file is part of Freie Bibel.
#
# Freie Bibel is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 or any later version,
# as published by the Free Software Foundation.
#
# Freie Bibel is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License 3 for more details.
#
# You should have received a copy of the GNU General Public License
# along with Freie Bibel. If not, see <http://www.gnu.org/licenses/>.



if [ $# -ne 2 ]; then
    printf "Usage:\n\thag2epub3.sh haggai-xml-file out-directory\n\n"
    exit 1
fi

if ! test -r "$1"; then
    printf "hag2epub3.sh: '$1' isn't readable.\n"
    exit -1;
fi

if ! test -d "$2"; then
    printf "hag2epub3.sh: '$2' isn't a directory.\n"
    exit -2
fi

# The -n option to avoid trailing \n.
echo -n "application/epub+zip" > "$2/mimetype"

if [ $? != 0 ]; then
    printf "hag2epub3.sh: Couldn't write '$2/mimetype'.\n"
    exit -3
fi

if [ ! -d "$2/META-INF" ]; then
    mkdir "$2/META-INF"

    if [ $? != 0 ]; then
        printf "hag2epub3.sh: Couldn't create directory '$2/META-INF'.\n"
        exit -4
    fi
fi

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!-- This file was generated by hag2epub3.sh of hag2epub3 (http://www.freie-bibel.de). -->
<container xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\" version=\"1.0\">
  <rootfiles>
    <rootfile full-path=\"main.opf\" media-type=\"application/oebps-package+xml\"/>
  </rootfiles>
</container>" > "$2/META-INF/container.xml"

if [ $? != 0 ]; then
    printf "hag2epub3.sh: Couldn't write '$2/META-INF/container.xml'.\n"
    exit -5
fi

echo "/* This file was generated by hag2epub3.sh of hag2epub3 (http://www.freie-bibel.de). */

h1
{
    font-weight:bold;
    text-decoration:none;
    color:black;
    text-align:center;
}

a.note
{
    font-weight:bold;
    text-decoration:none;
    color:black;
}

span.vsuper
{
    font-size:0.8em;
    color:#555555;
}" > "$2/epub1style.css"

if [ $? != 0 ]; then
    printf "hag2epub3.sh: Couldn't write '$2/epub1style.css'.\n"
    exit -6
fi

xmlstarlet transform hag2opf.xsl $1 > "$2/main.opf"
xmlstarlet transform hag2ncx.xsl $1 > "$2/toc.ncx"
xmlstarlet transform hag2navtoc.xsl $1 > "$2/navtoc.xhtml"
xmlstarlet transform hag2infohtml.xsl $1 > "$2/information.xhtml"

bookcount=`xmlstarlet select -t -v "count(/XMLBIBLE/BIBLEBOOK)" $1`
booklist=()
processed=0

printf "\n\nGenerating separate XMLs for $bookcount bible books:\n\n"

# Magical number 100 to just try out which books are available, 100 should suffice.
for booknumber in $(seq 100); do
  bookcontent=`xmlstarlet select -t -c "/XMLBIBLE/BIBLEBOOK[@bnumber='$booknumber']" $1`

  if [ ! -z "$bookcontent" ]
  then
    printf "Book #$booknumber...found.\n"
    booklist[$processed]=$booknumber
    echo $bookcontent > "$2/$booknumber.xml"
    processed=$(($processed+1))
  else
    printf "Book #$booknumber...not found.\n"
  fi

  if [ $processed -eq $bookcount ]
  then
    break
  fi
done

printf "\n\nProcessing to XHTMLs:\n\n"

for i in "${booklist[@]}"; do
  printf "$i.xml...\n"
  xmlstarlet transform hag2html.xsl "$2/$i.xml" > "$2/$i.xhtml"
done

cd "$2"
# -0 to make 'mimetype' the first file. -X to remove platform-dependend file attributes.
zip -X0 "out.epub" "mimetype"
zip -Xr "out.epub" "META-INF" "main.opf" "toc.ncx" "navtoc.xhtml" "information.xhtml" "epub1style.css"

for i in "${booklist[@]}"; do
  zip -Xr "out.epub" "$i.xhtml"
done

