Code:
#! /bin/bash
filename="$(basename $0)"
logfile="/var/log/scripts/$filename.log"
convertpath="/tmp/ebookconvert"
echo "Script output will be logged to $logfile"
#exec > "$logfile" 2>&1
/opt/scripts/scriptheader.sh "eBook Convert"
mkdir -p "$convertpath"
if ! [[ -f "/usr/bin/ebook-convert" ]]; then
echo "ebook-convert not found. Aborting."
exit 1
fi
if [[ -z $1 ]]; then
echo "No argument supplied. Please enter the path you which to convert."
exit 1
elif ! [[ -d "$1" ]]; then
echo "Argument passed is not a directory."
exit 1
fi
# Entering the directory
cd $1
echo "Scanning $1 for ebooks files..."
# List the types of files in the directory, with frequency
echo "eBook file types detected:"
find . -type f | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort | uniq -c
# Find the media types that will be converted.
# Useful for verifying after the fact.
echo "Files will be copied to '$convertpath' before conversion."
find . -type f -name "*.mobi" -o -type f -name "*.lit" -o -type f -name "*.pdf" > "$convertpath/ebooklist.txt"
echo "Converting MOBI Files."
# IFS change for file names with spaces in them
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in *.mobi; do
basefile=$(basename "$file")
# Check to see if we already have the format.
if ! [[ -f "$basefile.epub" ]]; then
/usr/bin/ebook-convert "$file" "$basefile.epub";
if [ $? -ne 0 ]; then
echo "Error converting $file.";
fi
/usr/bin/calibredb add $file
else
echo "ePub for $file exists already, no conversion needed.";
fi
done
# restore default IFS
IFS=$SAVEIFS