dir=/home/user1 
NMSPASSWD="pass121" 

let infiniteloop=1 
Add_user_name() { 

while [ "$option" -eq 1 ] 
do 
echo -n "Enter user-id: " 
read user_name 

         ### check for spaces or blank username 

if test -z "$user_name" ; then 
echo " " 
echo " Error: Blank username not allowed" 
echo " " ; echo " " 
sleep 2 
break 
# continue 
fi 

         ### Check for user name length 

         if test `expr length $user_name` -lt 4 ; then 
echo " Error : User name should be at least 4 character long " 
echo " " ; echo " " 
sleep 2 
continue 
         fi 

### Check if user already exist in database    

testuser=`cat /home/dir1/user_name.txt | tr -s ' ' | cut -f 1
-d ' ' | grep -w $user_name` 
if test "$testuser" = "$user_name" ; then 
echo " Error : User already exist" 
echo " " ; echo " " 
sleep 2 
continue 
                          
           
         else 
          echo -n "Enter password: " 
  read user_password 
    
         fi 
                  ## Check for space or blank password 
          if test -z "$user_password" ; then 
  echo " " 
  echo " Error : Spaces and blank password are not allowed." 
                  echo " " ; echo " " 
  sleep 2 
  continue 
          fi 


                  ##  Check for password length 
          if test `expr length $user_password` -lt 4 -o `expr
length $user_password` -gt 8 ; then 
  echo " " 
  echo " Error : Password length must be in between 4 and 8
characters " 
  echo " " ; echo " " 
  sleep 2 
  continue 
          fi 


                  echo -n "Enter \"$user_name's\" company1 Login
ID (e.g. mshoaib,shezy, etc..): " 
  read company1_id 

         ### check for spaces or blank company1 ID 

if test -z "$company1_id" ; then 
echo " " 
echo " Error: Blank ID is not allowed" 
echo " " ; echo " " 
sleep 2 
continue 
fi 

    

         ###  Adding user_name to Group 
    
  echo " " ; echo " " 
  echo " Please select the group for user \"$user_name\" " 
  echo " " ; echo " " 
    
  echo  "1. End User Courses. " 
  echo  "2. Technical Courses. " 
  echo  "3. Both (End User + Technical) Courses. " 
  echo -n " Option: " 
          read group_option 
  echo " " 
      case $group_option in 
1) 
      echo "End User Courses selected for \"$user_name\"" 
;; 
2) 
      echo "Technical Courses selected for \"$user_name\"" 
;; 
3) 
      echo "Both (End User + Technical Courses) selected for
\"$user_name\"" 
;; 
*) 
      echo "Invalid Input " 
continue 
;; 
      esac 
     


         ###  Adding Duration in months 
    
  echo " " 
  echo -n "Please enter number of months: " 
#   echo " " ; echo " " 
    
  read no_of_months 

      case $no_of_months in 
      1|2|3|4|5|6|7|8|9|10|11|12) 
      ;; 
      *) 
      echo "$no_of_months months are not allowed" 
      sleep 1 
      continue 
      ;; 
      esac 
    
#       if  test $no_of_months -eq 0 -o $no_of_months -gt 12 ;
then 
#       echo "$no_of_months months are not allowed" 
#       sleep 2 
#       continue 
#       fi 

echo -n "Enter Amount Paid in Rs.: " 
read amount 

         ### check for spaces or blank amount 

if test -z "$amount" ; then 
echo " " 
last_name="Not_Entered" 
else 
dummy=`echo "$last_name" | tr ' ' '_' ` 
last_name=`echo "$dummy"` 
fi 



echo -n "Enter First Name: " 
read first_name 

         ### check for spaces or blank username 

if test -z "$first_name" ; then 
echo " " 
echo " Error: Blank Firstname is not allowed" 
echo " " ; echo " " 
sleep 2 
continue 
else 
dummy=`echo "$first_name" | tr ' ' '_' ` 
first_name=`echo "$dummy"` 
fi 

echo -n "Enter Last Name: " 
read last_name 

         ### check for spaces or blank username 

if test -z "$last_name" ; then 
echo " " 
last_name="Not_Entered" 
else 
dummy=`echo "$last_name" | tr ' ' '_' ` 
last_name=`echo "$dummy"` 
fi 

echo -n "Enter NIC (e.g. 520-78-453323): " 
read nic 

         ### check for spaces or blank NIC 

