-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·156 lines (122 loc) · 4.16 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
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
#!/bin/bash
opirepo=https://github.com/smugg99/OPI.GPIO.git/
# Automatically retrieve the current username and primary group
username=$USER
group=$(id -gn $USER)
audio_device="hw:0,0"
config_file="service_config.txt"
service_filename="zsem_bells.service"
service_file_path="/etc/systemd/system"
service_description="ZSEM-Bells drivers system service"
# Function to install a package if it's not already installed
install_if_not_installed() {
PACKAGE=$1
if ! dpkg -l | grep -q $PACKAGE; then
echo "$PACKAGE is not installed. Installing..."
sudo apt install -y $PACKAGE
else
echo "$PACKAGE is already installed"
fi
}
# Update package lists
echo "Updating package lists..."
sudo apt update
# Install required packages
install_if_not_installed python3
install_if_not_installed python3-pip
install_if_not_installed python3-venv
install_if_not_installed python3-dev
install_if_not_installed python3-dialog
install_if_not_installed python3-systemd
install_if_not_installed git
# Check if systemctl is installed, and if not, install it,
# it's usually installed because it's used internally by this system
if ! command -v systemctl &>/dev/null; then
install_if_not_installed systemd
fi
# Create and activate virtual environment
echo "Creating and activating virtual environment"
python3 -m venv venv
source ./venv/bin/activate
# Check and install OPI.GPIO
if python3 -c "import OPI.GPIO" &>/dev/null; then
echo "OPI.GPIO is already installed"
else
echo "OPI.GPIO is not installed. Cloning..."
git clone $opirepo
echo "Installing OPI.GPIO module"
pushd OPI.GPIO
python3 setup.py install
pip3 install ./
popd
fi
# Install dependencies from requirements.txt
echo "Installing dependencies"
./venv/bin/pip install -r requirements.txt
# Deactivate virtual environment
echo "Deactivating virtual environment"
deactivate
while true; do
# If config file exists, load values from it
if [ -f "$config_file" ]; then
source "$config_file"
fi
# Display currently set values
echo "Current username: $username"
echo "Current group: $group"
read -p "Enter working directory: " new_working_directory
working_directory=${new_working_directory:-$working_directory}
read -p "Enter virtual environment path: " new_venv_path
venv_path=${new_venv_path:-$venv_path}
read -p "Enter main script path: " new_script_path
script_path=${new_script_path:-$script_path}
read -p "Enter the desired audio device (example: hw:0,0): " new_audio_device
audio_device=${new_audio_device:-$audio_device}
# Save the new values to the config file
cat << EOF > "$config_file"
working_directory="$working_directory"
venv_path="$venv_path"
audio_device="$audio_device"
script_path="$script_path"
service_filename="$service_filename"
service_file_path="$service_file_path"
EOF
# Confirm user actions
read -p "Confirm to create '$service_filename' with provided settings? (yes/no): " confirm
if [ "$confirm" == "yes" ] || [ "$confirm" == "y" ]; then
# Create the service file
cat << EOF > zsem_bells.service
[Unit]
Description=$service_description
After=network.target
[Service]
User=$username
Group=$group
WorkingDirectory=$working_directory
Environment="AUDIODEV=$audio_device"
Environment="PATH=$venv_path/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin"
ExecStart=$venv_path/bin/python3 $script_path
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=zsem_bells
[Install]
WantedBy=multi-user.target
EOF
echo "Service file '$service_filename' created"
break # Exit the loop if user confirms
else
echo "Operation cancelled. Going through configuration again"
fi
done
# Move the service file to the common system service directory
echo "Moving service file to $service_file_path"
if [ -f "$service_filename" ]; then
sudo mv zsem_bells.service $service_file_path
echo "Service file '$service_filename' moved to $service_file_path"
else
echo "Service file '$service_filename' somehow not found. Skipping move."
fi
echo "All packages checked and installed if needed"
echo "Installation completed!"