-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
92 lines (76 loc) · 2.89 KB
/
install.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
#!/bin/bash
# Get the absolute path to the current directory
# It is the directory from which you run this script
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Path to the NetworkManager Dispatcher script
dispatcher_script_path="/etc/NetworkManager/dispatcher.d/faffetch_tracking_network_script.sh"
# Check if the script is running with sudo
if [[ -n "$SUDO_USER" ]]; then
echo "Running as sudo by user: $SUDO_USER"
else
echo "Not running as sudo"
exit 1
fi
# Function to install the script
install_script() {
# Compile the C++ code using make
make clean &&
make
if [ $? -ne 0 ]; then
echo "Error: Compilation failed. Please check the dependencies then try again."
exit 1
fi
# Content of the NetworkManager Dispatcher script
dispatcher_script_content="#!/bin/bash\n\n"
dispatcher_script_content+="if [[ \$2 == \"connectivity-change\" ]];then\n"
dispatcher_script_content+=" $current_dir/bin/get_net_info &> $current_dir/cache.csv \n"
dispatcher_script_content+="fi"
# Create the NetworkManager Dispatcher script
echo -e "${dispatcher_script_content}" >"${dispatcher_script_path}"
echo "NetworkManager Dispatcher script created at: ${dispatcher_script_path}"
# Make the script executable
chmod +x "${dispatcher_script_path}"
echo "Testing dispatcher script"
# Execute the dispatcher script with test arguments
$dispatcher_script_path "x" "connectivity-change"
echo "Modifying /home/$SUDO_USER/.bashrc"
# Remove existing alias and lines containing 'faffetch' in .bashrc
sed -i '/alias faffetch=.*/d' "/home/$SUDO_USER/.bashrc"
sed -i '/faffetch/d' "/home/$SUDO_USER/.bashrc"
# Add new alias for faffetch with cache directory parameter
echo "alias faffetch=\"$current_dir/bin/faffetch $current_dir\"" >>"/home/$SUDO_USER/.bashrc"
echo "faffetch" >>"/home/$SUDO_USER/.bashrc"
# Source the updated .bashrc using sudo
echo "Source /home/$SUDO_USER/.bashrc as user $SUDO_USER"
sudo su $SUDO_USER
source ".bashrc"
}
# Function to uninstall the script
uninstall_script() {
# Remove the NetworkManager Dispatcher script
rm -f "${dispatcher_script_path}"
# Remove lines from .bashrc containing the faffetch alias
sed -i '/alias faffetch=.*/d' "/home/$SUDO_USER/.bashrc"
sed -i '/faffetch/d' "/home/$SUDO_USER/.bashrc"
echo "Removed hook in /home/$SUDO_USER/.bashrc"
echo "NetworkManager Dispatcher script removed from: ${dispatcher_script_path}"
echo "Uninstaller script executed successfully!"
}
# Check if any arguments are provided, if not, default to install
if [[ $# -eq 0 ]]; then
install_script
else
# Check for install or uninstall option
case "$1" in
install)
install_script
;;
uninstall)
uninstall_script
;;
*)
echo "Usage: $0 {install|uninstall}"
exit 1
;;
esac
fi