View Single Post
Old 07-23-2014, 12:11 AM   #8
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
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
eschwartz is offline   Reply With Quote