-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathinstall.sh
209 lines (178 loc) · 5.81 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
NVM_VERSION="0.39.3"
NVM_URL="https://cdn.pisugar.com/PiSugar-wificonfig/script/nvm/v$NVM_VERSION.tar.gz"
NPM_REGISTRY="https://registry.npmmirror.com"
REPO_URL="https://gitee.com/jdaie/sugar-wifi-config.git"
NODE_BINARY_INSTALL_URL="https://cdn.pisugar.com/PiSugar-wificonfig/script/node-binary/install-node-v18.19.1.sh"
INSTALL_DIR="/opt/sugar-wifi-config"
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install nvm and Node.js 18
install_node_nvm() {
echo "Installing Node.js 18 using nvm..."
# Install nvm if it's not already installed
if [ ! -d "$HOME/.nvm" ]; then
echo "Installing nvm..."
TEMP_DIR=$(mktemp -d)
curl -o $TEMP_DIR/nvm-$NVM_VERSION.tar.gz -L $NVM_URL
tar -xzf $TEMP_DIR/nvm-$NVM_VERSION.tar.gz -C $TEMP_DIR
mv $TEMP_DIR/nvm-$NVM_VERSION $HOME/.nvm
rm -rf $TEMP_DIR
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
else
echo "nvm is already installed."
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
fi
# Install and use Node.js 18
echo "Swith to Node.js 18"
nvm install 18
nvm use 18
# Verify installation
if command_exists node && [[ "$(node -v)" =~ ^v18 ]]; then
echo "Node.js 18 installed successfully."
else
echo "Failed to install Node.js 18."
exit 1
fi
}
install_node_binary() {
echo "Installing Node.js 18 for pi zero..."
TEMP_DIR=$(mktemp -d)
curl -o $TEMP_DIR/install-node-v18.19.1.sh -L $NODE_BINARY_INSTALL_URL
chmod +x $TEMP_DIR/install-node-v18.19.1.sh
sudo bash $TEMP_DIR/install-node-v18.19.1.sh
rm -rf $TEMP_DIR
# Verify installation
if command_exists node && [[ "$(node -v)" =~ ^v18 ]]; then
echo "Node.js 18 installed successfully."
else
echo "Failed to install Node.js 18."
exit 1
fi
}
install_node() {
if [[ "$(uname -m)" == "armv6l" ]]; then
install_node_binary
else
install_node_nvm
fi
}
# Check if Node.js is installed and is version 18
if command_exists node; then
NODE_VERSION=$(node -v)
if [[ "$NODE_VERSION" =~ ^v18 ]]; then
echo "Node.js 18 is already installed."
else
echo "Different version of Node.js detected: $NODE_VERSION"
install_node
fi
else
echo "Node.js is not installed."
install_node
fi
# Check if npm is installed
if ! command_exists npm; then
echo "npm is not installed. Installing npm..."
sudo apt-get install -y npm
# Verify installation
if command_exists npm; then
echo "npm installed successfully."
else
echo "Failed to install npm."
exit 1
fi
fi
# check if git is installed
if ! command_exists git; then
echo "git is not installed. Installing git..."
sudo apt-get install -y git
# Verify installation
if command_exists git; then
echo "git installed successfully."
else
echo "Failed to install git."
exit 1
fi
fi
# check if yarn is installed
if ! command_exists yarn; then
echo "yarn is not installed. Installing yarn..."
npm config set registry $NPM_REGISTRY
npm install -g yarn
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
# Verify installation
if command_exists yarn; then
echo "yarn installed successfully."
else
echo "Failed to install yarn."
exit 1
fi
fi
#sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
#sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"
#sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npx" "/usr/local/bin/npx"
# install repo
# Check if /opt/sugar-wifi-config exists and delete it if it does
if [ -d "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR exists. Deleting..."
sudo rm -rf "$INSTALL_DIR"
if [ ! -d "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR successfully deleted."
else
echo "Failed to delete $INSTALL_DIR."
exit 1
fi
else
echo "$INSTALL_DIR does not exist."
fi
echo "Cloning $REPO_URL to $INSTALL_DIR..."
mkdir -p $INSTALL_DIR
git clone $REPO_URL $INSTALL_DIR --depth 1
cd $INSTALL_DIR
git pull
echo "Installing dependencies..."
yarn --registry=$NPM_REGISTRY
chmod +x $INSTALL_DIR/run.sh
# Define service name and service file path
SERVICE_NAME="sugar-wifi-config.service"
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME"
# Remove old rc.local configuration
echo -e "Removing old rc.local configuration..."
sudo sed -i '/sugar-wifi-conf/d' /etc/rc.local
# If systemd service exists, remove old systemd service file
if [ -f "$SERVICE_FILE" ]; then
echo -e "Removing old systemd service file..."
sudo systemctl stop $SERVICE_NAME
sudo systemctl disable $SERVICE_NAME
sudo rm -f $SERVICE_FILE
fi
# Create systemd service file
echo -e "Creating systemd service file..."
sudo bash -c "cat > $SERVICE_FILE <<EOF
[Unit]
Description=Sugar WiFi Configuration Service
After=network.target
[Service]
ExecStart=/usr/bin/bash /opt/sugar-wifi-config/run.sh pisugar /opt/sugar-wifi-config/custom_config.json
WorkingDirectory=/opt/sugar-wifi-config
Restart=always
User=root
[Install]
WantedBy=multi-user.target
EOF"
# Reload systemd configuration
echo -e "Reloading systemd configuration..."
sudo systemctl daemon-reload
# Enable and start service
echo -e "Enabling and starting $SERVICE_NAME..."
sudo systemctl enable $SERVICE_NAME
sudo systemctl start $SERVICE_NAME
echo -e "You can check the service status by running: sudo systemctl status $SERVICE_NAME"
# Check if service is running
echo -e "\nWell done Pi Star people!"