-
Notifications
You must be signed in to change notification settings - Fork 9
/
install.sh
executable file
·121 lines (108 loc) · 3.2 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
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
#!/bin/bash
# Function to check if the script is run with sudo
check_sudo() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "Script must be run under sudo from the user you want to install for. Try 'sudo $0'"
exit 1
fi
}
# Function to update boot configuration
update_boot_config() {
local FILE="/boot/config.txt"
local LINE_TO_ENSURE="usb_max_current_enable=1"
local LINE_TO_REPLACE="usb_max_current_enable=0"
if grep -Fxq "$LINE_TO_ENSURE" "$FILE"; then
echo "/boot/config.txt is up-to-date, no action taken."
elif grep -Fxq "$LINE_TO_REPLACE" "$FILE"; then
sed -i "s/^$LINE_TO_REPLACE/$LINE_TO_ENSURE/" "$FILE"
echo "/boot/config.txt updated."
else
echo "$LINE_TO_ENSURE" | sudo tee -a "$FILE"
echo "/boot/config.txt updated."
fi
}
# Function to ensure all needed OS packages are installed
install_packages() {
apt-get install -y git cmake g++ doxygen libc6 libc6-dev libgpiod-dev gpiod || {
echo "Error during installation of APT packages"
exit 1
}
}
# Function to ensure we are within the ControlBlockService2 directory
ensure_directory() {
local currentDirectory=${PWD##*/}
if [[ $currentDirectory != "ControlBlockService2" ]]; then
if [[ -d ControlBlockService2 ]]; then
rm -rf ControlBlockService2
fi
git clone --recursive https://github.com/petrockblog/ControlBlockService2
cd ControlBlockService2 || exit
fi
}
# Function to ensure that the submodule data is available
update_submodules() {
git submodule update --init --recursive
}
# Function to create a folder for build artifacts and change into that folder
prepare_build_directory() {
if [[ -d build ]]; then
rm -rf build
fi
mkdir build || {
echo "Error while creating build folder"
exit 1
}
pushd build || {
echo "Error while changing into the folder build"
exit 1
}
}
# Function to create Makefiles and build the driver
build_driver() {
cmake .. || {
echo "Error while generating Makefiles"
exit 1
}
make || {
echo "Error during building binary"
exit 1
}
}
# Function to install the binary and the driver as a service
install_driver() {
make install || {
echo "Error during installation of binary"
exit 1
}
make installservice || {
echo "Error during installation of service"
exit 1
}
sleep 3
}
# Function to perform sanity checks
perform_sanity_checks() {
if [[ ! -f /usr/bin/controlblock ]]; then
echo "[ERROR] The ControlBlock driver binary is not installed"
else
echo "[SUCCESS] The ControlBlock driver binary is installed"
fi
local isServiceRunning
isServiceRunning=$(pgrep -x controlblock)
if [[ -n $isServiceRunning ]]; then
echo "[SUCCESS] The ControlBlock service is running"
else
echo "[ERROR] The ControlBlock service is not running"
fi
}
# Main script execution
check_sudo
install_packages
ensure_directory
update_submodules
prepare_build_directory
build_driver
install_driver
perform_sanity_checks
update_boot_config
echo "You can find the configuration file at /etc/controlblockconfig.cfg"