#!/bin/bash
#
# Add the lpf group to current user.
#

scriptdir=$( dirname $(readlink -fn $0))
source $scriptdir/lpf-defs.bash


function cli_confirm_add()
# Let user confirm adding of group pkg-build to $USER (no GUI).
{
    echo -n "OK to add group $LPF_GROUP to your current user $USER? [n]: "
    read
    if [[ "$REPLY" == [Yy]* ]]; then
        return 0
    else
        return 1
    fi
}


function gui_confirm_add()
# Let user confirm adding of group pkg-build to $USER (GUI).
{
    text="You must be member of the $LPF_GROUP group to run lpf"
    text="$text (log out and in again to mute this dialog)."
    text="$text\nOK to add group $LPF_GROUP to your current user $USER?"
    zenity --question  --text "$text"
}


function add_lpf_group()
# Add user pkg-build to current user. Use sudo if possible, else pkexec.
{
    if [ -n "$DISPLAY" ]; then
        gui_confirm_add || return 1
    else
        cli_confirm_add || return 1
    fi
    if sudo -l /usr/sbin/usermod &>/dev/null; then
        $SUDO usermod -a -G $LPF_GROUP $USER
    else
        pkexec /usr/sbin/usermod -a -G $LPF_GROUP $USER
    fi
}


trap "do_trap 83" SIGINT ERR
add_lpf_group


# vim: set expandtab ts=4 sw=4:
