forked from acabal/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebook-extract
executable file
·64 lines (53 loc) · 1.48 KB
/
ebook-extract
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Extract an epub or a mobi ebook into FILENAME.extracted/, or into the specified new target directory.
USAGE
ebook-extract [-d,--destination=DIRECTORY] FILENAME
EOF
exit
}
die(){ printf "Error: ${1}\n" 1>&2; exit 1; }
require(){ command -v $1 > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
outputDir="."
mobiExtractorPath="/usr/local/bin/mobi_unpack.py"
filename=""
require "${mobiExtractorPath}" "See: http://wiki.mobileread.com/wiki/KindleUnpack"
if [ $# -eq 0 ]; then
usage
fi
for i in "$@"
do
case $i in
-d=*|--destination=*)
outputDir=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
;;
*)
filename="${i}"
;;
esac
done
if [ "${outputDir}" = "." ]; then
outputDir="${outputDir}/$1.extracted/"
fi
if [ -d "${outputDir}" ]; then
die "${outputDir} already exists."
fi
if [ "${filename}" = "" ]; then
usage
fi
#Handle .mobi files
if file "${filename}" | grep -i ": Mobipocket E-book" > /dev/null 2>&1; then
require "${mobiExtractorPath}" "See: http://wiki.mobileread.com/wiki/KindleUnpack"
mkdir "${outputDir}"
"${mobiExtractorPath}" "${filename}" "${outputDir}" > /dev/null
#Handle .epub files
elif file "${filename}" | grep -i ": EPUB" > /dev/null 2>&1; then
mkdir "${outputDir}"
unzip "${filename}" -d "${outputDir}" > /dev/null
else
die "Unrecognized ebook file."
fi