| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: 0BSD |
| 3 | set -eu |
| 4 | |
| 5 | usage() { |
| 6 | printf '%s\n' 'Usage: pkg install|remove|info PACKAGE ...' \ |
| 7 | ' pkg search QUERY ... | pkg search --exact PACKAGE' \ |
| 8 | ' pkg list|update|upgrade' |
| 9 | } |
| 10 | |
| 11 | require_root() { |
| 12 | if [ "$(id -u)" != 0 ]; then |
| 13 | printf '%s\n' 'pkg: package changes require root' >&2 |
| 14 | exit 1 |
| 15 | fi |
| 16 | } |
| 17 | |
| 18 | check_query() { |
| 19 | for query do |
| 20 | case "$query" in |
| 21 | ''|-*) printf 'pkg: invalid query %s\n' "$query" >&2; exit 1 ;; |
| 22 | esac |
| 23 | done |
| 24 | } |
| 25 | |
| 26 | check_package() { |
| 27 | case "$1" in |
| 28 | ''|-*|*[!a-zA-Z0-9._+-]*) |
| 29 | printf 'pkg: invalid package name %s\n' "$1" >&2 |
| 30 | exit 1 |
| 31 | ;; |
| 32 | esac |
| 33 | } |
| 34 | |
| 35 | case "${1:-}" in |
| 36 | help|-h|--help) usage; exit 0 ;; |
| 37 | '') usage >&2; exit 1 ;; |
| 38 | esac |
| 39 | command=$1 |
| 40 | shift |
| 41 | |
| 42 | case "$command" in |
| 43 | install|add|remove|delete|del|info) |
| 44 | if [ "$#" -eq 0 ]; then usage >&2; exit 1; fi |
| 45 | for package do |
| 46 | shift |
| 47 | check_package "$package" |
| 48 | if [ "$package" = x11 ]; then package=vinix-release-x11; fi |
| 49 | set -- "$@" "$package" |
| 50 | done |
| 51 | case "$command" in |
| 52 | install|add) require_root; exec /usr/bin/vpac i --needed --noconfirm "$@" ;; |
| 53 | remove|delete|del) require_root; exec /usr/bin/vpac r --recursive --noconfirm "$@" ;; |
| 54 | info) exec /usr/bin/vpac q --noconfirm "$@" ;; |
| 55 | esac |
| 56 | ;; |
| 57 | search) |
| 58 | if [ "$#" -eq 0 ]; then usage >&2; exit 1; fi |
| 59 | if [ "$1" = --exact ]; then |
| 60 | shift |
| 61 | if [ "$#" -ne 1 ]; then usage >&2; exit 1; fi |
| 62 | check_package "$1" |
| 63 | name=$1 |
| 64 | if [ "$name" = x11 ]; then name=vinix-release-x11; fi |
| 65 | matches=$(/usr/bin/vpac s --remote --quiet --noconfirm "$name") |
| 66 | if printf '%s\n' "$matches" | grep -Fxq "$name"; then printf '%s\n' "$1"; fi |
| 67 | exit 0 |
| 68 | fi |
| 69 | check_query "$@" |
| 70 | exec /usr/bin/vpac s --remote --noconfirm "$@" |
| 71 | ;; |
| 72 | list|update|upgrade) |
| 73 | if [ "$#" -ne 0 ]; then usage >&2; exit 1; fi |
| 74 | case "$command" in |
| 75 | list) exec /usr/bin/vpac s --local --quiet ;; |
| 76 | update) require_root; exec /usr/bin/vpac s --remote --sync --quiet --noconfirm >/dev/null ;; |
| 77 | upgrade) require_root; exec /usr/bin/vpac u --sync --noconfirm ;; |
| 78 | esac |
| 79 | ;; |
| 80 | *) |
| 81 | printf 'pkg: unknown command %s\n' "$command" >&2 |
| 82 | usage >&2 |
| 83 | exit 1 |
| 84 | ;; |
| 85 | esac |
| 86 | |