-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep
executable file
·84 lines (75 loc) · 2.88 KB
/
ep
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" )
optq=0 force=0
dieifnotinpath() { command -v "$1" >/dev/null || die "$1 not in PATH"; }
dieifnotinpath find_calibre_binary
ecbin="$(find_calibre_binary ebook-convert)" || die
# echo "ecbin=$ecbin"
ecbin_version_2line="$("$ecbin" --version)" ; read -r ecbin_version <<<"$ecbin_version_2line"
[[ "$ecbin_version" ]] || die
# echo "ecbin_version=$ecbin_version"
convert_to_pdf() (
# https://manual.calibre-ebook.com/generated/en/ebook-convert.html#pdf-output-options
mpts=3 mpo='--pdf-page-margin-'
mbotpts="${EP_PDF_PG_MARGIN_BOTTOM:-"$mpts"}"
echo "$ecbin_version"
see "$ecbin" "$1" "$2" --output-profile=ipad --use-profile-size "${mpo}bottom=$mbotpts" "${mpo}left=$mpts" "${mpo}right=$mpts" "${mpo}top=$mpts"
)
do1file() {
local fnm="$1"
if [[ ! -f "$fnm" ]]; then
((optq!=0)) || echo "$fnm does not name a file"
return
fi
local ext="${fnm##*.}"
if [[ "$ext" && "${ext^^}" == "PDF" ]]; then # don't try to convert a PDF to PDF
((optq!=0)) || echo "skipping: src is $ext '$fnm'"
else
local fpath=""
if [[ $fnm == */* ]]; then
fpath="${fnm%/*}/"
fi # ; echo "fpath=$fpath'"
local sanspath="${fnm##*/}" # ; echo "sanspath=$sanspath'" # https://stackoverflow.com/a/965069
local sansext="${sanspath%.*}" # ; echo "sansext=$sansext'"
local srcfnm="$fnm" # ; echo "srcfnm=$srcfnm'"
local tgtfnm="$fpath$sansext.medtype.pdf" # ; echo "tgtfnm=$tgtfnm'"
if ((!force)) && [[ -f "$tgtfnm" && "$tgtfnm" -nt "$srcfnm" ]]; then
((optq!=0)) || echo "skipping: tgtfnm exists '$tgtfnm'"
else
local logfnm="$fpath$sansext.log" # ; echo "logfnm=$logfnm'"
if [[ -f "$logfnm" ]]; then
local logver ; read -r logver < "$logfnm"
if ((force)) || [[ "$logver" != "$ecbin_version" ]]; then
((optq!=0)) || echo "logver != ecbin_version: $logver != $ecbin_version"
rm -f "$logfnm"
fi
fi
if [[ -f "$logfnm" && "$logfnm" -nt "$srcfnm" ]]; then
((optq!=0)) || echo "skipping: logfile exists '$tgtfnm'"
else
echo "creating: $tgtfnm"
if >"$logfnm" 2>&1 convert_to_pdf "$srcfnm" "$tgtfnm"; then
# echo "log deleted"
rm -f "${logfnm:?}"
else
echo "FAILED: see '${logfnm}'"
fi
fi
fi
fi
}
# parse options
argerr() { die "usage: $0 [-q] FILE..." ; }
while getopts "fqh" opt; do
case $opt in
f) force=1 ;;
q) optq=1 ;;
h) argerr;;
\?) argerr;;
esac
done
shift "$((OPTIND-1))" # shift so that $@, $1, etc. refer to the non-option arguments
for fnm in "$@"; do
do1file "$fnm"
done