-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·162 lines (129 loc) · 5.11 KB
/
setup.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
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
#!/bin/sh
# Make sure user is on a VIT Wi-Fi network
echo "Please make sure you are connected to a VIT Wi-Fi network, and logged in."
echo "Press enter to continue once you are connected."
read -r _
echo "Checking system requirements..."
# Check that the user has at least Python 3.9
PYTHON3_VERSION=`python3 -c 'import sys; print(sys.version_info[1])'`
if [ $PYTHON3_VERSION -lt 9 ]; then
echo "You have `python3 -V` but Python 3.9 or higher is required"
exit 1
fi
# Check that the user has /etc/NetworkManager/dispatcher.d
if [ ! -d "/etc/NetworkManager/dispatcher.d" ]; then
echo "NetworkManager is not installed"
exit 1
fi
# Check if the user wants to install for all users
echo "Do you want to install WiCon for all users? (y/n)"
read -r ALL_USERS
echo ""
echo "Downloading WiCon from GitHub..."
if [ "$ALL_USERS" = "y" ]; then
echo "You may be prompted for your sudo password."
fi
# Clone the repository. If it already exists, pull the latest changes
if [ -d "wicon-py" ]; then
if [ "$ALL_USERS" = "y" ]; then
echo "Pulling latest changes..."
sudo git -C `pwd`/wicon-py pull
else
echo "Pulling latest changes..."
git -C wicon-py pull
fi
else
echo "Cloning repository..."
if [ "$ALL_USERS" = "y" ]; then
sudo git clone https://github.com/cocapp/wicon-py.git `pwd`/wicon-py
else
git clone https://github.com/cocapp/wicon-py.git
fi
fi
echo ""
echo "Setting up Python virtual environment..."
# Create and setup virtual environment
if [ "$ALL_USERS" = "y" ]; then
sudo python3 -m venv `pwd`/wicon-py/.venv --upgrade-deps || sudo python3 -m venv `pwd`/wicon-py/.venv
sudo `pwd`/wicon-py/.venv/bin/python -m pip install -r `pwd`/wicon-py/requirements.txt || {
echo "Failed to install dependencies."
echo "Please check your pip installation and try again."
exit 1
}
else
python3 -m venv `pwd`/wicon-py/.venv --upgrade-deps || python3 -m venv `pwd`/wicon-py/.venv
`pwd`/wicon-py/.venv/bin/python -m pip install -r `pwd`/wicon-py/requirements.txt || {
echo "Failed to install dependencies."
echo "Please check your pip installation and try again."
exit 1
}
fi
echo ""
echo "Creating login and logout scripts..."
# Create login binary
touch /tmp/wicon-py-login
echo "#!/bin/sh" >> /tmp/wicon-py-login
echo "if [ \"\$2\" = \"up\" ]; then" >> /tmp/wicon-py-login
echo "su $USER -c \"`pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py login -n\"" >> /tmp/wicon-py-login
echo "fi" >> /tmp/wicon-py-login
# Create logout binary
touch /tmp/wicon-py-logout
echo "#!/bin/sh" >> /tmp/wicon-py-logout
echo "su $USER -c \"`pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py logout -n\"" >> /tmp/wicon-py-logout
echo ""
echo "Setting up login and logout scripts to run on network change..."
if [ "$ALL_USERS" != "y" ]; then
echo "You may be prompted for your sudo password."
fi
# Move login binary to NetworkManager dispatcher
sudo mv /tmp/wicon-py-login /etc/NetworkManager/dispatcher.d/wicon-py-login
sudo chown root:root /etc/NetworkManager/dispatcher.d/wicon-py-login
sudo chmod 555 /etc/NetworkManager/dispatcher.d/wicon-py-login
# Move logout binary to NetworkManager dispatcher pre-down.d
sudo mv /tmp/wicon-py-logout /etc/NetworkManager/dispatcher.d/pre-down.d/wicon-py-logout
sudo chown root:root /etc/NetworkManager/dispatcher.d/pre-down.d/wicon-py-logout
sudo chmod 555 /etc/NetworkManager/dispatcher.d/pre-down.d/wicon-py-logout
echo ""
echo "Setting up WiCon..."
echo "You may be prompted for your VIT Wi-Fi username and password."
# Prompt to setup username and password
# This will be done using a command built into WiCon
# WiCon will return 0 if successful
# Continue until WiCon returns 0
while ! sudo -u $USER `pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py addcreds; do
echo ""
echo "Please try again."
done
echo "Setup complete!"
echo ""
echo "Testing logout..."
if sudo -u $USER `pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py logout > /dev/null; then
echo "Logout successful!"
else
echo "Logout failed!"
fi
if [ "$ALL_USERS" = "y" ]; then
echo "Setting permissions for all users..."
# Make current user the owner of the WiCon directory
sudo chown $USER `pwd`/wicon-py
sudo chown -R $USER `pwd`/wicon-py
# Allow all users to read and execute (but not write) all files
sudo chmod -R 777 `pwd`/wicon-py
echo "Permissions set!"
fi
echo "Testing login..."
if sudo -u $USER `pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py login > /dev/null; then
echo "Login successful!"
else
echo "Login failed!"
fi
echo ""
echo "Your WiCon logs are located at $HOME/.wicon/wicon.log. Note that these are not sent anywhere automatically for privacy reasons, but you can send them along when reporting issues."
echo "Your WiCon settings are located at $HOME/.wicon/wicon-settings.json."
# Add alias for WiCon
alias_string="alias wicon='`pwd`/wicon-py/.venv/bin/python `pwd`/wicon-py/login_cli.py'"
if [ "$ALL_USERS" = "y" ]; then
sudo /bin/sh -c "echo \"$alias_string\" >> /etc/bash.bashrc"
else
echo "$alias_string" >> ~/.bashrc
fi