#!/bin/sh
# Uses clit to convert .lit files to valid epub
# known issues: created metadata uses the old spec

check_tool()
{
    var=$1
    tool=$2
    toolpath=$(which $tool 2>/dev/null)
    if [ "$?" != 0 ]; then
        echo "Cannot find $tool, please make sure it is in your PATH"
        exit 1
    fi
    eval "$var=$toolpath"
}
 
check_tool CLIT clit
check_tool ZIP zip

LIT=$1

if [ "x$LIT" = "x" ]; then
    echo "$0 <file.lit> [file.epub]"
    exit 1;
fi


if [ "x$2" = "x" ]; then
	EPUB=$(echo $1 | sed -e 's/\.lit$/.epub/')
else 
    EPUB=$2
fi


MKTEMP=$(which mktemp)

if [ "$?" != 0 ]; then
    if [ ! -e /tmp/epub ]; then
        mkdir /tmp/epub
    else
        echo "/tmp/epub already exists please erase it first"
        exit 1
    fi
    
else
    TEMPDIR=$($MKTEMP -d)
fi

WD=$(pwd)

IFS='
'

$CLIT $LIT $TEMPDIR/OEBPS/
find "$TEMPDIR"/OEBPS/ -name "*.opf" -exec mv {} "$TEMPDIR"/OEBPS/content.opf \;
unset $IFS

mkdir "$TEMPDIR/META-INF"

cat << EOF > "$TEMPDIR/META-INF/container.xml"
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
    <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>
 
EOF

printf 'application/epub+zip' > "$TEMPDIR"/mimetype

IFS='
'
cd "$TEMPDIR"

$ZIP -0 -m "$WD/$EPUB" mimetype
$ZIP -rg "$WD/$EPUB" *

cd $WD
unset $IFS

rm -rf $TEMPDIR
