-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinbox-setup.sh
executable file
·168 lines (138 loc) · 4.6 KB
/
winbox-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
163
164
165
166
167
168
#!/bin/bash
# Variables
URL="https://mikrotik.com/download"
DEST_DIR="/opt/winbox"
DESKTOP_FILE="/usr/share/applications/winbox.desktop"
ICON_PATH="$DEST_DIR/assets/img/winbox.png"
VERSION_FILE="$DEST_DIR/version"
ZIP_FILE="$DEST_DIR/WinBox_Linux.zip"
# Function to download and extract WinBox
install_winbox() {
# Download the download page
echo "Fetching MikroTik download page..."
PAGE_CONTENT=$(curl -s "$URL")
# Parse the page for the WinBox Linux download link (regex to match the dynamic part)
DOWNLOAD_LINK=$(echo "$PAGE_CONTENT" | grep -oP 'https://download\.mikrotik\.com/routeros/winbox/[^"]+/WinBox_Linux\.zip')
if [ -z "$DOWNLOAD_LINK" ]; then
echo "Error: Could not find WinBox_Linux.zip download link."
exit 1
fi
echo "Found WinBox Linux download link: $DOWNLOAD_LINK"
# Extract version information from the URL (e.g., "4.0beta9" from "4.0beta9/WinBox_Linux.zip")
CURRENT_VERSION=$(echo "$DOWNLOAD_LINK" | grep -oP 'winbox/\K[^/]+')
echo "Current WinBox version available: $CURRENT_VERSION"
# If the version file exists, read the installed version
if [ -f "$VERSION_FILE" ]; then
INSTALLED_VERSION=$(cat "$VERSION_FILE")
echo "Installed version: $INSTALLED_VERSION"
else
INSTALLED_VERSION="none"
echo "No previous version installed."
fi
# Compare versions
if [ "$INSTALLED_VERSION" != "$CURRENT_VERSION" ]; then
echo "Updating to version $CURRENT_VERSION..."
# Download the file with a progress bar
echo "Downloading $DOWNLOAD_LINK..."
curl -L --progress-bar "$DOWNLOAD_LINK" -o "$ZIP_FILE"
# Check if the file was downloaded
if [ ! -f "$ZIP_FILE" ]; then
echo "Error: Download failed."
exit 1
fi
echo "Download complete. Extracting to $DEST_DIR..."
# Extract the zip file
sudo unzip -o "$ZIP_FILE" -d "$DEST_DIR"
# Cleanup the ZIP file after extraction
echo "Cleaning up..."
rm "$ZIP_FILE"
# Store the current version in the 'version' file
echo "$CURRENT_VERSION" | sudo tee "$VERSION_FILE" > /dev/null
echo "WinBox successfully installed/updated to $CURRENT_VERSION in $DEST_DIR"
else
echo "WinBox is already up-to-date (version $INSTALLED_VERSION). No update required."
fi
}
# Function to create .desktop file
create_desktop_file() {
echo "Creating .desktop file for system-wide usage..."
# Check if icon exists
if [ ! -f "$ICON_PATH" ]; then
echo "Icon file not found at $ICON_PATH, please place the icon correctly."
exit 1
fi
# Create the .desktop file
sudo bash -c "cat > $DESKTOP_FILE" << EOL
[Desktop Entry]
Version=1.0
Type=Application
Name=WinBox
Comment=MikroTik WinBox
Exec=$DEST_DIR/WinBox # Adjust this if the executable has a different name
Icon=$ICON_PATH
Terminal=false
Categories=Network;Utility;
EOL
# Make sure the .desktop file is executable
sudo chmod +x "$DESKTOP_FILE"
echo "Desktop entry created at $DESKTOP_FILE"
}
# Function to uninstall WinBox
uninstall_winbox() {
if [ -d "$DEST_DIR" ]; then
echo "Removing WinBox from $DEST_DIR..."
sudo rm -rf "$DEST_DIR"
else
echo "Error: WinBox is not installed."
exit 1
fi
if [ -f "$DESKTOP_FILE" ]; then
echo "Removing desktop entry..."
sudo rm "$DESKTOP_FILE"
fi
echo "WinBox successfully uninstalled."
}
# Ensure the user is using sudo
if [ "$EUID" -ne 0 ]; then
echo "This script needs to be run as root. Please enter your password."
sudo "$0" "$@"
exit
fi
# Check if the user provided the required parameter
if [ $# -eq 0 ]; then
echo "Usage: $0 [--install|-i] or [--update|-u] or [--remove|-rm]"
exit 1
fi
# Handle parameters
case "$1" in
--install|-i)
if [ -d "$DEST_DIR" ]; then
echo "Error: WinBox is already installed in $DEST_DIR."
exit 1
else
echo "Installing WinBox..."
sudo mkdir -p "$DEST_DIR"
sudo mkdir -p "$DEST_DIR/assets/img"
sudo chown $USER:$USER "$DEST_DIR"
install_winbox
create_desktop_file
fi
;;
--update|-u)
if [ ! -d "$DEST_DIR" ]; then
echo "Error: WinBox is not installed. Please install it first."
exit 1
else
echo "Checking for WinBox updates in $DEST_DIR..."
install_winbox
fi
;;
--remove|-rm)
echo "Uninstalling WinBox..."
uninstall_winbox
;;
*)
echo "Invalid option. Use --install|-i for new installation, --update|-u for updating, or --remove|-rm for uninstalling."
exit 1
;;
esac