-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent
276 lines (254 loc) · 9.08 KB
/
content
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh
# prerequisite programs
# http://komary.net/sh_ivtool/
SH_IVTOOL=/mnt/ext1/system/bin/sh_ivtool
# http://komary.net/unzip/
UNZIP=/mnt/ext1/system/bin/unzip
# http://komary.net/zip/
ZIP=/mnt/ext1/system/bin/zip
# default directories
sysbin="/mnt/ext1/system/bin"
config="/mnt/ext1/system/config"
openwith="/mnt/ext1/system/config/handlers.cfg"
usrapps="/mnt/ext1/applications"
# remove "Edit Book info" as default "open with" program for epub - next open of book will be with ereader
if [ -e "${openwith}" ]; then
mv "$openwith" "${openwith}.old"
IFS=''; cat "${openwith}.old" |
while read data; do
if echo "$data" | grep -q "Edit Book info"; then :
else echo "${data}" >> "$openwith"; fi
done
fi
if [ -e "${openwith}.old" ]; then rm "${openwith}.old"; fi
# if script is ran from applications, treat as initial setup
picview=/ebrmain/cramfs/bin/picviewer.app
bootlogo=/ebrmain/logo/bootlogo/PocketBook.bmp
scriptDir=$(dirname "$0")
scriptName=$(basename "$0" .app)
if [ "$scriptDir" == "/mnt/ext1/applications" ]; then
if [ ! -e "${config}/extensions.cfg" ];then
cp "/ebrmain/config/extensions.cfg" "${config}/extensions.cfg"
fi
# adds edit-epub option to extensions.cfg
mv "${config}/extensions.cfg" "${config}/extensions.cfg.old"
IFS=''; cat "${config}/extensions.cfg.old" |
while read data; do
if echo "$data" | grep -q "epub:@EPUB_file"; then
if echo "$data" | grep -q "Edit Book info"; then
# epub line on .cfg already correct
echo "${data}" >> "${config}/extensions.cfg"
else
# add edit-epub to .cfg
echo "${data%:*},Edit Book info:ICON_EPUB" >> "${config}/extensions.cfg"
fi
else
# non epub related line
echo "${data}" >> "${config}/extensions.cfg"
fi
done
if [ -e "${config}/extensions.cfg.old" ]; then rm "${config}/extensions.cfg.old"; fi
# unpack pre-reqs (sh-ivtool, zip and unzip) to system/bin
TarGz=`awk '/^__TARGZ__/ {print NR + 1; exit 0; }' $0`
tail -n+$TarGz $0 | tar xz -C /var/tmp
tar xzf /var/tmp/files
if [ -e "/var/tmp/files" ]; then rm "/var/tmp/files"; fi
# copy script to system/bin, delete orig, then reboot
if [ ! -e "$sysbin/$scriptName" ]; then
cp "${scriptDir}/${scriptName}.app" "$sysbin/Edit Book info"
rm "${scriptDir}/${scriptName}.app"
fi
dialog 1 "" "Setup Complete!" "RESTART"
if [ $? = 1 ]; then $picview "$bootlogo" & iv2sh reboot; exit 0; fi
iv2sh reboot
exit 0
fi
# detect if series template exists and if user selects option extracts book data from it
proceed=0; while [ $proceed == 0 ]; do
search_dir="/mnt/ext1/Series Templates"
fname=""
sname=""
title=""
seriesName=""
seriesNumb=""
genre=""
message=""
jump=0
skip=0
pass=0
count1=0
noTemplate=0
# option for quick exit if launched in error
message="- Book info Editor - \n\nChoose method of edit:"
dialog 1 "" "$(echo -e $message)" "Manual" "Template" "Exit"
ANS=$?
if [ $ANS == 2 ]; then pass=1; fi
if [ $ANS == 3 ]; then exit 0; fi
if [ $pass == 1 ]; then
if [ -d "$search_dir" ]; then
count1=`ls -1 $search_dir/*.tif 2>/dev/null | wc -l`
if [ $count1 == 0 ]; then
noTemplate=1
else
count2=0
results="Enter series template number or enter details manually:"$(echo -e "\n ")
for entry in "$search_dir"/*.tif; do
let count2++
entry=`basename "${entry}" .tif`
results=${results}$(echo -e "\n${count2} - ${entry}")
done
dialog 0 "" "$results" "Enter Number" "Enter Manually" "Exit"
ANS=$?
if [ $ANS == 3 ]; then exit 0; fi
if [ $ANS == 1 ]; then
selectNumb=`$SH_IVTOOL -n "Enter Series Template number:"`
count3=0
for entry in "$search_dir"/*.tif; do
let count3++
entry=`basename "${entry}"`
if [ $count3 == $selectNumb ]; then break; fi
done
jump=1
fname=$(sed '1q;d' "$search_dir/$entry")
sname=$(sed '2q;d' "$search_dir/$entry")
seriesName=$(sed '3q;d' "$search_dir/$entry")
genre=$(sed '4q;d' "$search_dir/$entry")
title=`$SH_IVTOOL -t "Enter book title: (leave blank to skip)"`
seriesNumb=`$SH_IVTOOL -n "Enter book number in series: (leave blank to skip)"`
fi
fi
else
noTemplate=1
fi
if [ $noTemplate == 1 ]; then
dialog 3 "" "No Series Templates found!" "Enter manually" "Exit"
ANS=$?
if [ $ANS == 2 ]; then exit 0; fi
fi
fi
# get user input for book meta-data - section skipped if series template option selected
if [ $jump == 0 ]; then
fname=`$SH_IVTOOL -t "Enter author's firstname: (leave blank to skip)"`
if [ "$fname" != "" ]; then sname=`$SH_IVTOOL -t "Enter author's surname: (leave blank to skip)"`; fi
title=`$SH_IVTOOL -t "Enter book title: (leave blank to skip)"`
seriesName=`$SH_IVTOOL -t "Enter name of book series: (leave blank to skip)"`
if [ "$seriesName" != "" ]; then seriesNumb=`$SH_IVTOOL -n "Enter book number in series: (leave blank to skip)"`; fi
genre=`$SH_IVTOOL -t "Enter genre of book: (leave blank to skip)"`
# create series template if sufficient user input gained
if [ $fname != "" -a $seriesName != "" -a $genre != "" ]; then
if [ ! -d "$search_dir" ]; then mkdir "$search_dir"; fi
echo $fname > "$search_dir/$fname $sname - $seriesName.tif"
echo $sname >> "$search_dir/$fname $sname - $seriesName.tif"
echo $seriesName >> "$search_dir/$fname $sname - $seriesName.tif"
echo $genre >> "$search_dir/$fname $sname - $seriesName.tif"
fi
fi
# write output for details to be changed
if [ "$fname" != "" ]; then message="Author: ${fname} ${sname}"; fi
if [ "$title" != "" ]; then message=${message}$(echo -e "\nTitle: ${title}"); fi
if [ "$seriesName" != "" ]; then message=${message}$(echo -e "\nSeries: ${seriesName}"); fi
if [ "$seriesNumb" != "" ]; then message=${message}$(echo -e "\nBook#: ${seriesNumb}"); fi
if [ "$genre" != "" ]; then message=${message}$(echo -e "\nGenre: ${genre}"); fi
# "no changes made" exit option
if [ "$fname" == "" -a "$title" == "" -a "$seriesName" == "" -a "$genre" == "" ]; then
dialog 1 "" "No changes made" "Start over" "Exit"
if [ $? == 2 ]; then exit 0; fi
skip=1
fi
# confirmation
if [ $skip == 0 ]; then
dialog 0 "" "$message" "Accept" "Start over" "Exit"
ANS=$?
if [ $ANS == 1 ]; then break; fi
if [ $ANS == 3 ]; then exit 0; fi
fi
done
# This is where we temporarily unpack the EPUB files.
# (It has to be inside a Unix directory, because unpacking and packing
# within a FAT directory seems to produce invalid EPUB files.)
BASENAME=`basename "$1"`
TMPDIR="/tmp/${BASENAME}.dir"
# Delete the temporary files later, no matter how we leave.
trap 'rm -rf "$TMPDIR"' 0
# Unpack the files.
mkdir -p "$TMPDIR"
cd "$TMPDIR"
$UNZIP -q -u "$1" -d "$TMPDIR"
if [ $? -gt 0 ]; then
$SH_IVTOOL -s "Problems while unzipping. Giving up!"
exit -1
fi
# backup and remove data from .opf
for f in `find "./" -iname "*.opf" -type f -print`; do
cp "$f" "${f}.orig"
mv "$f" "${f}.old"
IFS=''; cat "${f}.old" |
while read data; do
if echo "$data" | grep -q "dc:title"; then
if [ "$title" == "" ]; then echo "${data}" >> "${f}"; fi
elif echo "$data" | grep -q "dc:creator"; then
if [ "$fname" == "" ]; then echo "${data}" >> "${f}"; fi
elif echo "$data" | grep -q "dc:subject"; then
if [ "$genre" == "" ]; then echo "${data}" >> "${f}"; fi
elif echo "$data" | grep -q "calibre:series"; then
if [ "$seriesName" == "" ]; then echo "${data}" >> "${f}"; fi
else
echo "${data}" >> "${f}"
fi
done
done
rm "${f}.old"
# add user input to .opf
count=0
for f in `find "./" -iname "*.opf" -type f -print`; do
# backup/keep original .opf
mv "$f" "${f}.old"
# create new .opf
IFS=''; cat "${f}.old" |
while read data; do
if echo "$data" | grep -q "metadata"; then
let count++
if [ $count == 1 ]; then
echo "${data}" >> "${f}"
if [ "$title" != "" ]; then echo '<dc:title>'$title'</dc:title>"' >> "${f}"; fi
if [ "$fname" != "" ]; then echo '<dc:creator opf:role="aut" opf:file-as="'$sname', '$fname'">'$fname' '$sname'</dc:creator>' >> "${f}"; fi
if [ "$genre" != "" ]; then echo '<dc:subject>'$genre'</dc:subject>' >> "${f}"; fi
if [ "$seriesName" != "" ]; then echo '<meta name="calibre:series" content="'$seriesName'"/>' >> "${f}"; fi
if [ "$seriesName" != "" ]; then echo '<meta name="calibre:series_index" content="'$seriesNumb'"/>' >> "${f}"; fi
else
echo "${data}" >> "${f}"
fi
else
echo "${data}" >> "${f}"
fi
done
done
rm "${f}.old"
# Get the name of the output file
NEWFILE=`basename "$1"`
RESP=`$SH_IVTOOL -q "Overwrite the original EPUB file?"`
if [ $? -ne 0 -o "$RESP" == 'n' ]; then
repeat=0
while [ "${repeat}" == "0" ]; do
NEWFILE=`$SH_IVTOOL -t "Enter new filename (without .epub)"`.epub
if [ $? -ne 0 -o "$NEWFILE" == ".epub" ]; then
RESP=`$SH_IVTOOL -q "No filename entered. Exit edit-epub?"`
if [ "$RESP" == 'y' ]; then exit -1; fi
else repeat=1
fi
done
fi
# Zip up the contents
$ZIP "$NEWFILE" -0 mimetype
$ZIP -r "$NEWFILE" * -x mimetype
# Move the new file into the same directory as the old file
# (overwriting it if the names are the same)
mv "$NEWFILE" `dirname "$1"`
if [ "${BASENAME}" == "${NEWFILE}" ]; then
$SH_IVTOOL -s "EPUB file overwritten."
else
$SH_IVTOOL -s "New EPUB file created."
fi
exit 0
__TARGZ__
< prerequisite files in tar file >