#!/bin/bash


######################################
#### Default Vars ####################
######################################

user_groups=""
default_groups=(users disk network netdev floppy fuse video lp tty audio cdrom scanner adm vboxusers wheel)

# Avoid spaces troubles :)
OLD_IFS=$IFS
IFS="|
"
KDM_CONFFILE="/etc/kde/kdmrc"
GDM_CONFFILE="/etc/gdm/custom.conf"
LXDM_CONFFILE="/etc/lxdm/lxdm.conf"

######################################
#### Functions #######################
######################################

check_args()
{
    description="$1"
    name="$2"

    name_test=`echo -n "$name" | sed 's@^[a-z][a-z0-9]*$@@g'`
    if [ "$name_test" != "" ]; then
	echo 1>&2 "HATA=kullanıcı sorunlu kareketerler içerrmektedir."
	return 1
    fi
    if grep "$name" /etc/passwd > /dev/null; then
	echo 1>&2 "$name kullanıcısı zaten var!"
	return 2
    fi

    desc_test=`echo "$description" | sed 's@^[[:alnum:]! -.,~_@;%<>?]*$@@ig'`
    if [ "$desc_test" != "" ]; then
	echo 1>&2 "HATA=Uzun ismi ! -.,~_\\\@;%<>? karekerleri içeremez.Harf-sayı olmalıdır! "
	return 3
    fi
    return 0
}

usage()
{
  echo 1>&2 'KULLANIM:
 ko kullanıcı kullanıcı_ismi'
  exit 1
}

# kullanıcıya Milis Xfce4 öntanımlı masaüstü ayarlarının kopyalanması
xfce4_ayarla(){
	if [ $1 ];then
		_isim=$1
		mkdir -p /home/${_isim}/.config
		if [ -d /etc/skel/xfce4 ];then
			cp -rf /etc/skel/xfce4 /home/${_isim}/.config/
		else
			cp -rf /root/.config/xfce4 /home/${_isim}/.config/
		fi
	else
		echo "kullanıcı parametresi eksik"
	fi
}

if [ $# -eq 1 -o $# -gt 2 ]; then
    usage
fi

if [ $UID -ne 0 ]; then
    echo 1>&2 "Bu betik root yetkileriyle çalışmaktadır."
    exit 1;
fi

if [ $# -lt 2 ]; then
    echo -n "Kullanıcı: "
    read name
    echo -n "Uzun ismi: "
    read description
else
    description="$1"
    name="$2"
fi

check_args "$description" "$name"
ret=$?
while [ $ret -ne 0 ]; do
    if [ $ret -lt 3 ]; then
	echo -n "Kullancı: "
	read name
    fi
    if [ $ret -eq 3 ]; then
	echo -n "Uzun ismi: "
	read description
    fi
    if [ "$name" == "!stop!" -o "$description" == "!stop!" ]; then
	exit 1
    fi
    check_args "$description" "$name"
	ret=$?
done

i=0
while [ -n "${default_groups[i]}" ]; do
    if grep "${default_groups[i]}" /etc/group > /dev/null ; then
		if [ -z "$user_groups" ]; then
			user_groups="${default_groups[i]}"
		else
			user_groups="$user_groups,${default_groups[i]}"
		fi
    fi
    i=$((i+1))
done

echo 1>&2 "
$name kullanıcısının oluşturulması.
"



if [ -z "$user_groups" ]; then
    /sbin/useradd -c "${description}" -m "${name}" || exit 1
else
    /sbin/useradd -c "${description}" -G "$user_groups" -m "${name}" || exit 1
fi
if [ -f /root/.xinitrc ]; then
	cp /root/.xinitrc /home/${name}
fi


passwd "$name"

while true; do
	if [ `pgrep xfce4-session` ];then
		echo "${name} için Milis Xfce4 öntanımlı masaüstü ayarlarsın mı?";read -p "e veya h-> " eh
		case $eh in
			[Ee]* ) xfce4_ayarla ${name}; break;;
			[Hh]* ) break;;
			* ) echo "e veya h";;
		esac
	fi
	exit 0
done

# kullanıcı izinlerin ayarlanması
if [ -d /home/${name} ]; then
	#evdizini
	chown -R ${name}:${name} /home/${name}
	#ses aygıtları
	setfacl -m u:${name}:rw /dev/snd/* 
fi

exit 0