-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
97 lines (80 loc) · 4.06 KB
/
setup
File metadata and controls
97 lines (80 loc) · 4.06 KB
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
#!/usr/bin/sh
if [ $USER != "root" ]; then
echo "Please run as root"
exit 1
fi
echo "==================\e[1;34m Setup Started \e[0m=================="
ruledir="/etc/udev/rules.d/99-usb.rules"
servicedir="/etc/systemd/system/usb@.service"
scriptdir="/usr/local/bin/usb.sh"
localdir="/home/$SUDO_USER/usb-data"
echo "Rule file: $ruledir"
echo "Service file: $servicedir"
echo "Script file: $scriptdir"
echo "Local Storage: $localdir"
echo "Installing source files..."
install -o root -g root -m 644 rule $ruledir # Create Rule file
install -o root -g root -m 644 service $servicedir # Create Service file
install -o root -g root -m 744 script $scriptdir # Create script file
usbdir=$(dirname "$(pwd)")
userial=$(udevadm info "$(findmnt -no SOURCE $usbdir)" | grep -oP "ID_SERIAL=\K.*")
echo "Setting variables..."
sed -i "s|SERIALID|$userial|" $ruledir # Assign Serial ID in Rule file
sed -i "s|USER|$SUDO_USER|" $scriptdir # Assign Local dir in Script file
localconf="$localdir/conf"
usbconf="$usbdir/Data/conf"
cserial=$(dmidecode -s chassis-serial-number)
echo "Creating data files..."
mkdir -pm 777 "$localdir/Send" # Create local send and
mkdir -pm 777 "$localdir/Recv" # receive directories.
touch "$localdir/History" # Create history file.
mkdir -p "$usbdir/Data/1-to-2" # Make this PC to other PC dir
mkdir -p "$usbdir/Data/2-to-1" # and other PC to this PC dir.
touch "$usbdir/Data/1-to-2/todelete" # Create todelete files in both
touch "$usbdir/Data/2-to-1/todelete" # 1-to-2 and 2-to-1 directories.
if [ ! -f "$localconf" ]; then # If config file doesn't exist,
echo "Local config file created..." # create one and set doTransfer
echo "serial=$cserial" > "$localconf" # to 1 and thiPC to 0.
echo "doTransfer=1" >> "$localconf" # to 1 and thiPC to 0.
echo "thisPC=0" >> "$localconf"
else
echo "Local config file found..."
if [ "$(grep -oP "doTransfer=\K.*" "$localdir/conf")" -eq 0 ]; then
echo "\e[0;33m└─doTransfer set to 0. Make it 1 to enable tranfer.\e[0m"
fi
fi
if [ ! -f "$usbconf" ]; then # If config file doesn't exist,
echo "USB config file created..." # create one and put default
echo "timeout=120" > $usbconf # timeout value of 2 minutes.
else
echo "USB config file found..."
fi
this=$(grep -oE "PC[1-2]=$cserial" $usbconf)
if [ $this ]; then this=$(echo $this | cut -c3)
elif ! grep -q "PC1=" $usbconf; then this=1 # Check which numbers are
elif ! grep -q "PC2=" $usbconf; then this=2 # occupied and which are
else # not. And assign a
echo "All system numbers occupied..." # number to this PC.
tail +2 "$usbconf"
while [ "$this" != 1 ] && [ "$this" != 2 ]; do # If both the numbers
echo -n "Choose which system to replace (1/2): "; read this # are occupied, ask
done # which PC to replace.
printf "$(grep -v "PC$this=" $usbconf)\n" > $usbconf
fi
echo "System numbered as $this..."
! grep -q "PC$this=" $usbconf && echo "PC$this=$cserial" >> $usbconf # Add the PC to conf file.
sed -i "/thisPC=/{s/[0-2]/$this/g}" "$localconf"
echo "Reloading Systemd and Udev..."
udevadm control -R # Reload Udev
systemctl daemon-reload # Reload Systemd
if ! command -v rsync > /dev/null; then # Install rsync command
echo "Installing rsync..." # if not existing.
apt-get install rsync > /dev/null 2>&1 || \
dpkg -i rsync_3.1.3-6_amd64.deb > /dev/null # If no internet
fi
if ! command -v nocache > /dev/null; then # Install nocache command
echo "Installing nocache..." # if not existing.
apt-get install nocache > /dev/null 2>&1 || \
dpkg -i nocache_1.1-1_amd64.deb > /dev/null # If no internet
fi
echo "=================\e[1;32m Setup Successful \e[0m================"