summarylogtreecommitdiffstats
path: root/all
blob: b2b14ab41f6f2ede45dc0b6cbf88f27a7fffcd3a (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash

# Universal Package Manager Wrapper
# Supports: pacman, yay (AUR), apt, dnf, zypper, flatpak, snap

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Function to check if a command exists
command_exists() {
    command -v "$1" &> /dev/null
}

# Function to search for package in different managers
search_package() {
    local pkg_name="$1"
    local -a found_in=()
    
    echo -e "${CYAN}Searching for '${pkg_name}'...${NC}\n" >&2
    
    # Search in pacman (Arch Linux)
    if command_exists pacman; then
        echo -e "${BLUE}Searching pacman...${NC}" >&2
        if pacman -Ss "^${pkg_name}$" &> /dev/null; then
            found_in+=("pacman")
        fi
    fi
    
    # Search in AUR via yay (Arch Linux)
    if command_exists yay; then
        echo -e "${BLUE}Searching AUR (yay)...${NC}" >&2
        if yay -Ss "^${pkg_name}$" &> /dev/null; then
            found_in+=("yay")
        fi
    fi
    
    # Search in APT (Debian/Ubuntu)
    if command_exists apt; then
        echo -e "${BLUE}Searching APT...${NC}" >&2
        if apt-cache search --names-only "^${pkg_name}$" 2>/dev/null | grep -q "^${pkg_name}"; then
            found_in+=("apt")
        fi
    fi
    
    # Search in DNF (Fedora/RHEL)
    if command_exists dnf; then
        echo -e "${BLUE}Searching DNF...${NC}" >&2
        if dnf list --available "${pkg_name}" &> /dev/null; then
            found_in+=("dnf")
        fi
    fi
    
    # Search in Zypper (openSUSE)
    if command_exists zypper; then
        echo -e "${BLUE}Searching Zypper...${NC}" >&2
        if zypper search -x "${pkg_name}" &> /dev/null; then
            found_in+=("zypper")
        fi
    fi
    
    # Search in Flatpak
    if command_exists flatpak; then
        echo -e "${BLUE}Searching Flatpak...${NC}" >&2
        if flatpak search "${pkg_name}" 2>/dev/null | grep -qi "${pkg_name}"; then
            found_in+=("flatpak")
        fi
    fi
    
    # Search in Snap
    if command_exists snap; then
        echo -e "${BLUE}Searching Snap...${NC}" >&2
        if snap find "${pkg_name}" 2>/dev/null | grep -q "^${pkg_name}"; then
            found_in+=("snap")
        fi
    fi
    
    echo "${found_in[@]}"
}

# Function to install package using specified manager
install_package() {
    local pkg_name="$1"
    local manager="$2"
    
    echo -e "\n${GREEN}Installing '${pkg_name}' from ${manager}...${NC}\n"
    
    case "$manager" in
        pacman)
            sudo pacman -S "${pkg_name}"
            ;;
        yay)
            yay -S "${pkg_name}"
            ;;
        apt)
            sudo apt update && sudo apt install "${pkg_name}"
            ;;
        dnf)
            sudo dnf install "${pkg_name}"
            ;;
        zypper)
            sudo zypper install "${pkg_name}"
            ;;
        flatpak)
            flatpak install "${pkg_name}"
            ;;
        snap)
            sudo snap install "${pkg_name}"
            ;;
        *)
            echo -e "${RED}Unknown package manager: ${manager}${NC}"
            exit 1
            ;;
    esac
}

# Function to remove/uninstall package
remove_package() {
    local pkg_name="$1"
    local manager="$2"
    
    echo -e "\n${RED}Removing '${pkg_name}' using ${manager}...${NC}\n"
    
    case "$manager" in
        pacman)
            sudo pacman -R "${pkg_name}"
            ;;
        yay)
            yay -R "${pkg_name}"
            ;;
        apt)
            sudo apt remove "${pkg_name}"
            ;;
        dnf)
            sudo dnf remove "${pkg_name}"
            ;;
        zypper)
            sudo zypper remove "${pkg_name}"
            ;;
        flatpak)
            flatpak uninstall "${pkg_name}"
            ;;
        snap)
            sudo snap remove "${pkg_name}"
            ;;
        *)
            echo -e "${RED}Unknown package manager: ${manager}${NC}"
            exit 1
            ;;
    esac
}

# Function to update packages
update_packages() {
    echo -e "${CYAN}Updating all package managers...${NC}\n"
    
    if command_exists pacman; then
        echo -e "${BLUE}Updating pacman...${NC}"
        sudo pacman -Syu
    fi
    
    if command_exists yay; then
        echo -e "${BLUE}Updating AUR (yay)...${NC}"
        yay -Syu
    fi
    
    if command_exists apt; then
        echo -e "${BLUE}Updating APT...${NC}"
        sudo apt update && sudo apt upgrade
    fi
    
    if command_exists dnf; then
        echo -e "${BLUE}Updating DNF...${NC}"
        sudo dnf upgrade
    fi
    
    if command_exists zypper; then
        echo -e "${BLUE}Updating Zypper...${NC}"
        sudo zypper update
    fi
    
    if command_exists flatpak; then
        echo -e "${BLUE}Updating Flatpak...${NC}"
        flatpak update
    fi
    
    if command_exists snap; then
        echo -e "${BLUE}Updating Snap...${NC}"
        sudo snap refresh
    fi
}

