-
Notifications
You must be signed in to change notification settings - Fork 1
/
fud
executable file
·150 lines (136 loc) · 4.97 KB
/
fud
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
CONFIGFILE="$HOME/.fud/fud.conf"
LOGFILE="$HOME/.fud/fud.log"
ICON="/usr/share/app-install/icons/fud.png"
MOUNTPOINT="$HOME/.fud/remotedir"
#Colors
RED="\e[1;31m" #Red
GREEN="\e[1;32m" #Green
YELLOW="\e[1;33m" #Yellow
RESET="\e[0m" #Reset!
# NOTA DA RIMUOVERE: questa è una semplice funzione di stampa. Per ora non fa nulla di particolarmente straordinario, ma potrebbe essere utilizzata più in là per sopprimere agevolmente l'output nel terminale, se passata un'opzione come -silent.
# Print on screen
printScreen() { echo -e "$@"; }
# Test if software installation and server connection are ok
testStuff () {
# Check if $MOUNTPOINT is avaible
if [[ ! -e $MOUNTPOINT ]]; then
printScreen $RED"Error: "$RESET"mount point $MOUNTPOINT is not available. Type \`mkdir -p $MOUNTPOINT\` first, then restart this script.";
exit 1
fi
# Check if $MOUNTPOINT is writable
if [[ ! -w $MOUNTPOINT ]]; then
printScreen $RED"Error: "$RESET"mount point $MOUNTPOINT is not writable. Type \`chmod -R 755 $MOUNTPOINT\` first, then restart this script.";
exit 1
fi
# Check if inotify is installed
if ! which inotifywait &> /dev/null; then
printScreen $RED"Error: "$RESET"install inotify-tools first, then restart this script."
exit 1
fi
# Check if unison is installed
if ! which unison &> /dev/null; then
printScreen $RED"Error: "$RESET"install unison first, then restart this script.";
exit 1
fi
# Check if notify-send is installed
if ! which notify-send &> /dev/null; then
printScreen $RED"Error: "$RESET"install libnotify-bin first, then restart this script.";
exit 1
fi
# Check if user is in the fuse group
if ! groups | grep "fuse" &> /dev/null; then
printScreen $RED"Error: "$RESET"your user is not in the fuse group. Type \`addgroup $USER fuse\` as root first, then logout, login and restart this script.";
exit 1
fi
}
# Mount remote directory in $MOUNTPOINT
mountRemote() {
while :; do
# If $MOUNTPOINT is already mounted, skip
if cat /proc/mounts | grep $MOUNTPOINT &> /dev/null; then
break;
# Else, if $MOUNTPOINT is not already mounted
else
# Check for ssh connection
if [ "$METHOD" == "ssh" ]; then
# Check if sshfs is installed
if ! which sshfs &> /dev/null; then
printScreen $RED"Error:"$RESET" install sshfs first, then restart this script.";
exit 1
fi
# Try to mount the remote directory with sshfs and verify
if ! sshfs $LOGIN@$SERVER:$RPATH $MOUNTPOINT -p $PORT -C &> /dev/null; then
printScreen $RED"Error:"$RESET" impossible to login to ssh server. Check your server configuration and your internet connection first, then restart the script."
printScreen "Fud retry the connection in 30 seconds."
sleep 30
fi
# Check for ftp connection
elif [ "$METHOD" == "ftp" ]; then
# Check if curlftpfs is installed
if ! which curlftpfs &> /dev/null; then
printScreen $RED"Error:"$RESET" install curlftpfs first, then restart this script.";
exit 1
fi
# Try to mount the remote directory with curlftpfs and verify
if ! curlftpfs -v $SERVER:$PORT/$RPATH $MOUNTPOINT -o user=$LOGIN:$PASSWORD -o allow_other -o nonempty; then
printScreen $RED"Error:"$RESET" impossible to login to ftp server. Check your server configuration and your internet connection first, then restart the script."
printScreen "Fud retry the connection in 30 seconds."
sleep 30
fi
fi
# Continue while (and re-check for a connection)
continue
fi
done
}
# Notify how many files have been synchronized
notifySinc() {
# Check if at least one file has been synchronized
if [[ "$1" -ge "1" ]]; then
# If a file (and only one) has been synchronized
if [[ "$1" -eq "1" ]]; then
MESSAGE="$1 file have been synchronized"
# If more files has been synchronized
elif [[ "$1" -gt "1" ]]; then
MESSAGE="$1 files have been synchronized"
fi
# Notify how many files have been synchronized
notify-send --icon=$ICON "$MESSAGE"
printScreen $MESSAGE
fi
}
# Sync
syncDir() {
OUTPUT=$(unison $LPATH $MOUNTPOINT -logfile $LOGFILE $EXTRAOPTIONS -batch -auto -terse -confirmbigdel="false" -retry 3 2>&1)
# SYNCFILES will contain the number of files synchronized
SYNCFILES=$(echo `expr match "$OUTPUT" '.*\(([0-9]* item[s]* transferred\)'` | sed -n "s/(//g;s/item[s]* transferred//g;/[0-9]*/p")
# Notify how many files have been synchronized
notifySinc $SYNCFILES
}
# SCRIPT STARTS HERE
# Check if config file exists
if [[ -e $CONFIGFILE ]]; then
# Include config file
source $CONFIGFILE
# Perform test
testStuff
# Mount remote directory and sync
while :; do
# Mount remote directory
mountRemote
# Finally, sync dirs
syncDir
while inotifywait -r -e modify -e create -e move -e delete -e open $LPATH $MOUNTPOINT &> /dev/null; do
sleep 10
syncDir
done
# Sleep if remote folder is not mounted or if the synchronization failed
sleep 30
done
exit 0
# If config file doesn't exists
else
printScreen $RED"Error: "$RESET"create and set "$CONFIGFILE" first, then restart the script.";
exit 1
fi