#!/usr/bin/sh
 check_loop=1
 
 write_record_function()
 {
 echo "file in use" >> lock_file.txt
 echo "creatnew" $user_id $password $first_name $last_name $create_date $home_phone $hours_purchased $dealer_name >> record_file.sh 
 echo "creatnew" $user_id $password $first_name $last_name $create_date $home_phone $hours_purchased $dealer_name >> temp_record_file.sh 
 rm lock_file.txt
 sleep 2
 main_menu_function
 }

 pre_add_record_function()
 {
 echo "You have entered following information.\n"
 echo "$user_id"
 echo "$first_name $last_name"
 echo "$password"
 echo "$create_date"
 echo "$home_phone"
 echo "$hours_purchased"  
 echo "$dealer_name"\
 echo " "
 echo "Do you want to continue? [y/n]"
  yn=n
  read yn
  if test "$yn" = "y"
  then  
  write_record_function
  elif test "$yn" = "Y"
  then
  write_record_function
  else
  main_menu_function
  fi
 }


 add_normal_user_function() {
 echo "Enter user id:"
 read  user_id
 ## check for spaces of blank user name
 if test -z "$user_id" ; then
 echo " "
 echo "Error: Blank user name not allowed"
 echo " "
 sleep 2
 main_menu_function 
 fi
 
 ## check for character length not less than 3
 if test `expr "$user_id" : '.*'` -lt 3 ; then
 echo " "
 echo "Error: User name should be at least 3 characters long."
 echo " " 
 sleep 2
 main_menu_function
 fi

 ## check for character length not greate than 8
 if test `expr "$user_id" : '.*'` -gt 8 ; then
 echo " "
 echo "Error: User name should not excede 8 charaters."
 echo " " 
 sleep 2
 main_menu_function
 fi
 
 ##Check if user_id already exists or not
 testuser=`cat record_file.sh | cut -f 2 -d ' '|grep $user_id`
 if test "$testuser" = "$user_id" 
 then
 echo " "
 echo "Error: User name already exists. Please try again."
 echo " "
 sleep 2
 main_menu_function
 fi 

  echo "Enter Password :"
  read password
 ## check for spaces of blank user name
 if test -z "$password" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function
 fi
 
 ## check for password length not less than 4
 if test `expr "$password" : '.*'` -lt 4 ; then
 echo " "
 echo "Error: Length should be at least 4 characters long."
 echo " " 
 sleep 2
 main_menu_function
 fi

 ## check for pasword length not greate than 8
 if test `expr "$password" : '.*'` -gt 8 ; then
 echo " "
 echo "Error: Length should not excede 8 charaters."
 echo " " 
 sleep 2
 main_menu_function
 fi

 echo "Enter First Name :"
 read first_name 
 ## check for spaces of blank user name
 if test -z "$first_name" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function
 fi

 echo "Enter Last Name :"
 read last_name 
 ## check for spaces of blank user name
 if test -z "$last_name" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function 
 fi


 echo "Enter create_date [dd-mm-yyyy]:"
 read create_date
 ## check for spaces of blank user name
 if test -z "$create_date" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function
 fi


  echo "Enter Home Phone#:"
 read home_phone
 ## check for spaces of blank user name
 if test -z "$home_phone" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function 
 fi


 echo "Enter Hours Purchased:"
 read hours_purchased
 ## check for spaces of blank user name
 if test -z "$hours_purchased" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function
 fi


 echo "Enter Dealer Name:"
 read dealer_name
 ## check for spaces of blank user name
 if test -z "$dealer_name" ; then
 echo " "
 echo "Error: Blank fields not allowed"
 echo " "
 sleep 2
 main_menu_function 
 fi
 pre_add_record_function
 }

 change_user_password_function()
 {
 echo "changing password not enabled yet. I have told you. You are idiot,"
 echo "still pressing this option."
 echo 
 }

 disable_account_function()
 {
 
 echo "file in use" >> lock_file.txt
 echo "creatfrz" $user_idd $date >> disable_account.sh 
 echo "creatfrz" $user_idd $date >> temp_disable_account.sh 
 rm lock_file.txt
 sleep 2


  echo "Nothing"
 sleep 2
 main_menu_function
 } 
 
 
 main_menu_function() {
 clear
 sleep 2
 echo "\n\n   Gem Internet Services (Pvt.) Ltd. Network Management System."
 echo "    -----------------------------------------------------------\n\n"
 echo  "1)Add user"
 echo  "2)Change user password [This option is not enabled yet]"
 echo  "3)Disable account"
 echo  "4)Refresh account"
 echo  "5)Exit"
 echo  "\nEnter your choice"
 read option
 
        
 case $option in
        1)
        sleep 2
        add_normal_user_function
        ;;
        2)
        sleep 2
        change_user_password_function
        ;;
        3)
        sleep 2
        #disable_account_function

        ### This function disables a user account.      
        echo "Enter user_id:"
        read user_idd
        
        ##Check if user_id exists or not
        testuser=`cat record_file.sh | cut -f 2 -d ' '|grep $user_idd`
        if test "$testuser" != "$user_idd" 
        then
        echo " "
        echo "Error: User name doesn't exist. Please try again."
        echo " "
        sleep 2
        main_menu_function
        fi

        echo "Do you really want to disable user_id $user_idd? [y/n]"
        yn=n
        read yn
        if test "$yn" = "y"
        then  
        disable_account_function
        elif test "$yn" = "Y"
        then
        disable_account_function
        else
        main_menu_function
        fi
        ;;    
        5)
        echo "Exiting ........"
        sleep 2
        exit
        ;;
        *)
        echo "Please type correct option"
        echo "Hit any key to retun to main menu."
        read garbage
        main_menu_function
        ;;
 esac
 }

 main_menu_function