-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfudconf
executable file
·223 lines (206 loc) · 7.08 KB
/
fudconf
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
FUDDIR="$HOME/.fud"
FUDCONF="$FUDDIR/fud.conf"
MOUNTPOINT="$HOME/.fud/remotedir"
METHODS[1]="ssh"
METHODS[2]="ftp"
#Colors
RED="\e[1;31m" #Red
GREEN="\e[1;32m" #Green
RESET="\e[0m" #Reset!
# Print on screen
printScreen() { echo -e "$@"; }
printScreen "This script will configure Fud with interactive questions. For more information, consult the Fud documentation at https://github.com/mirkop88/Fud/wiki or look at the example file fud.sample-conf."
printScreen -n "Do you wanna configure Fud? (Y/n): "
read SURE
if [[ $SURE = "Y" || $SURE = "y" || $SURE = "" ]]; then
# ---
# PRELIMINARY ACTIONS
# ---
# Create $FUDDIR, if doesn't exists, and set permissions
if [ ! -e $FUDDIR ]; then
mkdir -p $FUDDIR &> /dev/null
printScreen "Create directory $FUDDIR."
fi
chmod -R 755 $FUDDIR &> /dev/null
# If $MOUNTPOINT is already mounted, unmount it
if ! cat /proc/mounts | grep $MOUNTPOINT &> /dev/null; then
# NOTA DA RIMUOVERE: siamo sicuri che riesca a smontarlo, visto che questo script è lanciato da un utente normale?
umount -f $MOUNTPOINT &> /dev/null
fi
# Create $MOUNTPOINT, if doesn't exists and set permissions
if [ ! -e $MOUNTPOINT ]; then
mkdir -p $MOUNTPOINT &> /dev/null
printScreen "Created $MOUNTPOINT for mounting the remote directory."
fi
chmod -R 755 $MOUNTPOINT &> /dev/null
# ---
# CONNECTION METHOD
# ---
# Executes the loop until a supported method is selected
while :; do
printScreen "Select the method to use to synchronize."
# Loop array METHODS, which contains supported methods
for (( i=1; i<=${tLen=${#METHODS[@]}}; i++ )); do
printScreen "[$i] ${METHODS[$i]}"
done
printScreen -n "Method: "
read METHOD
# If has been selected a supported method
if [[ ${METHODS[$METHOD]} != "" ]]; then
# Rewrite variable METHOD, which at this point will contain the name of the method chosen and can be used later to ask some questions related to the method chosen
METHOD=${METHODS[$METHOD]}
printScreen "You have selected $METHOD method."
# Write method in the configuration file
echo "METHOD=\"$METHOD\"" > $FUDCONF
# Break loop
break
# If has not been selected a supported method
else
printScreen ${RED}"Error"${RESET}": choose a method listed."
printScreen
# Continue loop
continue
fi
done
# ---
# SERVER FOR CONNECTION
# ---
# Executes the loop until a server for connection is entered
while :; do
printScreen -n "Server for connection (server name or ip address): "
read SERVER
# If a server has been correctly entered
if [[ $SERVER != "" ]]; then
# Write server in the configuration file
echo "SERVER=\"$SERVER\"" >> $FUDCONF
# Break loop
break
# If a server has not been correctly entered
else
printScreen ${RED}"Errore"${RESET}": specify a server for connection (server name or ip address)."
printScreen
# Continue loop
continue
fi
done
# ---
# USER FOR CONNECTION
# ---
# Executes the loop until a user for connection is entered
while :; do
printScreen -n "User for connection: "
read LOGIN
# If a user has been correctly entered
if [[ $LOGIN != "" ]]; then
# Write user in the configuration file
echo "LOGIN=\"$LOGIN\"" >> $FUDCONF
# Break loop
break
# If a user has not been correctly entered
else
printScreen ${RED}"Errore"${RESET}": specify a user for connection."
printScreen
# Continue loop
continue
fi
done
# ---
# PASSWORD FOR CONNECTION
# ---
printScreen "To connect to the server, you may need a password. However, this option is not necessary, because the specified user may not have a password on the server, or because you decided to use ssh with a certificate, for example. If it is not necessary to specify a password, skip this step."
printScreen -n "Password for connection: "
read PASSWORD
# Write password in the configuration file
echo "PASSWORD=\"$PASSWORD\"" >> $FUDCONF
# ---
# PORT FOR CONNECTION
# ---
# First, defines the default ports for each method
case $METHOD in
# Ssh method
ssh)
DPORT="21";;
# Ftp method
ftp)
DPORT="22";;
esac
printScreen -n "Port for connection ($DPORT): "
read PORT
# If a port has not been specified, set at default port for the selected method
if [[ $PORT = "" ]]; then
PORT=$DPORT
fi
# Write port in the configuration file
echo "PORT=\"$PORT\"" >> $FUDCONF
# ---
# LOCAL DIRECTORY TO SYNCHRONIZE
# ---
printScreen "Enter the local directory to synchronize. If this directory doesn't exist, will be created."
printScreen -n "Local directory to synchronize: [/home/$USER/Backup] "
read LPATH
# If a local directory has not been entered, set it at /home/$USER/Backup
if [[ $LPATH = "" ]]; then
LPATH="/home/$USER/Backup"
fi
# Create local directory, if doesn't exists
if [ ! -e $LPATH ]; then
mkdir -p $LPATH
printScreen "Create directory $LPATH"
fi
# Set local directory permissions
chmod -R 755 $FUDDIR
# Write local directory in the configuration file
echo "LPATH=\"$LPATH\"" >> $FUDCONF
# ---
# REMOTE DIRECTORY FOR SYNCHRONIZATION
# ---
printScreen "Enter the remote directory for synchronization. If this directory doesn't exist, will be created."
printScreen -n "Remote directory for synchronization: "
read RPATH
# Write remote directory in the configuration file
echo "RPATH=\"$RPATH\"" >> $FUDCONF
# ---
# ADDITIONAL OPTIONS FOR UNISON
# ---
printScreen "If you wish, you can specify additional options for Unison, in addition to those already used by Fud. See \`man unison\` for more information. If you don't know what you're doing, please, skip this step."
printScreen -n "Additional options for Unison: "
read EXTRAOPTIONS
# Write additional options in the configuration file
echo "EXTRAOPTIONS=\"$EXTRAOPTIONS\"" >> $FUDCONF
# ---
# ADD TO FUSE GROUP
# ---
# Check if user is in the fuse group
if ! groups | grep "fuse" &> /dev/null; then
printScreen -n "Do you wanna add your user to fuse group? You will need to enter the root password (Y/n): "
read SURE
if [[ $SURE = "Y" || $SURE = "y" || $SURE = "" ]]; then
# Add user to fuse group
su -c 'adduser $USER fuse'
fi
fi
# ---
# ENABLE AUTOSTART
# ---
printScreen -n "Would you like Fud is launched automatically at startup? (Y/n): "
read SURE
if [[ $SURE = "Y" || $SURE = "y" || $SURE = "" ]]; then
# Create ~/.config/autostart, if doesn't exists
mkdir -p ~/.config/autostart/
# Create ~/.config/autostart/fud.desktop file
echo -e "\n[Desktop Entry]\nName=Fud\nGenericName=File Synchronizer\nComment[en_GB]=Sync your files\nComment[it]=Sincronizza i tuoi file\nExec=fud\nTerminal=false\nType=Application\nIcon=fud\nHidden=false\nCategories=Network;FileTransfer;\nStartupNotify=false" > ~/.config/autostart/fud.desktop
# If user doesn't wanna launch fud at startup
else
# Remove ~/.config/autostart/fud.desktop, if exists
rm -f ~/.config/autostart/fud.desktop
fi
printScreen
# End
printScreen $GREEN"Success"$RESET": Fud has been properly configured and it's now ready for use. Good sync!"
printScreen "Note: if you just added your user to fuse group, you need to logout and login before using Fud."
exit 0;
# If user has decided to don't configure Fud, leave
else
exit 1;
fi