if test -z "$nic" ; then 
echo " " 
echo " Error: Blank NIC is not allowed" 
echo " " ; echo " " 
sleep 2 
continue 
else 
dummy=`echo "$nic" | tr ' ' '-' ` 
nic=`echo "$dummy"` 
fi 



echo -n "Enter Company Name: " 
read comp_name 

         ### check for spaces or blank username 

if test -z "$comp_name" ; then 
echo " " 
comp_name="Not_Entered" 
else 
dummy=`echo "$comp_name" | tr ' ' '_' ` 
comp_name=`echo "$dummy"` 
fi 

echo -n "Enter Address: " 
read address 

         ### check for spaces or blank username 

if test -z "$address" ; then 
echo " " 
address="Not_Entered" 
else 
dummy=`echo "$address" | tr ' ' '_' ` 
address=`echo "$dummy"` 
fi 

echo -n "Enter Telephone# : " 
read telephone 

         ### check for spaces or blank telephone 

if test -z "$telephone" ; then 
echo " " 
telephone="Not_Entered" 
fi 

echo -n "Enter Fax# : " 
read fax 

         ### check for spaces or blank telephone 

if test -z "$fax" ; then 
echo " " 
fax="Not_Entered" 
fi 



echo -n "Add user \"$user_name\" info in a database (y\n): " 
read choice 
case $choice in 
n) echo "you typed n" 
          continue 
;; 
N)  echo "you typed N" 
            continue 
;; 
y)  echo "you typed y" 
     break 
;; 
Y)  echo "you typed Y" 
                    break 
;; 
*)  echo "you typed invalid" 
       ;; 
esac 

done 
### Saving user record    
echo -n "`date` $operator_name $nmspassword $user_name
$user_password " >> $dir/dir1.comd 
echo -n "$group_option $no_of_months $first_name $last_name
$comp_name " >> $dir/dir1.comd 
echo "$address $telephone $fax $nic $company1_id $amount
xyz.net.pk " >> $dir/dir1.comd 

echo -n "`date` $operator_name $nmspassword $user_name
$user_password " >> $dir/service.log 
echo -n "$group_option $no_of_months $first_name $last_name
$comp_name " >> $dir/service.log 
echo "$address $telephone $fax $nic $company1_id $amount
xyz.net.pk " >> $dir/service.log 


} # close braces for Add_user_name() 



Exit() { 
        infiniteloop=0 
echo " Have fun !! " 
          
       } 

                 
#---------------------------------------------------------------------------



                #--------------------------------------------# 
################  Main Programm ############## 
#--------------------------------------------# 

### First enter into an infinite loop 
operator_name=`echo $USER` 
nmspasswordloop=1 
letexit=0 
while [ $nmspasswordloop -eq 1 ] 
        do 

clear 
echo " company1 Private Limited " 
echo " " 
echo -n "Enter NMS password:" 
stty -echo 

read nmspassword 
stty echo 
# echo $nmspassword 

    if test "$nmspassword" != "$NMSPASSWD" ; then 
echo " "; echo " " 
echo "NMS Password does not match" 
echo "Try again !" 
sleep 1 
let letexit=$letexit+1 
if test $letexit -eq 2 ; then 
exit 
fi 
continue 
    fi 

        ### Following  lines are useless; program will never
enter into that 
### area anymore. 

if test -z "$nmspassword" ; then 
echo " " 
echo "Have you forgotten your password?" 
echo "Your complete information has been sent" 
echo " to company1 System Administrator." 
sleep 1 
continue 
fi 

if test `expr length $nmspassword` -le 3 ; then 
echo " " 
echo "This password is invalid." 
echo "You seem to be intruder." 
echo "I am emailing all your inofo to company1 " 
echo "Network Administrator" 
sleep 2 
continue 

else 
nmspasswordloop=0 
fi 
done 



#  Enter into an infinite loop 

while [ "$infiniteloop" -eq 1 ] 
do 
clear 
echo 
"--------------------------------------------------------" 
echo  " Slect from the menu                    " 
echo  "-------------------------------------------------------
" 
let option=0 
echo  "1. Add a User-id." 
echo  "2. Refresh User-id. " 
echo  "3. Exit. " 
echo -n " Choice : " 
read option ; 


case $option in 
1) 
  Add_user_name 
;; 
3) 
  Exit 
;; 
*) 
  echo "Bad command or file name" 
;; 
esac 
done