Debian Codegraph @

resources/calibre-portable.sh

raw
1 #!/bin/bash
2 # Calibre-Portable.sh
3 # ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
4 #
5 # Shell script file to start a calibre configuration on Linux
6 # giving explicit control of the location of:
7 # - calibre program files
8 # - calibre library files
9 # - calibre config files
10 # - calibre metadata database
11 # - calibre source files
12 # - calibre temp files
13 # By setting the paths correctly it can be used to run:
14 # - A "portable calibre" off a USB stick.
15 # - A network installation with local metadata database
16 # (for performance) and books stored on a network share
17 # - A local installation using customised settings
18 #
19 # If trying to run off a USB stick then the folder structure
20 # shown below is recommended (relative to the location of
21 # this script file). This structure can also be used when
22 # running of a local hard disk if you want to get the level
23 # of control this script file provides.
24 # - Calibre Location of Linux program files
25 # - CalibreConfig Location of configuration files
26 # - CalibreLibrary Location of books and metadata
27 # - CalibreSource Location of calibre source files (optional)
28 #
29 # This script file is designed so that if you create the recommended
30 # folder structure then it can be used 'as is' without modification.
31 # To use your own structure, simply set the variables in the generated configuration file.
32 #
33 # More information on the environment variables used by calibre can
34 # be found at:
35 # https://manual.calibre-ebook.com/customize.html#environment-variables
36 #
37 # NOTE: It is quite possible to have both Windows and Linux binaries on the same
38 # USB stick but set up to use the same calibre settings otherwise.
39 # In this case you use:
40 # - calibre-portable.bat to run the Windows version
41 # - calibre-portable.sh to run the Linux version
42 #
43 # CHANGE HISTORY
44 # ¬¬¬¬¬¬¬¬¬¬¬¬¬¬
45 # 22 Jan 2012 itimpi ----- First version based on the calibre-portable.bat file for Windows
46 # It should have identical functionality but for a Linux environment.
47 # It might work on MacOS but that has not been validated.
48 #
49 # 02 Feb 2015 eschwartz -- Fix path issues, allow setting each location in one variable, allow
50 # specifying a list of libraries in descending order of priority.
51 #
52 # 01 Apr 2015 eschwartz -- Fix temp dir and permissions, migrate settings to configuration file.
53
54 # -----------------------------------------------------
55 # On exit, make sure all files are marked world-writable.
56 # This allows you to use calibre on other computers
57 # without changing fstab rules and suchlike.
58 # You can now use an ext3 drive instead of vfat so the
59 # binaries and script will be executable.
60 # -----------------------------------------------------
61
62 cleanup() {
63 # Check if user has disabled cleanup
64 if [[ "${CALIBRE_NO_CLEANUP}" = "1" ]]; then
65 return
66 fi
67
68 for i in "${CONFIG_DIR}" "${CALIBRE_LIBRARY_DIRECTORY}" \
69 "${METADATA_DIR}" "${SRC_DIR}" "${BIN_DIR}"; do
70 if [[ -d "${i}" ]]; then
71 chmod a+rwx "${i}"
72 fi
73 done
74 rm -rf "${CALIBRE_TEMP_DIR}"
75 }
76
77 trap cleanup EXIT
78
79 # ------------------------------------------------
80 # Interactive options.
81 # ------------------------------------------------
82
83 usage()
84 {
85 cat <<- _EOF_
86 Usage: calibre-portable.sh [OPTIONS]
87 Run a portable instance of calibre.
88
89 OPTIONS
90 -u, --upgrade-install upgrade or install the portable calibre binaries
91 -h, --help show this usage message then exit
92 _EOF_
93 }
94
95 do_upgrade()
96 {
97 wget -nv -O- https://raw.githubusercontent.com/kovidgoyal/calibre/master/setup/linux-installer.py \
98 | python -c "import sys; main=lambda x,y:sys.stderr.write('Download failed\n'); \
99 exec(sys.stdin.read()); main('$(pwd)', True)"
100 }
101
102 while [[ "${#}" -gt 0 ]]; do
103 case "${1}" in
104 -h|--help)
105 usage
106 exit
107 ;;
108 -u|--upgrade-install)
109 do_upgrade
110 exit
111 ;;
112 *)
113 echo "calibre-portable.sh: unecognzed option '${1}'"
114 echo "Try 'calibre-portable.sh --help' for more information."
115 exit 1
116 esac
117 shift
118 done
119
120 # ------------------------------------------------
121 # Create or read configuration file.
122 # ------------------------------------------------
123
124 if [[ -f "$(pwd)/calibre-portable.conf" ]]; then
125 source "$(pwd)/calibre-portable.conf"
126 else
127 cat <<- _EOF_ > $(pwd)/calibre-portable.conf
128 # Configuration file for calibre-portable. Generated on $(date)
129 # Settings in here will override the defaults specified in the portable launcher.
130
131 ##################################################
132 # Set up calibre config folder.
133 #
134 # This is where user specific settings are stored.
135 ##################################################
136
137 # CONFIG_DIR="\$(pwd)/CalibreConfig"
138
139 ################################################################
140 # -- Specify the location of your calibre library.
141 #
142 # -- Either set an explicit path, or if running from a USB stick
143 # -- a relative path can be used to avoid needing to know the
144 # -- mount point of the USB stick.
145 #
146 # -- Specify a list of libraries here, by adding new elements to the
147 # -- array. The first value of LIBRARY_DIRS that is an existing directory
148 # -- will be used as the current calibre library.
149 ################################################################
150
151 # LIBRARY_DIRS[0]="/path/to/first/CalibreLibrary"
152 # LIBRARY_DIRS[1]="/path/to/second/CalibreLibrary"
153 # LIBRARY_DIRS[2]="\$(pwd)/CalibreLibrary"
154
155 ################################################################
156 # -- Specify location of metadata database (optional).
157 #
158 # -- Location where the metadata.db file is located. If not set
159 # -- then the same location as the library folder will be assumed.
160 # -- This option is typically used to get better performance when the
161 # -- library is on a (slow) network drive. Putting the metadata.db
162 # -- file locally then makes gives a big performance improvement.
163 #
164 # -- NOTE. If you use this option, then the ability to switch
165 # -- libraries within calibre will be disabled. Therefore
166 # -- you do not want to set it if the metadata.db file
167 # -- is at the same location as the book files.
168 #
169 # -- Another point to watch is that plugins can cause problems
170 # -- as they often store absolute path information.
171 ################################################################
172
173 # METADATA_DIR="\$(pwd)/CalibreMetadata"
174
175 ################################################################
176 # -- Specify location of source (optional).
177 #
178 # -- It is easy to run calibre from source. Just set the environment
179 # -- variable to where the source is located. When running from source
180 # -- the GUI will have a '*' after the version number that is displayed
181 # -- at the bottom of the calibre main screen.
182 #
183 # -- More information on setting up a development environment can
184 # -- be found at:
185 # -- https://manual.calibre-ebook.com/develop.html#develop
186 ################################################################
187
188 # SRC_DIR="$\(pwd)/CalibreSource/src"
189
190 ################################################################
191 # -- Specify location of calibre Linux binaries (optional).
192 #
193 # -- To avoid needing calibre to be set in the search path, ensure
194 # -- that if calibre program files exists, we manually specify the
195 # -- location of the binary.
196 # -- The following test falls back to using the search path, or you
197 # -- can specifically use the search path by leaving the BIN_DIR blank.
198 #
199 # -- This folder can be populated by copying the /opt/calibre folder
200 # -- from an existing installation or by installing direct to here.
201 #
202 # -- NOTE. Do not try and put both Windows and Linux binaries into
203 # -- the same folder as this can cause problems.
204 ################################################################
205
206 # BIN_DIR="$\(pwd)/calibre"
207
208 ################################################################
209 # -- Location of calibre temporary files (optional).
210 #
211 # -- calibre creates a lot of temporary files while running
212 # -- In theory these are removed when calibre finishes, but
213 # -- in practice files can be left behind (particularly if
214 # -- a crash occurs). Using this option allows some
215 # -- explicit clean-up of these files.
216 # -- If not set calibre uses the normal system TEMP location
217 ################################################################
218
219 # CALIBRE_TEMP_DIR="/tmp/CALIBRE_TEMP_\$(tr -dc 'A-Za-z0-9'</dev/urandom |fold -w 7 | head -n1)"
220
221 ################################################################
222 # -- Set the interface language (optional).
223 #
224 # -- Defaults to whatever is stored in Preferences
225 ################################################################
226
227 # CALIBRE_OVERRIDE_LANG="EN"
228
229 ##################################################################
230 # -- Wait until user can check the settings before starting calibre.
231 # -- (default=yes)
232 #
233 # -- Set CALIBRE_NOCONFIRM_START to "1" to disable.
234 # -- Set CALIBRE_NOCONFIRM_START to "0" to enable.
235 ##################################################################
236
237 # CALIBRE_NOCONFIRM_START=0
238
239 ##################################################################
240 # -- Cleanup all files when done (default=yes).
241 #
242 # -- On launcher termination, change all library, configuration,
243 # -- source, binary, etc. files to world-writable, and remove the
244 # -- temporary folder.
245 #
246 # -- Technically this is a security risk. This is a portableapp, so
247 # -- we don't care. Disable this if *you* do.
248 #
249 # -- Set CALIBRE_NO_CLEANUP to "1" to disable.
250 # -- Set CALIBRE_NO_CLEANUP to "0" to enable.
251 ##################################################################
252
253 # CALIBRE_NO_CLEANUP=0
254
255 ##################################################################
256 # -- Set and export any other environment variables needed. More
257 # -- information can be found at:
258 # https://manual.calibre-ebook.com/customize.html#environment-variables
259 ##################################################################
260
261 # some other example variables calibre recognizes:
262 # export CALIBRE_NO_NATIVE_FILEDIALOGS=
263 # export CALIBRE_NO_NATIVE_MENUBAR=
264 # export CALIBRE_IGNORE_SYSTEM_THEME=
265 # export http_proxy=
266 _EOF_
267
268 echo "Generating default configuration file at $(pwd)/calibre-portable.conf"
269 echo "Set any non-default options here."
270 fi
271
272 # ------------------------------------------------
273 # Set up calibre config folder.
274 # ------------------------------------------------
275
276 : ${CONFIG_DIR:="$(pwd)/CalibreConfig"}
277
278 if [[ -d "${CONFIG_DIR}" ]]; then
279 export CALIBRE_CONFIG_DIRECTORY="${CONFIG_DIR}"
280 echo "CONFIG FILES: ${CONFIG_DIR}"
281 else
282 echo -e "\033[0;31mCONFIG FILES: Not found\033[0m"
283 fi
284 echo "--------------------------------------------------"
285
286 # --------------------------------------------------------------
287 # Specify the location of your calibre library.
288 # --------------------------------------------------------------
289
290 : ${LIBRARY_DIRS[0]:="/path/to/first/CalibreLibrary"}
291 : ${LIBRARY_DIRS[1]:="/path/to/second/CalibreLibrary"}
292 : ${LIBRARY_DIRS[2]:="$(pwd)/CalibreLibrary"}
293
294 for LIBRARY_DIR in "${LIBRARY_DIRS[@]}"; do
295 if [[ -d "${LIBRARY_DIR}" ]]; then
296 CALIBRE_LIBRARY_DIRECTORY="${LIBRARY_DIR}"
297 echo "LIBRARY FILES: ${CALIBRE_LIBRARY_DIRECTORY}"
298 break
299 fi
300 done
301
302 [[ -z "${CALIBRE_LIBRARY_DIRECTORY}" ]] && echo -e "\033[0;31mLIBRARY FILES: Not found\033[0m"
303 echo "--------------------------------------------------"
304
305 # --------------------------------------------------------------
306 # Specify location of metadata database (optional).
307 # --------------------------------------------------------------
308
309 : ${METADATA_DIR:="$(pwd)/CalibreMetadata"}
310
311 if [[ -f "${METADATA_DIR}/metadata.db" && "${CALIBRE_LIBRARY_DIRECTORY}" != "${METADATA_DIR}" ]]; then
312 export CALIBRE_OVERRIDE_DATABASE_PATH="${METADATA_DIR}/metadata.db"
313 echo "DATABASE: ${METADATA_DIR}/metadata.db"
314 echo
315 echo -e "\033[0;31m***CAUTION*** Library Switching will be disabled\033[0m"
316 echo
317 echo "--------------------------------------------------"
318 fi
319
320 # --------------------------------------------------------------
321 # Specify location of source (optional).
322 # --------------------------------------------------------------
323
324 : ${SRC_DIR:="$(pwd)/CalibreSource/src"}
325
326 if [[ -d "${SRC_DIR}" ]]; then
327 export CALIBRE_DEVELOP_FROM="${SRC_DIR}"
328 echo "SOURCE FILES: ${SRC_DIR}"
329 else
330 echo "SOURCE FILES: *** Not being Used ***"
331 fi
332 echo "--------------------------------------------------"
333
334 # --------------------------------------------------------------
335 # Specify location of calibre Linux binaries (optional).
336 # --------------------------------------------------------------
337
338 : ${BIN_DIR:="$(pwd)/calibre"}
339
340 if [[ -d "${BIN_DIR}" ]]; then
341 CALIBRE="${BIN_DIR}/calibre"
342 echo "PROGRAM FILES: ${BIN_DIR}"
343 elif [[ -z "${BIN_DIR}" ]]; then
344 CALIBRE="calibre"
345 echo "PROGRAM FILES: Using system search path"
346 else
347 CALIBRE="calibre"
348 echo "PROGRAM FILES: No portable copy found."
349 echo "To install a portable copy, run './calibre-portable.sh --upgrade-install'"
350 echo -e "\033[0;31m*** Using system search path instead***\033[0m"
351 fi
352 echo "--------------------------------------------------"
353
354 # --------------------------------------------------------------
355 # Location of calibre temporary files (optional).
356 # --------------------------------------------------------------
357
358 : ${CALIBRE_TEMP_DIR:="/tmp/CALIBRE_TEMP_$(tr -dc 'A-Za-z0-9'</dev/urandom |fold -w 7 | head -n1)"}
359
360 if [[ ! -z "${CALIBRE_TEMP_DIR}" ]]; then
361 export CALIBRE_TEMP_DIR
362 export CALIBRE_CACHE_DIRECTORY="${CALIBRE_TEMP_DIR}/calibre_cache"
363 echo "TEMPORARY FILES: ${CALIBRE_TEMP_DIR}"
364 echo "--------------------------------------------------"
365 rm -rf "${CALIBRE_TEMP_DIR}"
366 mkdir "${CALIBRE_TEMP_DIR}"
367 mkdir "${CALIBRE_CACHE_DIRECTORY}"
368 fi
369
370 # --------------------------------------------------------------
371 # Set the interface language (optional).
372 # --------------------------------------------------------------
373
374 if [[ "${CALIBRE_OVERRIDE_LANG}" != "" ]]; then
375 export CALIBRE_OVERRIDE_LANG
376 echo "INTERFACE LANGUAGE: ${CALIBRE_OVERRIDE_LANG}"
377 fi
378
379 # ---------------------------------------------------------------
380 # Wait until user can check the settings before starting calibre.
381 # ---------------------------------------------------------------
382
383 if [[ "${CALIBRE_NOCONFIRM_START}" != "1" ]]; then
384 echo
385 echo "Press CTRL-C if you do not want to continue"
386 echo "Press ENTER to continue and start calibre"
387 read DUMMY
388 fi
389
390 # --------------------------------------------------------
391 # Start up the calibre program.
392 # --------------------------------------------------------
393
394 echo "Starting up calibre from portable directory \"$(pwd)\""
395 $CALIBRE --with-library "${CALIBRE_LIBRARY_DIRECTORY}"