calibre-portable.sh: misc fixes.

- Fix temp dir.
- Migrate settings to configuration file.
- Use trap to exit and cleanup. umask was doing absolutely nothing.

Fixes #1426222 [Updated version of calibre-portable.sh](https://bugs.launchpad.net/calibre/+bug/1426222)
This commit is contained in:
Eli Schwartz 2015-04-13 14:46:17 -04:00 committed by Kovid Goyal
parent 0dd3fc7f2e
commit afd774cc9f

311
resources/calibre-portable.sh Normal file → Executable file
View File

@ -28,7 +28,7 @@
#
# This script file is designed so that if you create the recommended
# folder structure then it can be used 'as is' without modification.
# To use your own structure, simply set the variables at the head of each block.
# To use your own structure, simply set the variables in the generated configuration file.
#
# More information on the Environment Variables used by Calibre can
# be found at:
@ -48,17 +48,33 @@
#
# 02 Feb 2015 eschwartz -- Fix path issues, allow setting each location in one variable, allow
# specifying a list of libraries in descending order of priority.
#
# 01 Apr 2015 eschwartz -- Fix temp dir and permissions, migrate settings to configuration file.
# -----------------------------------------------------
# Make all new files get created read-writable by world
# by default. This allows you to use calibre on other
# computers without changing fstab rules and suchlike.
#
# You can now use an ext3 drive instead of vfat and the
# On exit, make sure all files are marked world-writable.
# This allows you to use calibre on other computers
# without changing fstab rules and suchlike.
# You can now use an ext3 drive instead of vfat so the
# binaries and script will be executable.
# -----------------------------------------------------
umask 000
cleanup() {
# Check if user has disabled cleanup
if [[ "${CALIBRE_NO_CLEANUP}" = "1" ]]; then
return
fi
for i in "${CONFIG_DIR}" "${CALIBRE_LIBRARY_DIRECTORY}" \
"${METADATA_DIR}" "${SRC_DIR}" "${BIN_DIR}"; do
if [[ -d "${i}" ]]; then
chmod a+rwX "${i}"
fi
done
rm -rf "${CALIBRE_TEMP_DIR}"
}
trap cleanup EXIT
# ------------------------------------------------
# Interactive options.
@ -102,12 +118,162 @@ while [[ "${#}" -gt 0 ]]; do
done
# ------------------------------------------------
# Set up Calibre Config folder
#
# This is where user specific settings are stored.
# Create or read configuration file.
# ------------------------------------------------
CONFIG_DIR="$(pwd)/CalibreConfig"
if [[ -f "$(pwd)/calibre-portable.conf" ]]; then
source "$(pwd)/calibre-portable.conf"
else
cat <<- _EOF_ > $(pwd)/calibre-portable.conf
# Configuration file for calibre-portable. Generated on $(date)
# Settings in here will override the defaults specified in the portable launcher.
##################################################
# Set up calibre config folder.
#
# This is where user specific settings are stored.
##################################################
# CONFIG_DIR="\$(pwd)/CalibreConfig"
################################################################
# -- Specify the location of your calibre library.
#
# -- Either set an explicit path, or if running from a USB stick
# -- a relative path can be used to avoid needing to know the
# -- mount point of the USB stick.
#
# -- Specify a list of libraries here, by adding new elements to the
# -- array. The first value of LIBRARY_DIRS that is an existing directory
# -- will be used as the current calibre library.
################################################################
# LIBRARY_DIRS[0]="/path/to/first/CalibreLibrary"
# LIBRARY_DIRS[1]="/path/to/second/CalibreLibrary"
# LIBRARY_DIRS[2]="\$(pwd)/CalibreLibrary"
################################################################
# -- Specify location of metadata database (optional).
#
# -- Location where the metadata.db file is located. If not set
# -- then the same location as the library folder will be assumed.
# -- This option is typically used to get better performance when the
# -- library is on a (slow) network drive. Putting the metadata.db
# -- file locally then makes gives a big performance improvement.
#
# -- NOTE. If you use this option, then the ability to switch
# -- libraries within calibre will be disabled. Therefore
# -- you do not want to set it if the metadata.db file
# -- is at the same location as the book files.
#
# -- Another point to watch is that plugins can cause problems
# -- as they often store absolute path information.
################################################################
# METADATA_DIR="\$(pwd)/CalibreMetadata"
################################################################
# -- Specify location of source (optional).
#
# -- It is easy to run calibre from source. Just set the environment
# -- variable to where the source is located. When running from source
# -- the GUI will have a '*' after the version number that is displayed
# -- at the bottom of the calibre main screen.
#
# -- More information on setting up a development environment can
# -- be found at:
# -- http://manual.calibre-ebook.com/develop.html#develop
################################################################
# SRC_DIR="$\(pwd)/CalibreSource/src"
################################################################
# -- Specify location of calibre linux binaries (optional).
#
# -- To avoid needing Calibre to be set in the search path, ensure
# -- that if Calibre Program Files exists, we manually specify the
# -- location of the binary.
# -- The following test falls back to using the search path, or you
# -- can specifically use the search path by leaving the BIN_DIR blank.
#
# -- This folder can be populated by copying the /opt/calibre folder
# -- from an existing installation or by installing direct to here.
#
# -- NOTE. Do not try and put both Windows and Linux binaries into
# -- the same folder as this can cause problems.
################################################################
# BIN_DIR="$\(pwd)/calibre"
################################################################
# -- Location of calibre temporary files (optional).
#
# -- calibre creates a lot of temporary files while running
# -- In theory these are removed when calibre finishes, but
# -- in practice files can be left behind (particularly if
# -- a crash occurs). Using this option allows some
# -- explicit clean-up of these files.
# -- If not set calibre uses the normal system TEMP location
################################################################
# CALIBRE_TEMP_DIR="/tmp/CALIBRE_TEMP_\$(tr -dc 'A-Za-z0-9'</dev/urandom |fold -w 7 | head -n1)"
################################################################
# -- Set the Interface language (optional).
#
# -- Defaults to whatever is stored in Preferences
################################################################
# CALIBRE_OVERRIDE_LANG="EN"
##################################################################
# -- Wait until user can check the settings before starting calibre.
# -- (default=yes)
#
# -- Set CALIBRE_NOCONFIRM_START to "1" to disable.
# -- Set CALIBRE_NOCONFIRM_START to "0" to enable.
##################################################################
# CALIBRE_NOCONFIRM_START=0
##################################################################
# -- Cleanup all files when done (default=yes).
#
# -- On launcher termination, change all library, configuration,
# -- source, binary, etc. files to world-writable, and remove the
# -- temporary folder.
#
# -- Technically this is a security risk. This is a portableapp, so
# -- we don't care. Disable this if *you* do.
#
# -- Set CALIBRE_NO_CLEANUP to "1" to disable.
# -- Set CALIBRE_NO_CLEANUP to "0" to enable.
##################################################################
# CALIBRE_NO_CLEANUP=0
##################################################################
# -- Set and export any other environment variables needed. More
# -- information can be found at:
# http://manual.calibre-ebook.com/customize.html#environment-variables
##################################################################
# some other example variables calibre recognizes:
# export CALIBRE_NO_NATIVE_FILEDIALOGS=
# export CALIBRE_NO_NATIVE_MENUBAR=
# export CALIBRE_IGNORE_SYSTEM_THEME=
# export http_proxy=
_EOF_
echo "Generating default configuration file at $(pwd)/calibre-portable.conf"
echo "Set any non-default options here."
fi
# ------------------------------------------------
# Set up calibre config folder.
# ------------------------------------------------
: ${CONFIG_DIR:="$(pwd)/CalibreConfig"}
if [[ -d "${CONFIG_DIR}" ]]; then
export CALIBRE_CONFIG_DIRECTORY="${CONFIG_DIR}"
@ -118,25 +284,16 @@ fi
echo "--------------------------------------------------"
# --------------------------------------------------------------
# Specify Location of ebooks
#
# Location where Book files are located.
# Either set explicit path, or if running from a USB stick
# a relative path can be used to avoid need to know the
# drive letter of the USB stick.
#
# Specify a list of libraries here, by adding new elements to the
# array. The first value of LIBRARY_DIRS that is an existing directory
# will be used as the current calibre library.
# Specify the location of your calibre library.
# --------------------------------------------------------------
LIBRARY_DIRS[0]="/path/to/first/CalibreLibrary"
LIBRARY_DIRS[1]="/path/to/second/CalibreLibrary"
LIBRARY_DIRS[2]="$(pwd)/CalibreLibrary"
: ${LIBRARY_DIRS[0]:="/path/to/first/CalibreLibrary"}
: ${LIBRARY_DIRS[1]:="/path/to/second/CalibreLibrary"}
: ${LIBRARY_DIRS[2]:="$(pwd)/CalibreLibrary"}
for LIBRARY_DIR in "${LIBRARY_DIRS[@]}"; do
if [[ -d "${LIBRARY_DIR}" ]]; then
export CALIBRE_LIBRARY_DIRECTORY="${LIBRARY_DIR}"
CALIBRE_LIBRARY_DIRECTORY="${LIBRARY_DIR}"
echo "LIBRARY FILES: ${CALIBRE_LIBRARY_DIRECTORY}"
break
fi
@ -146,26 +303,12 @@ done
echo "--------------------------------------------------"
# --------------------------------------------------------------
# Specify Location of metadata database (optional)
#
# Location where the metadata.db file is located. If not set
# then the same location as Book files will be assumed. This
# option is typically used to get better performance when the
# Library is on a (slow) network drive. Putting the metadata.db
# file locally then makes gives a big performance improvement.
#
# NOTE. If you use this option, then the ability to switch
# libraries within Calibre will be disabled. Therefore
# you do not want to set it if the metadata.db file
# is at the same location as the book files.
#
# Another point to watch is that plugins can cause problems
# as they often store absolute path information.
# Specify location of metadata database (optional).
# --------------------------------------------------------------
METADATA_DIR="$(pwd)/CalibreMetadata"
: ${METADATA_DIR:="$(pwd)/CalibreMetadata"}
if [[ -f "${METADATA_DIR}/metadata.db" && "$CALIBRE_LIBRARY_DIRECTORY" != "${METADATA_DIR}" ]]; then
if [[ -f "${METADATA_DIR}/metadata.db" && "${CALIBRE_LIBRARY_DIRECTORY}" != "${METADATA_DIR}" ]]; then
export CALIBRE_OVERRIDE_DATABASE_PATH="${METADATA_DIR}/metadata.db"
echo "DATABASE: ${METADATA_DIR}/metadata.db"
echo
@ -175,19 +318,10 @@ if [[ -f "${METADATA_DIR}/metadata.db" && "$CALIBRE_LIBRARY_DIRECTORY" != "${MET
fi
# --------------------------------------------------------------
# Specify Location of source (optional)
#
# It is easy to run Calibre from source.
# Just set the environment variable to where the source is located.
# When running from source the GUI will have a '*' after the version
# number that is displayed at the bottom of the Calibre main screen.
#
# More information on setting up a development environment can
# be found at:
# http://manual.calibre-ebook.com/develop.html#develop
# Specify location of source (optional).
# --------------------------------------------------------------
SRC_DIR="$(pwd)/CalibreSource/src"
: ${SRC_DIR:="$(pwd)/CalibreSource/src"}
if [[ -d "${SRC_DIR}" ]]; then
export CALIBRE_DEVELOP_FROM="${SRC_DIR}"
@ -198,22 +332,10 @@ fi
echo "--------------------------------------------------"
# --------------------------------------------------------------
# Specify Location of calibre linux binaries (optional)
#
# To avoid needing Calibre to be set in the search path, ensure
# that if Calibre Program Files exists, we manually specify the
# location of the binary.
# The following test falls back to using the search path, or you
# can specifically use the search path by leaving the BIN_DIR blank.
#
# This folder can be populated by copying the /opt/calibre folder
# from an existing installation or by installing direct to here.
#
# NOTE. Do not try and put both Windows and Linux binaries into
# the same folder as this can cause problems.
# Specify location of calibre linux binaries (optional).
# --------------------------------------------------------------
BIN_DIR="$(pwd)/calibre"
: ${BIN_DIR:="$(pwd)/calibre"}
if [[ -d "${BIN_DIR}" ]]; then
CALIBRE="${BIN_DIR}/calibre"
@ -230,51 +352,44 @@ fi
echo "--------------------------------------------------"
# --------------------------------------------------------------
# Location of Calibre Temporary files (optional)
#
# Calibre creates a lot of temporary files while running
# In theory these are removed when Calibre finishes, but
# in practise files can be left behind (particularly if
# a crash occurs). Using this option allows some
# explicit clean-up of these files.
# If not set Calibre uses the normal system TEMP location
# Location of calibre temporary files (optional).
# --------------------------------------------------------------
CALIBRE_TEMP_DIR="/tmp/CALIBRE_TEMP"
echo "TEMPORARY FILES: ${CALIBRE_TEMP_DIR}"
echo "--------------------------------------------------"
rm -rf "${CALIBRE_TEMP_DIR}"
mkdir "${CALIBRE_TEMP_DIR}"
# set the following for any components that do
# not obey the CALIBRE_TEMP_DIR setting
: ${CALIBRE_TEMP_DIR:="/tmp/CALIBRE_TEMP_$(tr -dc 'A-Za-z0-9'</dev/urandom |fold -w 7 | head -n1)"}
if [[ ! -z "${CALIBRE_TEMP_DIR}" ]]; then
export CALIBRE_TEMP_DIR
export CALIBRE_CACHE_DIRECTORY="${CALIBRE_TEMP_DIR}/calibre_cache"
echo "TEMPORARY FILES: ${CALIBRE_TEMP_DIR}"
echo "--------------------------------------------------"
rm -rf "${CALIBRE_TEMP_DIR}"
mkdir "${CALIBRE_TEMP_DIR}"
mkdir "${CALIBRE_CACHE_DIRECTORY}"
fi
# --------------------------------------------------------------
# Set the Interface language (optional)
#
# Remove the if test to use this. ;)
# Set the Interface language (optional).
# --------------------------------------------------------------
if false; then
export CALIBRE_OVERRIDE_LANG="EN"
if [[ "${CALIBRE_OVERRIDE_LANG}" != "" ]]; then
export CALIBRE_OVERRIDE_LANG
echo "INTERFACE LANGUAGE: ${CALIBRE_OVERRIDE_LANG}"
fi
# ----------------------------------------------------------
# The following gives a chance to check the settings before
# starting Calibre. It can be commented out if not wanted.
# ----------------------------------------------------------
# ---------------------------------------------------------------
# Wait until user can check the settings before starting calibre.
# ---------------------------------------------------------------
echo
echo "Press CTRL-C if you do not want to continue"
echo "Press ENTER to continue and start Calibre"
read DUMMY
if [[ "${CALIBRE_NOCONFIRM_START}" != "1" ]]; then
echo
echo "Press CTRL-C if you do not want to continue"
echo "Press ENTER to continue and start Calibre"
read DUMMY
fi
# --------------------------------------------------------
# Start up the calibre program.
# --------------------------------------------------------
echo "Starting up Calibre from portable directory \"$(pwd)\""
$CALIBRE --with-library "$CALIBRE_LIBRARY_DIRECTORY"
$CALIBRE --with-library "${CALIBRE_LIBRARY_DIRECTORY}"