#!/bin/bash
# Скрипт настройки /etc/sudoers для пользователя (Script for configuring /etc/sudoers for user)
# Автор - Логинов Алексей <loginov.alex.valer@gmail.com> (Author - Loginov Alexey <loginov.alex.valer@gmail.com>)
# Скрипт распространяется по лицензии GPL v.3 (Licence GPL v.3)

export TEXTDOMAIN="xsudo"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

USER0=$1
SCRIPT=$2

MES0="$(eval_gettext 'Do you want to cancel for the user $USER0 to run applications via sudo/xsudo without entering the administrator password?')"
MES1="$(eval_gettext 'Do you want to allow for the user $USER0 to run applications via sudo/xsudo without entering the administrator password?')"
MES2="$(eval_gettext 'User $USER0 was added to /etc/sudoers. If to use sudo/xsudo, then password of user will be prompted.')"
MES3="$(eval_gettext 'User $USER0 was removed from /etc/sudoers.')"
MES4="$(eval_gettext 'User $USER0 already in /etc/sudoers, addition is not required.')"
MES5="$(eval_gettext 'User $USER0 is not in /etc/sudoers, removal is not required.')"
MES6="$(gettext 'It is not necessary to configure /etc/sudoers for root.')"
MES7="$(gettext 'You must be root.')"
MES8="$(gettext 'Requires a username in the first parameter.')"
MES9="$(eval_gettext 'User $USER0 does not exist.')"
MES10="$(gettext 'The second parameter is required to specify: add2sudoers or rmfromsudoers.')"
MES11="$(gettext 'Choice of a variant for a password')"
MES12="$(gettext 'Choose variant with a password PASSWD (recommended) or without a password NOPASSWD (too dangerous):')"
MES13="$(gettext 'Marker of choice')"
MES14="$(gettext 'The choices')"
MES15="$(gettext 'Further configuration was interrupted')"
MES16="$(eval_gettext 'User $USER0 was added to /etc/sudoers. If to use sudo/xsudo, then password of user will not be prompted.')"

if [ ! "$USER" = "root" ]
then
  zenity --info --text "$MES7"
  exit 0
fi

if [ "$USER0" = "" ]
then
  zenity --info --text "$MES8"
  exit 0
fi

if [ ! -d /home/$USER0 ]
then
  zenity --info --text "$MES9"
  exit 0
fi

if [ "$USER0" = "root" ]
then
  zenity --info --text "$MES6"
  exit 0
fi

if [ ! "$SCRIPT" = "add2sudoers" ] && [ ! "$SCRIPT" = "rmfromsudoers" ]
then
  zenity --info --text "$MES10"
  exit 0
fi

TESTROOT=`cat /etc/sudoers|grep "$USER0 ALL=(ALL)"|grep "PASSWD:"`

if [ "$SCRIPT" = "add2sudoers" ]
then
    if [ ! "$SCRIPT" = "rmfromsudoers" ]
    then
	if [ "$TESTROOT" = "" ]
	then
	  zenity --question --text "$MES1"
	  if [ "$?" = "1" ]
	  then
	    exit 0
	  else
	    A=`zenity --list --radiolist --title="$MES11" --text="$MES12" --column="$MES13" --column="$MES14" TRUE PASSWD FALSE NOPASSWD`
	    if [ ! "$A" = "PASSWD" ]
	    then
	      if [ ! "$A" = "NOPASSWD" ]
	      then
	        zenity --info --text "$MES15"
	        exit 0
	      fi
	    fi
	    if [ "$A" = "PASSWD" ]
	    then
	       echo "$USER0 ALL=(ALL) PASSWD: ALL" >> /etc/sudoers
	       zenity --info --text "$MES2"
	    else
	       echo "$USER0 ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
	       zenity --info --text "$MES16"
	    fi
	  fi
	else
	  zenity --info --text "$MES4"
	fi
    fi
fi
 
if [ "$SCRIPT" = "rmfromsudoers" ]
then
    if [ ! "$SCRIPT" = "add2sudoers" ]
    then
	if [ ! "$TESTROOT" = "" ]
	then
	  zenity --question --text "$MES0"
	  if [ "$?" = "1" ]
	  then
	    exit 0
	  else
	    sed "/$USER0 ALL=(ALL) PASSWD: ALL/d" /etc/sudoers > /tmp/sudoers
	    mv -f /tmp/sudoers /etc/sudoers
	    sed "/$USER0 ALL=(ALL) NOPASSWD: ALL/d" /etc/sudoers > /tmp/sudoers
	    mv -f /tmp/sudoers /etc/sudoers
	    chmod 0440 /etc/sudoers
            zenity --info --text "$MES3"
	  fi
	else
	  zenity --info --text "$MES5"
	fi 
    fi
fi