#!/bin/bash

. ./param_bash.ini

if [ $# -lt "5" ] ; then
    ERROR "Usage: $0 <userName> <Home> <Uid> <group> <Gid> <User>" "$0"
fi
user=$1
user_home=$2
uid=$3
group=$4
gid=$5
user_to_add=$6
first="Y"

#######################################################
#funcs

UserExist()
{
    grep "^${user}:" /etc/passwd > /dev/null
    return "$?"
}
#######################################################
GetUserTocreate()
{
    echo -n "Insert the alternative name instead ${user} user (must be up to 8 characters alphanumerics lowercase) :"
    read ans
    new_user=$ans
    if [ "$new_user" = "" ] ; then
        exit 1
    fi
    user_len=`echo $new_user | wc -c`
    while [ "${user_len}" -gt "9" ]
    do
        echo "The length of the user must be less then 8 charecters!"
        echo "Please insert a valid username"
        read ans
        new_user=$ans
        user_len=`echo $new_user | wc -c`
        id $new_user > /dev/null 2>&1
        if [ "$?" = "0" ] ; then
            INFORM "This user allredy existes please reenter username"
            user_len=""
        fi
    done
    new_user=`echo $new_user|tr "[A-Z]" "[a-z]"`
    echo ""
}
#######################################################
CreateUser()
{
    out=0
    while [ "$out" != "1" -a "$uid" -lt "10000" ];
         do
                exist_group=""
                group_gid=`cat /etc/passwd | grep $uid | head -1 | cut -d ":" -f4`
                if [ "$group_gid" != "" ] ; then
                    exist_group=`cat /etc/group | grep $group_gid | cut -d ":" -f1`
                fi
                if [ "$exist_group" == "$group" ] ; then
                        user_name=`cat /etc/passwd | grep $uid | head -1 | cut -d ":" -f1`
                        if [ "$user_name" == "$user_to_add" ] ; then
                           out=1
                        else
                           let uid++
                        fi
                else
                    stat=`cat /etc/passwd | grep $uid | cut -d ":" -f3`
                    if [ "$stat" != "" ] ; then
                        let uid++
                    else
                        out=1
                    fi
                fi
        done
    if [ "$uid" -eq "10000" ] ; then
        INFORM "Error: Can not create the user."
        exit 1
    fi
    CreatingTheUser

}

CreatingTheUser()

{
    echo -n "Creating the ${user} user : "
    echo -n "Creating the ${user} user : " >> $gen_log
    if [ `uname` = "SunOS" ] ; then
        /usr/sbin/useradd -u $uid -o -g $group -d $user_home -s /bin/tcsh $user
    else
        /usr/sbin/useradd -u $uid -o -M -g $group -d $user_home -s /bin/tcsh $user
    fi
    if [ "$?" = "0" ] ; then
           echo ""
           INFORM "Info:Successfully Created $user"
           passwd $user
           if [ "${user_to_add}" != "$MAIN_PRODUCT" ] ; then
               Update_params "ORACLE_USER" "${user}"
           fi
           exit 0
    else
           echo ""
           INFORM "Info:Failed to create $user user"
           exit 1
    fi
}
#######################################################
########end funcs
if [ ! -w /etc/passwd ] ; then
   echo "Error: Please run the script as root user only."
   exit 1
fi

#check and create group
grep "^${group}:" /etc/group > /dev/null 2>&1
if [ "$?" = "0" ] ; then
     INFORM "The group ${group} already exist."
     echo
else
     INFORM "Creating group ${group} : "
     /usr/sbin/groupadd ${group}
     if [ "$?" = "0" ] ; then
           INFORM "Info:group $group created"
     else
           INFORM "Info:FAILED to create group ${group}"
           exit 1
     fi
fi
gid=`cat /etc/group | grep "^${group}:" | awk -F: '{print $3}'`
#check if user $user already exist.
id $user > /dev/null 2>&1
if [ "$?" = "0" ] ; then
    UserExist "${user}"
    if [ "$?" != "0" ] ; then
        INFORM "The user ${user} already exist, but does not exist in /etc/passwd."
        exit 0
    else
        INFORM "The user ${user} already exist."
        #Check if the user home is correct
        uid=`cat /etc/passwd | grep "^${user}:" | awk -F: '{print $3}'`
        lins=`cat /etc/passwd | grep $uid`
        for LINE in $(echo $lins)
        do
            line=$LINE
            line_uid=`echo $line | awk -F: '{print $3}'`
            if [ "${line_uid}" == "${uid}" ] ; then
                line_name=`echo $line | awk -F: '{print $1}'`
                if [ "${line_name}" != "${user_to_add}" -a "$first" == "Y" ] ; then
                    INFORM "The first user name with Uid-${uid} is not ${user_to_add}"
                    break
                else
                    first="N"
                    line_home=`echo $line | awk -F: '{print $6}'`
                    line_gid=`echo $line | awk -F: '{print $4}'`
                    if [ "${line_home}" == "${user_home}" -a "${line_gid}" == "${gid}" ] ; then
                        line_user=`echo $line | awk -F: '{print $1}'`
                        INFORM "$user user exist: ${line_user}"
			if [ "$user_to_add" = "oracle" ] ; then
                            Update_params "ORACLE_USER" "${line_user}"
			fi
                        exit 0
                    fi
                fi
            fi
        done
        GetUserTocreate
        INFORM "The user ${new_user} will be created with same uid as user ${user}"
        user=$new_user
        CreatingTheUser
        if [ "$user_to_add" = "oracle" ] ; then
           Update_params "ORACLE_USER" "oracle"
        fi
        exit 0
    fi
fi
CreateUser
