From 9a7042d2de2260ca3026ce5d608637607d7040b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=B3nya=20M=C3=A1rton?= Date: Mon, 1 Mar 2021 23:07:05 +0100 Subject: [PATCH] added script --- calibre-remote.desktop | 10 ++++ calibre-remote.sh | 113 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 calibre-remote.desktop create mode 100755 calibre-remote.sh diff --git a/calibre-remote.desktop b/calibre-remote.desktop new file mode 100644 index 0000000..36a1ead --- /dev/null +++ b/calibre-remote.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=Calibre Remote +GenericName=Remote e-book library management +Comment=It's Calibre, but configured to work with a remote Calibre library +Exec=calibre-remote +Terminal=true +Icon=calibre-gui +Type=Application +Categories=Office; +StartupNotify=false diff --git a/calibre-remote.sh b/calibre-remote.sh new file mode 100755 index 0000000..9eb0e1c --- /dev/null +++ b/calibre-remote.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +# Declaring the folder and database paths +LOCAL_CALIBRE_DB="" +REMOTE_CALIBRE_DB="" +REMOTE_CALIBRE_LIBRARY="" + +# Declaring Calibre-Web db reconnect URL (leave it to "NaN" if not used) +CALIBRE_WEB_URL="NaN" + +# The function that will handle starting Calibre and copying the metadata.db after Calibre exits +start_calibre() { + echo "Starting up Calibre..." + + # Calling Calibre with the set remote library + calibre --with-library $CALIBRE_LIBRARY_DIRECTORY + + echo "Calibre exited!" + + # Checking if the user edited anything + if ! cmp $LOCAL_CALIBRE_DB $REMOTE_CALIBRE_DB >/dev/null 2>&1; then + # If the user edited anything copying the new metadata.db to the server automatically + echo "Local database edited. Copying it to remote database..." + cp -f $LOCAL_CALIBRE_DB $REMOTE_CALIBRE_DB + echo "Copied!" + if [[ $CALIBRE_WEB_URL = "NaN" ]]; then + echo "Calibre-Web URL not defined. Proceeding without reconnecting Calibre-Web to database." + else + curl $CALIBRE_WEB_URL + echo "Calibre-Web reconnected to database." + fi + read -n 1 -s -r -p "Press any key to exit..." + exit + else + # If nothing was edited prompt the user if they want to copy metadata.db or not + echo "You did not edit the the database, so it is not necessarry to overwrite remote db. Do you want to? (y/n)" + read ANSWER + if [[ $ANSWER = "y" ]]; then + cp -f $LOCAL_CALIBRE_DB $REMOTE_CALIBRE_DB + echo "Copied!" + if [[ $CALIBRE_WEB_URL = "NaN" ]]; then + echo "Calibre-Web URL not defined. Proceeding without reconnecting Calibre-Web to database." + else + curl $CALIBRE_WEB_URL + echo "Calibre-Web reconnected to database." + fi + read -n 1 -s -r -p "Press any key to exit..." + exit + + else + echo "Database won't be copied!" + read -n 1 -s -r -p "Press any key to exit..." + exit + fi + fi +} + +# Checking if the remote library is mounted +if [ -d $REMOTE_CALIBRE_LIBRARY ]; then + CALIBRE_LIBRARY_DIRECTORY=$REMOTE_CALIBRE_LIBRARY + echo "Remote library found: $CALIBRE_LIBRARY_DIRECTORY" + + # Checking if metadata.db is present on the local storage + if [ -f $LOCAL_CALIBRE_DB ]; then + + # If local metadata.db exists check if the remote metadata.db was edited on the server + if ! cmp $LOCAL_CALIBRE_DB $REMOTE_CALIBRE_DB >/dev/null 2>&1; then + # If it was edited, prompting the user if they want to copy the remote one to the local storage + echo "Remote and local databases are different. Do you want to overwrite local db? (y/n): " + read ANSWER + if [[ $ANSWER = "y" ]]; then + cp -f $REMOTE_CALIBRE_DB $LOCAL_CALIBRE_DB + else + echo "Cannot start Calibre without an up-to-date database!" + read -n 1 -s -r -p "Press any key to exit..." + exit 1 + fi + fi + + # Setting up the env variable for Calibre + export CALIBRE_OVERRIDE_DATABASE_PATH=$LOCAL_CALIBRE_DB + echo "Metadata db found: $CALIBRE_OVERRIDE_DATABASE_PATH" + + # Start Calibre with the correct remote Library path and local metadata.db path + start_calibre + + else + # If local metadata.db doesn't exists copy it from the server + echo "Metadata db not found, trying to copy it from remote server..." + if [ -f $REMOTE_CALIBRE_DB ]; then + cp $REMOTE_CALIBRE_DB $LOCAL_CALIBRE_DB + echo "Database copied succesfully\!" + export CALIBRE_OVERRIDE_DATABASE_PATH=$LOCAL_CALIBRE_DB + echo "Metadata db now exists: $CALIBRE_OVERRIDE_DATABASE_PATH" + + # Start Calibre with the correct remote Library path and local metadata.db path + start_calibre + + else + # If the remote metadata.db cannot be found exit the program + echo "Cannot find database on remote server!" + read -n 1 -s -r -p "Press any key to exit..." + exit 1 + fi + fi + +else + # If the remote library cannot be found exit the program + echo "Remote library not found!" + read -n 1 -s -r -p "Press any key to exit..." + echo "Exiting..." + exit 1 +fi