#!/usr/bin/env sh
# shellcheck shell=sh
# shellcheck disable=SC2016
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
USER_UID="$(id -u)"
USER_GID="$(id -g)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -x "$(command -v apt 2>/dev/null)" ]; then
  export DEBIAN_FRONTEND=noninteractive
  pkmgr_cmd="apt"
  pkmgr_clean_cmd="$pkmgr_cmd clean"
  pkmgr_mkcache_cmd="$pkmgr_cmd update"
  pkmgr_update_cmd="$pkmgr_cmd upgrade -yy"
  pkmgr_install_cmd="$pkmgr_cmd install -yy $PKMGR_OPTS"
  pkmgr_install_post="$pkmgr_cmd --fix-broken install"
elif [ -x "$(command -v apt-get 2>/dev/null)" ]; then
  export DEBIAN_FRONTEND=noninteractive
  pkmgr_cmd="apt-get"
  pkmgr_clean_cmd="$pkmgr_cmd clean"
  pkmgr_mkcache_cmd="$pkmgr_cmd update"
  pkmgr_update_cmd="$pkmgr_cmd upgrade -yy"
  pkmgr_install_cmd="$pkmgr_cmd install -yy $PKMGR_OPTS"
  pkmgr_install_post="$pkmgr_cmd --fix-broken install"
elif [ -x "$(command -v dnf 2>/dev/null)" ]; then
  pkmgr_cmd="dnf"
  pkmgr_clean_cmd="$pkmgr_cmd clean all"
  pkmgr_mkcache_cmd="$pkmgr_cmd makecache"
  pkmgr_update_cmd="$pkmgr_cmd update -y --skip-broken $PKMGR_OPTS"
  pkmgr_install_cmd="$pkmgr_cmd install -y --skip-broken $PKMGR_OPTS"
elif [ -x "$(command -v yum 2>/dev/null)" ]; then
  pkmgr_cmd="yum"
  pkmgr_clean_cmd="$pkmgr_cmd clean all"
  pkmgr_mkcache_cmd="$pkmgr_cmd makecache"
  pkmgr_update_cmd="$pkmgr_cmd update -y --skip-broken $PKMGR_OPTS"
  pkmgr_install_cmd="$pkmgr_cmd install -y --skip-broken $PKMGR_OPTS"
elif [ -n "$(command -v pacman 2>/dev/null)" ]; then
  pkmgr_cmd="pacman"
  pkmgr_mkcache_cmd="true"
  pkmgr_clean_cmd="$pkmgr_cmd -Scc --noconfirm"
  pkmgr_update_cmd="$pkmgr_cmd -Syyu --noconfirm $PKMGR_OPTS"
  pkmgr_install_cmd="$pkmgr_cmd -Syy --noconfirm $PKMGR_OPTS"
elif [ -x "$(command -v apk 2>/dev/null)" ]; then
  pkmgr_cmd="apk"
  pkmgr_mkcache_cmd="true"
  pkmgr_clean_cmd="$pkmgr_cmd cache clean"
  pkmgr_update_cmd="$pkmgr_cmd -U upgrade --no-cache $PKMGR_OPTS"
  pkmgr_install_cmd="$pkmgr_cmd add --no-cache $PKMGR_OPTS"
elif [ -x "$(command -v zypper 2>/dev/null)" ]; then
  pkmgr_cmd="zypper"
  pkmgr_mkcache_cmd="true"
  pkmgr_clean_cmd="$pkmgr_cmd clean --all"
  pkmgr_update_cmd="$pkmgr_cmd update -y $PKMGR_OPTS"
  pkmgr_install_cmd="$pkmgr_cmd install -y $PKMGR_OPTS"
else
  pkmgr_cmd="true"
  pkmgr_mkcache_cmd="$pkmgr_cmd"
  pkmgr_clean_cmd="$pkmgr_cmd"
  pkmgr_update_cmd="$pkmgr_cmd"
  pkmgr_install_cmd="$pkmgr_cmd"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "/config/pkmgr/settings.conf" ]; then
  . "/config/pkmgr/settings.conf"
elif [ -f "/etc/pkmgr/settings.conf" ]; then
  . "/etc/pkmgr/settings.conf"
else
  mkdir -p "/config/pkmgr"
  cat <<EEOF >"/config/pkmgr/settings.conf"
pkmgr_cmd="$pkmgr_cmd"
pkmgr_clean_cmd="$pkmgr_clean_cmd"
pkmgr_update_cmd="$pkmgr_update_cmd"
pkmgr_install_cmd="$pkmgr_install_cmd"
pkmgr_mkcache_cmd="$pkmgr_mkcache_cmd"
EEOF
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -n "$pkmgr_cmd" ] || { echo "Can not determine the package manager" && exit 1; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case "$1" in
pip)
  shift 1
  pip_bin="$(command -v python3 2>/dev/null || command -v python2 2>/dev/null || command -v python 2>/dev/null || echo "")"
  py_version="$($pip_bin --version | sed 's|[pP]ython ||g' | awk -F '.' '{print $1$2}' | grep '[0-9]' || echo "0")"
  [ "$py_version" -gt "310" ] && pip_opts="--break-system-packages " || pip_opts=""
  case "$1" in
  install)
    shift 1
    pkg_list="$*"
    for pkg in $pkg_list; do
      $pip_bin -m pip $pip_opts "$pkg"
    done
    ;;
  *)
    for pkg in "$@"; do
      $pip_bin -m pip "$pkg"
    done
    ;;
  esac
  exit
  ;;
install)
  shift 1
  [ -n "$1" ] || exit 0
  [ "$USER_UID" -eq 0 ] || [ "$USER" = "root" ] || pkmgr_install_cmd="sudo $pkmgr_install_cmd"
  if [ -f "$1" ]; then
    install_list="$(cat "$1")"
  else
    install_list="$*"
  fi
  for pkg in $install_list;do
    echo "installing packages command: $pkmgr_install_cmd $pkg"
    $pkmgr_install_cmd $pkg
    if [ -n "$pkmgr_install_post" ]; then eval $pkmgr_install_post; fi
  done
  exit
  ;;
update | upgrade)
  shift $#
  [ "$USER_UID" -eq 0 ] || [ "$USER" = "root" ] || pkmgr_update_cmd="sudo $pkmgr_install_cmd"
  echo "Updating packages command: $pkmgr_update_cmd"
  $pkmgr_mkcache_cmd
  $pkmgr_update_cmd
  exit $?
  ;;
clean)
  shift $#
  [ -n "$1" ] || exit 0
  [ "$USER_UID" -eq 0 ] || [ "$USER" = "root" ] || pkmgr_clean_cmd="sudo $pkmgr_clean_cmd"
  echo "Cleaning package cache: $pkmgr_clean_cmd"
  $pkmgr_clean_cmd
  exit $?
  ;;
*)
  [ -n "$1" ] || exit 0
  [ "$USER_UID" -eq 0 ] || [ "$USER" = "root" ] || pkmgr_cmd="sudo $pkmgr_cmd"
  echo "executing packages command: $pkmgr_cmd $*"
  $pkmgr_cmd "$@"
  exit $?
  ;;
esac
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# end
