calibre-portable.sh 15 KB

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