Files
ebook-technology-alternatives/convert.sh

156 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# 2024-03-06 Hyperling
# PanDoc Usage: https://pandoc.org/MANUAL.html#general-writer-options
## Setup ##
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
DIR="`pwd`"
echo "$DIR"
date
## Global Variables ##
header="header.md"
work_dir="$DIR/build"
export_dir="$DIR/export"
ebook_prefix="TechnologyAlternatives"
## Functions ##
function log {
echo -e "*** $1 ***"
}
# Start Log Section
function logs {
echo -e "\n***** $1 *****"
}
# Finish Log Section
function logf {
echo -e "***** $1 *****\n"
}
# Copy a file into another file.
function insert {
input="$1"
ebook="$2"
log "Adding '$input' to '$ebook'."
cat "$input" >> "$ebook"
}
# Compile all the snippets into one Markdown file.
function combine {
ebook="$1.md"
logs "Combining documents to create '$ebook'."
shift
for addition in "$@"; do
insert "$addition" "$ebook"
done
logf "Done!"
}
# Create the PDF and booklets them move them back to the project directory.
function convert {
source="$1"
version="$2"
out_dir="$export_dir"
ebook="${source//.md/.pdf}"
logs "Creating '$ebook' from '$source'."
book="${ebook//.pdf/-book.pdf}"
booklet="${ebook//.pdf/-Booklet.pdf}"
rotated="${ebook//.pdf/-Rotated.pdf}"
log "Set VERSION to '$version'."
if [[ -z "$version" ]]; then
log "Version is null, removing the extra space."
space=" "
else
space=""
fi
sed -i "s/$space{VERSION}/$version/g" "$source"
log "Compiling '$source' to '$ebook'."
pandoc "$source" \
-s -N --toc-depth=4 \
-o "$ebook" \
>/dev/null
status=$?
log "Creating booklet of '$ebook'."
pdfbook2 -n "$ebook" >/dev/null
mv -v "$book" "$booklet"
log "Creating rotated booklet of '$ebook'."
pdfbook2 -ns "$ebook" >/dev/null
mv -v "$book" "$rotated"
log "Moving '$ebook' and its booklets to '$out_dir'."
mkdir -p "$out_dir"
mv -v *.pdf "$out_dir"/
logf "Complete!"
}
## Cleanup ##
logs "Clean PDFs"
echo "Old:"
find "$DIR" -name "*".pdf -exec du -h {} \; -delete | sort -h
logs "Clean working directory '$work_dir'"
if [[ -e "$work_dir" ]]; then
rm -rv "$work_dir"
fi
## Main ##
mkdir -pv "$work_dir"
cd "$work_dir"
cp -v "$DIR/$header" ./
sed -i "s/{REVISION_DATE}/`date +'%Y-%m-%d'`/g" "$header"
# Primary, All In One (AIO)
name="$ebook_prefix""_AllPlatforms"
combine "$name" "$header" `ls "$DIR"/content*.md`
convert "$name.md" ""
# Secondary, Mobile
name="$ebook_prefix""_MobileOnly"
combine "$name" "$header" "$DIR/content0"* "$DIR/content1"* \
"$DIR/content7"* "$DIR/content9"*
convert "$name.md" "(Mobile Only)"
# Secondary, Desktop
name="$ebook_prefix""_DesktopOnly"
combine "$name" "$header" "$DIR/content0"* "$DIR/content2"* \
"$DIR/content7"* "$DIR/content9"*
convert "$name.md" "(Desktop Only)"
# Secondary, Server
name="$ebook_prefix""_ServerOnly"
combine "$name" "$header" "$DIR/content0"* "$DIR/content3"* \
"$DIR/content7"* "$DIR/content9"*
convert "$name.md" "(Server Only)"
## Finish! ##
cd "$DIR"
echo "New:"
find "$DIR" -name "*".pdf -exec du -h {} \; | sort -h
date
exit 0