#!/bin/sh
# makepub version 0.0.4
# last modified 21 Jul 2017
# Validated posix compliant by ShellCheck: https://www.shellcheck.net/
# Copyright © Guy Inchbald 2016
#
# Description
# -----------
# Builds an ePub media file, from prepared xml source including package document (.opf)
#
# Before running
# --------------
# Install any missing dependencies:
#   zip
# Ensure necessary EPUB 2.0.1 compliance: 
#   all plain text files UTF-8 character encoded,
#   all content files placed in /OEPBS and defined in /OEBPS/content.opf
# Place your runtime copy of makepub.sh in the parent directory of /OEBPS
#
# Function
# --------
# Prompts for manual filename entry in command shell,
#   entry of .epub dot extension is optional,
#   If no filename is provided, defaults to new.epub.
# Creates and overwrites any existing mimetype.
# Checks for and if need be creates /META-INF/container.xml,
#   ignores any other files in /META-INF.
# Overwrites any existing epub with the same name.
# Compatible with IDPF EPUB 2.0.1, 3 and 3.0.1 specifications, 
#   but does *not* check *your* source files or package document!
#
# Wantlist
# --------
# Ability to create and populate /OEBPS/content.opf?
#   can get most of it from toc.ncx.
#   still requires e.g. language, title, isbn, creator, etc, inserted,
#   check specs for minimal requirement and possible range.
# Detect zip, pkzip, gzip, compress, etc. and use whichever.
#   If several are present, use the first one detected?
# Option to abandon 2.0.1 compatibility and run with 3+.
# Options to enter source and dest path/filenames at runtime:
#   ./makepub.sh -s source/path -d dest/path/filename,
#   before checking whether or not the .epub suffix is entered.
# Encryption. What's that all about? DRM?
#   Seems to take place after file compression but before "storing" in the zip archive. 
#   Does this affect the use of zip's own compression?

# input filename from terminal. (read -p is undefined in posix)
printf "Enter ePub file name and press [ENTER]: "
read -r NAME

# check for null return
if [ -z "$NAME" ]; then
  NAME=new
fi

# ensure .epub dot extension is present
if [ ${NAME} = ${NAME%.epub} ]; then
  NAME=$NAME.epub
fi

# create and add mimetype file, uncompressed
# echo option n = omit trailing newline character
echo -n "application/epub+zip" > mimetype 
# zip options:
#  X = no extra file attributes, which may be system-specific
#  0 = uncompressed
zip -X0 $NAME mimetype

# check for and if need be add the xml container, compressed
# if options:
#  d = exists and is a directory
#  r = exists and is readable (not the same as the zip option r)
if [ ! -d META-INF ]; then
  mkdir META-INF
fi
if [ ! -r META-INF/container.xml ]; then
# echo option n suppresses opening newline character
  echo -n > META-INF/container.xml
  printf '<?xml version="1.0" encoding="UTF-8"?>\n<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">\n  <rootfiles>\n    <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>\n  </rootfiles>\n</container>' >> META-INF/container.xml
fi
# zip options:
#  X = no extra file attributes, which may be system-specific
#  9 = compression level 9 (maximum)
#  D = don't create standalone Directory entries
#  r = recursive: all files in the directory are added (not the same as the if option r)
zip -X9Dr $NAME META-INF

# add the OEBPS tree, compressed
zip -X9Dr $NAME OEBPS

