#!/bin/bash

B1=B
B2=("USER MENU ENTRIES")


function change_permissions {
    unalias ls 2> /dev/null
    if which $1 2> /dev/null; then
	FILE=`which $1`
	date >> $HOME/.conmen/original_permissions
	echo -e "File permissions of $FILE are:" >> $HOME/.conmen/original_permissions
	ls -l $FILE >> $HOME/.conmen/original_permissions
	chmod $2 $FILE
	chown $3 $FILE
	echo -e "\nwas changed to:" >> $HOME/.conmen/original_permissions
	ls -l $FILE >> $HOME/.conmen/original_permissions
	echo -e "\n\n" >> $HOME/.conmen/original_permissions
    else
	FILES=(`nice -19 find / -name $1 -type f -perm +111`)
	for FILE in ${FILES[@]} ; do
	    if [ -x $FILE ]; then
		date >> $HOME/.conmen/original_permissions
		echo -e "File permissions of $FILE are:" >> $HOME/.conmen/original_permissions
		ls -l $FILE >> $HOME/.conmen/original_permissions
		chmod $2 $FILE
		chown $3 $FILE
		echo -e "\nwas changed to:" >> $HOME/.conmen/original_permissions
		ls -l $FILE >> $HOME/.conmen/original_permissions
		echo -e "\n\n" >> $HOME/.conmen/original_permissions
	    fi
	done
    fi
}

function add_groups {
    /usr/sbin/groupadd -f $GRP

    /usr/sbin/usermod  -G "$GRPS" $USR

    msgbox "User \"$USR\" has been added to group \"$GRP\""
}

function test_for_sudo {
    if [ -f /etc/sudoers ]; then

	backup_conf /etc/sudoers
	CAPITALUSER=`echo $USR | sed y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/`
	local CONMEN_HOST=`hostname`

	cat /etc/sudoers | \
	sed "s|Host_Alias.*CONMEN_HOST.*|Host_Alias CONMEN_HOST=${CONMEN_HOST}|" > /etc/sudoers.tmp
	mv -f /etc/sudoers.tmp /etc/sudoers && chmod 0440 /etc/sudoers

	if ! grep -qe "CONMEN_HOST=${CONMEN_HOST}" /etc/sudoers ; then
	    echo "
################### Added by conmen ###################
Host_Alias	CONMEN_HOST=${CONMEN_HOST}
#######################################################" >> /etc/sudoers
	    msgbox "Conmen has modified the file \"/etc/sudoers\""
	fi

	if ! grep -qe "CONMEN_${CAPITALUSER}=$LINE" /etc/sudoers ; then
	    echo "
################### Added by conmen ###################
Cmnd_Alias	CONMEN_${CAPITALUSER}=$LINE
$USR		CONMEN_HOST=NOPASSWD: CONMEN_${CAPITALUSER}
#######################################################" >> /etc/sudoers
	    msgbox "Conmen has modified the file \"/etc/sudoers\""
	fi
	echo "${USR}-${CMND}" >> ${DBFILE}-${USR}

    else

	msgbox "File /etc/sudoers not found !
This feature will not work without the program \"sudo\"
Install \"sudo\" and then run this menu again"

    fi
}

function makedb {
    yesnobox "Do you want ${USR} to be able to ${STRN}"
    [ "${YNRETVAL}" = "0" -a "$1" = "--sudo" ] && test_for_sudo
    if [ "${YNRETVAL}" = "0" -a "$1" = "--add-group" ]; then
	add_groups
	echo ${USR}-${CMND} >> ${DBFILE}-${USR}
    fi
    [ "${YNRETVAL}" = "0" -a -z "$1" ] && echo ${USR}-${CMND} >> ${DBFILE}-${USR}
}

function user_cfg {
	    specify_user
	    [ -z "${INPUT}" ] && return

	    backup_conf --force ${DBFILE}-${USR}
	    > ${DBFILE}-${USR}

	    # without sudo
	    STRN="use Shell Acces ?"
	    CMND=shell
	    makedb

	    STRN="use the Printer Menu ?"
	    CMND=cmprint
	    makedb

	    STRN="use the Floppy Menu ?"
	    CMND=cmfloppy
	    makedb

	    STRN="use the Graphics Menu ?"
	    CMND=cmgraphics
	    makedb

	    STRN="use the User Configuration ?"
	    CMND=cmuser
	    makedb

	    STRN="make mp3 files?"
	    CMND=cmmp3
	    makedb

	    # with sudo
	    STRN="dial an Internet Provider ?"
	    LINE="/usr/bin/cmdialup"
	    CMND=cmdialup
	    makedb --sudo
#	    GRP=pppusers
#	    makedb --add-group
#	    change_permissions pppd 4550 root:$GRP
#	    change_permissions chat 550 root:$GRP

	    STRN="run the Games-Menu ?"
	    LINE="/usr/bin/cmgames"
	    CMND=cmgames
	    makedb --sudo
#	    GRP=games
#	    makedb --add-group
#	    change_permissions lsdoom 755 root:$GRP
#	    change_permissions lxdoom 755 root:$GRP
#	    change_permissions squake 755 root:$GRP
#	    change_permissions quake.x11 755 root:$GRP
#	    change_permissions qwcl 755 root:$GRP
#	    change_permissions glqwcl 755 root:$GRP
#	    change_permissions quake2 755 root:$GRP

	    STRN="record CD'S ?"
	    LINE="/usr/bin/cmcdrecording"
	    CMND=cmcdrecording
	    makedb --sudo
#	    GRP=cdwriter
#	    makedb --add-group
	    # Permissions on a Mandrake 7.2 system.
#	    change_permissions mkisofs 2750 root:$GRP
#	    change_permissions cdrecord 6750 root:$GRP
#	    change_permissions cdrdao 755 root:root
#	    change_permissions cdparanoia 755 root:root

	    STRN="install / uninstall / query RPMS ?"
	    LINE="/usr/bin/cmrpm"
	    CMND=cmrpm
	    makedb --sudo

	    STRN="view logfiles ?"
	    LINE="/usr/bin/cmlog"
	    CMND=cmlog
	    makedb --sudo

	    STRN="reboot the computer ?"
	    LINE="/sbin/shutdown -r now"
	    CMND=reboot
	    makedb --sudo

	    STRN="shutdown the computer ?"
	    LINE="/sbin/shutdown -h now"
	    CMND=shutdown
	    makedb --sudo

#	    msgbox "Some file permissions may have been changed,
#the original permissions were saved in
#$HOME/.conmen/original_permissions"
}
