# This file is part of ctkarch-sysconfig.
#
# ctkarch-sysconfig is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

menu() {
  [ -d "${systemroot}/etc/polkit-1/rules.d" ] || { mydialog --msgbox "$_pknotinstalled" 10 72; return 0; }

  local items=('allow'    "$_allowstoragegroup"
               'disallow' "$_disallow")
  local item="${items[0]}" err
  [ -f "${systemroot}/etc/polkit-1/rules.d/10-internalmount-auth.rules" ] || item="${items[2]}"
  while
    item="$(dialogmenu --default-item "$item" 2>&1>&3)"
    [[ "$item" != '' ]]
  do
    if err="$(setpkinternalmounts "$item" 2>&1>&3)" ;then
      mydialog --msgbox "$([ "$item" = allow ] && echo "$_explain_allowstoragegroup" || echo "$_explain_disallow")" 10 72
    else
      mydialog --msgbox "${err}" 10 72
    fi
  done
  return 0
}

# Allow users in the storage group with a local and active session to mount internal filesystems
# parms: allow|disallow
setpkinternalmounts() {
  case "$1" in
    'allow')
      cp "${moduledir}/res/10-internalmount-auth.rules" "${systemroot}/etc/polkit-1/rules.d/" || return 1
    ;;
    'disallow')
      rm -f "${systemroot}/etc/polkit-1/rules.d/10-internalmount-auth.rules" || return 1
    ;;
    *)
      echo "invalid argument '$1'" >&2
      return 1
    ;;
  esac
  echo "internal mounts for group storage: ${1}"
}
