-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibre-remote.sh
executable file
·113 lines (97 loc) · 4.47 KB
/
calibre-remote.sh
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
#!/bin/bash
# Declaring the folder and database paths
LOCAL_CALIBRE_DB="<path to the locally stored metadata.db>"
REMOTE_CALIBRE_DB="<path to the remotely stored metadata.db>"
REMOTE_CALIBRE_LIBRARY="<path to the remote library root folder>"
# 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