# Function to show usage
show_usage() {
    cat << EOF
${CYAN}Universal Package Manager Wrapper${NC}

Usage: all [COMMAND] [PACKAGE]

Commands:
  install, i      Install a package
  remove, r       Remove a package
  update, u       Update all packages
  search, s       Search for a package
  help, h         Show this help message

Examples:
  all install firefox
  all remove vlc
  all update
  all search git

Supported package managers:
  - pacman (Arch Linux)
  - yay (AUR - Arch User Repository)
  - apt (Debian/Ubuntu)
  - dnf (Fedora/RHEL)
  - zypper (openSUSE)
  - flatpak
  - snap

EOF
}

# Main script logic
main() {
    if [ $# -eq 0 ]; then
        show_usage
        exit 0
    fi
    
    local command="$1"
    local pkg_name="$2"
    
    case "$command" in
        install|i)
            if [ -z "$pkg_name" ]; then
                echo -e "${RED}Error: Package name required${NC}"
                echo "Usage: all install [PACKAGE]"
                exit 1
            fi
            
            # Search for package
            local found=($(search_package "$pkg_name"))
            
            if [ ${#found[@]} -eq 0 ]; then
                echo -e "\n${RED}Package '${pkg_name}' not found in any package manager${NC}"
                exit 1
            elif [ ${#found[@]} -eq 1 ]; then
                echo -e "\n${GREEN}Package '${pkg_name}' found in: ${found[0]}${NC}"
                install_package "$pkg_name" "${found[0]}"
            else
                echo -e "\n${YELLOW}Package '${pkg_name}' found in multiple locations:${NC}\n"
                
                # Display options
                for i in "${!found[@]}"; do
                    echo -e "  ${GREEN}$((i+1)))${NC} ${found[$i]}"
                done
                
                # Prompt user for selection
                echo -e "\n${CYAN}Which location do you want to install '${pkg_name}' from?${NC}"
                read -p "Enter number (1-${#found[@]}): " choice
                
                # Validate input
                if [[ ! "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#found[@]} ]; then
                    echo -e "${RED}Invalid choice${NC}"
                    exit 1
                fi
                
                # Install from selected manager
                local selected_manager="${found[$((choice-1))]}"
                install_package "$pkg_name" "$selected_manager"
            fi
            ;;
            
        remove|r)
            if [ -z "$pkg_name" ]; then
                echo -e "${RED}Error: Package name required${NC}"
                echo "Usage: all remove [PACKAGE]"
                exit 1
            fi
            
            # For removal, we need to detect which manager has it installed
            echo -e "${CYAN}Detecting installed package managers for '${pkg_name}'...${NC}\n"
            
            local installed_in=()
            
            if command_exists pacman && pacman -Q "$pkg_name" &> /dev/null; then
                installed_in+=("pacman")
            fi
            
            if command_exists apt && dpkg -l "$pkg_name" &> /dev/null; then
                installed_in+=("apt")
            fi
            
            if command_exists dnf && dnf list installed "$pkg_name" &> /dev/null; then
                installed_in+=("dnf")
            fi
            
            if command_exists zypper && zypper search -i "$pkg_name" &> /dev/null; then
                installed_in+=("zypper")
            fi
            
            if command_exists flatpak && flatpak list | grep -q "$pkg_name"; then
                installed_in+=("flatpak")
            fi
            
            if command_exists snap && snap list "$pkg_name" &> /dev/null; then
                installed_in+=("snap")
            fi
            
            if [ ${#installed_in[@]} -eq 0 ]; then
                echo -e "${RED}Package '${pkg_name}' is not installed${NC}"
                exit 1
            elif [ ${#installed_in[@]} -eq 1 ]; then
                remove_package "$pkg_name" "${installed_in[0]}"
            else
                echo -e "${YELLOW}Package '${pkg_name}' is installed in multiple locations:${NC}\n"
                for i in "${!installed_in[@]}"; do
                    echo -e "  ${GREEN}$((i+1)))${NC} ${installed_in[$i]}"
                done
                
                echo -e "\n${CYAN}Which installation do you want to remove?${NC}"
                read -p "Enter number (1-${#installed_in[@]}): " choice
                
                if [[ ! "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#installed_in[@]} ]; then
                    echo -e "${RED}Invalid choice${NC}"
                    exit 1
                fi
                
                local selected_manager="${installed_in[$((choice-1))]}"
                remove_package "$pkg_name" "$selected_manager"
            fi
            ;;
            
        update|u)
            update_packages
            ;;
            
        search|s)
            if [ -z "$pkg_name" ]; then
                echo -e "${RED}Error: Package name required${NC}"
                echo "Usage: all search [PACKAGE]"
                exit 1
            fi
            
            local found=($(search_package "$pkg_name"))
            
            if [ ${#found[@]} -eq 0 ]; then
                echo -e "\n${RED}Package '${pkg_name}' not found${NC}"
            else
                echo -e "\n${GREEN}Package '${pkg_name}' found in:${NC}"
                for manager in "${found[@]}"; do
                    echo -e "  - ${manager}"
                done
            fi
            ;;
            
        help|h|--help|-h)
            show_usage
            ;;
            
        *)
            echo -e "${RED}Unknown command: ${command}${NC}"
            echo ""
            show_usage
            exit 1
            ;;
    esac
}

main "$@"