#!/bin/bash
clear
# Script is also called via AppleScript which does not get path by itself
PATH=/usr/local/texlive/2012/bin/universal-darwin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# Change working folder of the script as this is set to the users home directory by defaults
cd "`dirname "$0"`"
# (I'd prefer the script to be in a subdirectory as well but "pandoc ../*.md" didn't seem to work)
# Title and authors as they're used in metadata and title page
TITLE="Lifehacking met Evernote"
AUTHOR="Frank Meeuwsen, Oskar van Rijswijk, Patrick Mackaaij"
DATE=$(date +%d-%m-%Y)
# Create title.txt for the title page
# In the current folder instead of eBooks/ as Pandoc will look for images relative to title.txt's folder
TITLEPAGE=title.txt
echo Creating $TITLEPAGE for $TITLE
echo "% $TITLE" > $TITLEPAGE
echo "% $AUTHOR" >> $TITLEPAGE
echo "% $DATE" >> $TITLEPAGE
# Create metadata.xml for ePub (and mobi)
# Date/time differs from title page, includes a timestamp following the ePub standard
METADATA=eBooks/metadata.xml
echo Creating $METADATA
echo "nl-NL" > $METADATA
echo "$(date +%Y-%m-%dT%H:%M:%S)" >> $METADATA
echo "Lifehacking.nl" >> $METADATA
echo "$TITLE" >> $METADATA
echo "Evernote" >> $METADATA
echo "Lifehacking met Evernote. Handboek met tips voor slim gebruik." >> $METADATA
echo "$AUTHOR" >> $METADATA
# Convert the cover image to a PDF file via Markdown and it's own latex template
echo Creating PDF cover
pandoc cover.markdown -o "eBooks/cover.pdf" --template=eBooks/cover.latex
# Create the PDF (without the cover image)
# For the Mac cmd symbool ⌘ you'd have to use a different latex engine but also font: --latex-engine=xelatex --variable mainfont="Menlo"
echo Creating PDF
pandoc $TITLEPAGE *.md -o "eBooks/Lifehacking met Evernote zonder cover.pdf" --variable mainfont="Georgia" --variable lang="dutch" --number-sections --toc --template=eBooks/default.latex
# Merge cover and PDF
echo Merging cover and PDF
pdftk "eBooks/cover.pdf" "eBooks/Lifehacking met Evernote zonder cover.pdf" cat output "eBooks/Lifehacking met Evernote.pdf"
# Split PDF for sneak preview (cover, title page and table of contents)
echo Splitting PDF first pages
pdftk "eBooks/Lifehacking met Evernote.pdf" cat 1-4 output "eBooks/Lifehacking met Evernote - inhoudsopgave.pdf"
# Create ePub (sed commands add voorwoord to the table of contents as it will be an empty line in iBooks otherwise)
echo Creating EPUB
sed -i "" 's/\\section\*{Voorwoord}/# Voorwoord/g' "0.00 Voorwoord.md"
sed -i "" 's/\\subsection\*{Aanbevelingen}/### Aanbevelingen/g' "0.00 Voorwoord.md"
pandoc $TITLEPAGE *.md -o "eBooks/Lifehacking met Evernote.epub" --toc --epub-metadata=$METADATA --epub-cover-image=images/cover.jpg --data-dir=eBooks/
sed -i "" 's/# Voorwoord/\\section\*{Voorwoord}/g' "0.00 Voorwoord.md"
sed -i "" 's/### Aanbevelingen/\\subsection\*{Aanbevelingen}/g' "0.00 Voorwoord.md"
# Convert ePub to mobi with official Amazon-tool
echo Creating MOBI
eBooks/KindleGen_Mac_i386_v2_5/kindlegen "eBooks/Lifehacking met Evernote.epub" -o "Lifehacking met Evernote.mobi" > /dev/null
# Zip it
echo Zipping
zip --junk-paths "eBooks/Lifehacking met Evernote.zip" "eBooks/Lifehacking met Evernote.pdf" "eBooks/Lifehacking met Evernote.epub" "eBooks/Lifehacking met Evernote.mobi" "readme.txt" > /dev/null
# Clean up files ("bakken" is used to start the script via Folder Actions/Automator)
echo Cleaning up
rm -f bakken.*
rm "eBooks/Lifehacking met Evernote zonder cover.pdf"
rm "eBooks/cover.pdf"
rm "title.txt"
echo Done.