#!/bin/bash
# Copyright (C) 2012-2014  Stephan Kreutzer
#
# 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 Affero 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 Affero General Public License 3 for more details.
#
# You should have received a copy of the GNU Affero General Public License 3
# along with Freie Bibel. If not, see <http://www.gnu.org/licenses/>.



printf "hag2epub2.sh  Copyright (C) 2012-2014  Stephan Kreutzer\n"
printf "This program comes with ABSOLUTELY NO WARRANTY.\n"
printf "This is free software, and you are welcome to redistribute it\n"
printf "under certain conditions. See the GNU Affero General Public\n"
printf "License 3 or any later version for details.\n\n"

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

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

if ! test -d "$2"; then
    printf "hag2epub2.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 "hag2epub2.sh: Couldn't write '$2/mimetype'.\n"
    exit -3
fi

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

    if [ $? != 0 ]; then
        printf "hag2epub2.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 hag2epub2.sh (http://www.freie-bibel.de). -->
<container xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\" version=\"1.0\">
  <rootfiles>
    <rootfile full-path=\"content.opf\" media-type=\"application/oebps-package+xml\"/>
  </rootfiles>
</container>" > "$2/META-INF/container.xml"

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

xmlstarlet transform hag2opf.xsl $1 > "$2/content.opf"
xmlstarlet transform hag2ncx.xsl $1 > "$2/toc.ncx"


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

xmlstarlet transform hag2infohtml.xsl $1 > "$2/information.xhtml"

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" "content.opf" "toc.ncx" "information.xhtml"

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

