-
Notifications
You must be signed in to change notification settings - Fork 1
/
upm.sh
251 lines (235 loc) · 8.26 KB
/
upm.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/sh
# Universal Package Manager (upm)
# Usage: upm <action> <package(s)>
# Actions: install, remove, update, upgrade, search
# Function to display help message
show_help() {
echo "Universal Package Manager (upm)"
echo "Usage: upm <action> [package(s)]"
echo "Actions:"
echo " install <package(s)> - Install one or more packages"
echo " remove <package(s)> - Remove one or more packages"
echo " update <package(s)> - Update specific package(s)"
echo " upgrade - Upgrade all packages"
echo " search <package> - Search for a package"
echo "Options:"
echo " -h, - Show this help message"
exit 0
}
check_version() {
CURRENT_VERSION_FILE="/etc/upm-version"
REMOTE_VERSION_URL="https://raw.githubusercontent.com/sctech-tr/upm/main/upm-version"
# Fetch the remote version from the URL
if ! REMOTE_VERSION=$(curl -s "$REMOTE_VERSION_URL"); then
echo "Error: Failed to fetch remote version." >&2
exit 1
fi
# Read the current version from the version file
if [ -f "$CURRENT_VERSION_FILE" ]; then
CURRENT_VERSION=$(cat "$CURRENT_VERSION_FILE")
else
echo "Error: Current version file not found." >&2
exit 1
fi
# Compare the remote version with the current version
if [ "$REMOTE_VERSION" != "$CURRENT_VERSION" ]; then
echo "upm $REMOTE_VERSION is available! You are currently on version $CURRENT_VERSION."
exit 0
fi
}
# Function to detect the primary package manager
detect_package_manager() {
if command -v apt &> /dev/null; then
echo "apt"
elif command -v dnf &> /dev/null; then
echo "dnf"
elif command -v yum &> /dev/null; then
echo "yum"
elif command -v pacman &> /dev/null; then
echo "pacman"
elif command -v zypper &> /dev/null; then
echo "zypper"
elif command -v apk &> /dev/null; then
echo "apk"
elif command -v brew &> /dev/null; then
echo "brew"
elif command -v port &> /dev/null; then
echo "port"
elif command -v pkg &> /dev/null; then
echo "pkg"
elif command -v emerge &> /dev/null; then
echo "emerge"
elif command -v xbps-install &> /dev/null; then
echo "xbps"
elif command -v nix-env &> /dev/null; then
echo "nix"
elif command -v snap &> /dev/null; then
echo "snap"
elif command -v flatpak &> /dev/null; then
echo "flatpak"
else
echo "unknown"
fi
}
# Function to run the appropriate command based on the package manager and action
run_command() {
local pm="$1"
local action="$2"
shift 2
local packages="$@"
case "$pm" in
apt)
case "$action" in
install) sudo apt install $packages ;;
remove) sudo apt remove $packages ;;
update) sudo apt update && sudo apt upgrade $packages ;;
upgrade) sudo apt update && sudo apt upgrade ;;
search) apt search $packages ;;
*) echo "Error: Invalid action for apt."; exit 1 ;;
esac
;;
dnf|yum)
case "$action" in
install) sudo $pm install $packages ;;
remove) sudo $pm remove $packages ;;
update) sudo $pm update $packages ;;
upgrade) sudo $pm upgrade ;;
search) $pm search $packages ;;
*) echo "Error: Invalid action for $pm."; exit 1 ;;
esac
;;
pacman)
case "$action" in
install) sudo pacman -S $packages ;;
remove) sudo pacman -R $packages ;;
update) sudo pacman -Syu $packages ;;
upgrade) sudo pacman -Syu ;;
search) pacman -Ss $packages ;;
*) echo "Error: Invalid action for pacman."; exit 1 ;;
esac
;;
zypper)
case "$action" in
install) sudo zypper install $packages ;;
remove) sudo zypper remove $packages ;;
update) sudo zypper update $packages ;;
upgrade) sudo zypper upgrade ;;
search) zypper search $packages ;;
*) echo "Error: Invalid action for zypper."; exit 1 ;;
esac
;;
apk)
case "$action" in
install) sudo apk add $packages ;;
remove) sudo apk del $packages ;;
update) sudo apk update && sudo apk upgrade $packages ;;
upgrade) sudo apk update && sudo apk upgrade ;;
*) echo "Error: Invalid action for apk."; exit 1 ;;
esac
;;
brew)
case "$action" in
install) brew install $packages ;;
remove) brew uninstall $packages ;;
update) brew upgrade $packages ;;
upgrade) brew upgrade ;;
*) echo "Error: Invalid action for brew."; exit 1 ;;
esac
;;
port)
case "$action" in
install) sudo port install $packages ;;
remove) sudo port uninstall $packages ;;
update) sudo port selfupdate && sudo port upgrade $packages ;;
upgrade) sudo port selfupdate && sudo port upgrade outdated ;;
*) echo "Error: Invalid action for port."; exit 1 ;;
esac
;;
pkg)
case "$action" in
install) sudo pkg install $packages ;;
remove) sudo pkg delete $packages ;;
update) sudo pkg update && sudo pkg upgrade $packages ;;
upgrade) sudo pkg update && sudo pkg upgrade ;;
*) echo "Error: Invalid action for pkg."; exit 1 ;;
esac
;;
emerge)
case "$action" in
install) sudo emerge $packages ;;
remove) sudo emerge --unmerge $packages ;;
update) sudo emerge --update $packages ;;
upgrade) sudo emerge --update --deep --with-bdeps=y @world ;;
*) echo "Error: Invalid action for emerge."; exit 1 ;;
esac
;;
xbps)
case "$action" in
install) sudo xbps-install $packages ;;
remove) sudo xbps-remove $packages ;;
update) sudo xbps-install -u $packages ;;
upgrade) sudo xbps-install -Su ;;
*) echo "Error: Invalid action for xbps."; exit 1 ;;
esac
;;
nix)
case "$action" in
install) nix-env -i $packages ;;
remove) nix-env -e $packages ;;
update) nix-env -u $packages ;;
upgrade) nix-env -u ;;
*) echo "Error: Invalid action for nix."; exit 1 ;;
esac
;;
snap)
case "$action" in
install) sudo snap install $packages ;;
remove) sudo snap remove $packages ;;
update) sudo snap refresh $packages ;;
upgrade) sudo snap refresh ;;
*) echo "Error: Invalid action for snap."; exit 1 ;;
esac
;;
flatpak)
case "$action" in
install) flatpak install $packages ;;
remove) flatpak uninstall $packages ;;
update) flatpak update $packages ;;
upgrade) flatpak update ;;
*) echo "Error: Invalid action for flatpak."; exit 1 ;;
esac
;;
*)
echo "Error: Unable to detect a supported package manager."
exit 1
;;
esac
}
# Main script
check_version
# Parse command line options
while getopts ":h" opt; do
case ${opt} in
h )
show_help
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ $# -eq 0 ]; then
show_help
fi
action="$1"
shift
# Multi-package support
packages="$@"
if [[ "$action" != "upgrade" && -z "$packages" && "$action" != "search" ]]; then
echo "Error: Package name is required for $action action."
exit 1
fi
pm=$(detect_package_manager)
run_command "$pm" "$action" "$packages"