#!/bin/bash
set -u

COMMON=/usr/lib/onix/onix-pkg-common
if [[ ! -r "$COMMON" ]]; then
    echo "onix: package helper is missing: $COMMON" >&2
    exit 1
fi
# shellcheck disable=SC1091
. "$COMMON"

usage() {
    cat <<'EOF'
OnixOS package manager

Usage:
  opkg update                 Full system update
  opkg install <packages...> Install packages with a full upgrade
  opkg remove <packages...>  Remove packages and unused dependencies
  opkg clean                 Remove unused package cache entries
  opkg mirror                Refresh mirrors, then update the keyring
EOF
}

case "${1:-help}" in
    update)
        refresh_mirrors || exit 1
        full_upgrade
        ;;
    install)
        shift
        install_packages "$@"
        ;;
    remove)
        shift
        remove_packages "$@"
        ;;
    clean)
        clean_cache
        ;;
    mirror)
        refresh_mirrors || exit 1
        prepare_keyring
        ;;
    help|-h|--help)
        usage
        ;;
    *)
        echo "onix: unknown command: $1" >&2
        usage >&2
        exit 2
        ;;
esac
