#getting command line arguments 
#------------------------------------
. $HPD_ROOT/bin/nr_mongodb_utils

usage()
{
        echo -e "nv_modify_kw.sh -f [<file name contains keyword>]\nnv_modify_kw.sh -k [<keyword with value, ex-\"keyword value\">] ( set keyword mode )\nnv_modify_kw.sh -r -k [<keyword only, ex-\"keyword_name\">] ( remove keyword mode )\nnv_modify_kw.sh -i [<keyword with value, ex-\"keyword value\">] (insert keyword mode )\nUSE ONE MODE AT A TIME";
  exit 1;
}

while getopts "i::rk::f::" opt
do
   case "$opt" in
      f ) file_name="$OPTARG" ;;
      k ) keyword="$OPTARG" ;;
      r ) remove=1 ;;
      i ) insert=$OPTARG ;;
      * ) usage ;; # Print usage in case parameter is non-existent

   esac
done

#if remove flag is set but keyword is not set then show usage {  remove flag is set only  when  keyword is set }
if [[ "${remove}" == 1 &&  -z "${keyword}" ]]
  then
  echo "-r flag only used with -k !";
  usage;
  fi

#if filename,insert and keyword all are empty then show usage 
if [ -z "${file_name}" ] && [ -z "${keyword}" ] && [ -z "${insert}" ] ; then
    echo "no argument !"
    usage
fi


#if  filename,insert and keyword all are given then show usage { use one mode at time }
if [[  ! -z "${file_name}"  &&  ! -z "${keyword}"  ]] || 
   [[  ! -z "${file_name}"  &&  ! -z "${insert}"   ]] || 
   [[  ! -z "${insert}"     &&  ! -z "${keyword}"  ]]; 
   then
    echo "Use One Mode Only !"
    usage

    fi
 

#---------------------------------------------------------
# MONGODB_SERVER 1 10.20.0.81 27017 test cavisson cavisson --> example line from hpd.conf ( This  line have mongodb credentials )
#get above line in db_details variable
db_details=$(cat  $HPD_ROOT/conf/hpd.conf|grep -e "^MONGODB_SERVER");
if [[ "X$db_details" == "X" ]]; then
	 echo -e "\e[1;33mWARNING: Mongo server Keyword  is Disabled in hpd.conf file. Please enable it.\e[0m "
	 exit 0
fi

#making of connection string which have format --> "mongodb://username:password@hostip:port/dbname" 
                                                                  #from line -->
                                                                  #MONGODB_SERVER <enable/disable> <ip> <port> <dbname> <username> <passowrd>"
                                                                  #which is present in hpd.conf file

hostip=$(echo $db_details|grep -e MONGODB_SERVER|cut -d ' ' -f 3);
port=$(echo $db_details|grep -e MONGODB_SERVER|cut -d ' ' -f 4);
dbname=$(echo $db_details|grep -e MONGODB_SERVER|cut -d ' ' -f 5);
username=$(echo $db_details|grep -e MONGODB_SERVER|cut -d ' ' -f 6);
password=$(echo $db_details|grep -e MONGODB_SERVER|cut -d ' ' -f 7 );


#making connection string from above variables 
#connection_string="mongodb://$username:$password@$hostip:$port/$dbname?replicaSet=rs0&authSource=admin"
#taking client id from rum.conf file 
client_id=$(cat $HPD_ROOT/rum/config/rum.conf|grep ^RUM_CLIENT|cut -d ' ' -f 2)


#get controller name 
controller_name=$(echo $HPD_ROOT|cut -d '/' -f 4);


#hpd function used to ask user for restarting hpd or not after making changes 
hpd(){
  
  printf "%s" "DO YOU WANT TO RESTART HPD [y-YES|n-NO] : ";
  read mode ;
  
  if [ $mode == 'n' ]
    then
    echo "exit without restarting HPD"
    exit 0;
  
  elif [ $mode == 'y' ]
	then
	 if [ $controller_name == "work" ]
	   then
	    /etc/init.d/hpd restart;
		  exit 0;
	 else
		  /etc/init.d/hpd_$controller_name restart;
      exit 0;
	 fi
  
  else
    echo -e "invalid argument !\nexit without restarting HPD";
    exit 0;
  fi
}

#--------------get slave id
is_cluster_mode=$(grep ^HPD_CLUSTER $HPD_ROOT/conf/hpd.conf)

if [ -f "$HPD_ROOT/rum/.slaveid" ]
  then
  slave_id=$(cat $HPD_ROOT/rum/.slaveid);
  else
    slave_id=1;
    fi

if [  -z "${is_cluster_mode}" ]
then
	slave_id=1;
fi

#-------------------------------- #insert mode --> insert one keyword at a time  ----------------------
if [ ! -z "${insert}" ]
  then
  
  line=$(echo $insert)
  N=$(echo $line|wc -w)
  parameter=$(echo $line|cut -d ' ' -f 1);
  value=$(echo $line|cut -d ' ' -f 2-$N);
  if [[  "${parameter}" !=  "NV_REPORTING_TRACE_LEVEL"        &&
       ( "${parameter}" ==  "NV_ASSIGN_MULTIDISK_TABLESPACE"  ||  
	 "${parameter}" ==  "MULTIDISK_TABLESPACE_INFO"       || 
	 "${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD" || 
	 "${parameter}" == *"NV_REPORTING_"*                  ||
	 "${parameter}" == *"NV_AGG"* )  ]];
  then
	  if [[ "${parameter}" == *"NV_REPORTING_"*  ||
	        "${parameter}" == *"NV_AGG"*  ||
		"${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD"  ]];
	  then
      run_mongo_query "db.RUM_CONFIG_${client_id}.update({'id':'$slave_id'}, {\$set:{'$parameter':'$value'}})"
	  else
      run_mongo_query "db.RUM_CONFIG_${client_id}.update({'id':'$slave_id'}, {\$push:{'$parameter':{'value':'$value'}}})"
	  fi
    
          echo -e "Keyword has been inserted in collection RUM_CONFIG_$client_id\n"
          hpd;

  else
	  if [[ "${parameter}" ==  "BLACKLIST_RESOURCE_DOMAIN_URL"   || "${parameter}" ==  "URL_GROUP" ]]
	  then
      run_mongo_query "db.RUM_CONFIG_${client_id}.update({'id':'-1'}, {\$push:{'$parameter':{'value':'$value'}}})"
	  else
      run_mongo_query "db.RUM_CONFIG_${client_id}.update({'id':'-1'}, {\$set:{'$parameter':'$value'}})"
	  fi
	  echo -e "Keyword has been inserted in collection RUM_CONFIG_$client_id\n";
          hpd;
      
      fi
  fi


#-------------------------------- #file mode --> change multiple keywords through file --------------------

if [ ! -z "${file_name}" ]
  then
  if [ ! -f "${file_name}" ]
    then
    
    echo "invalid file name !";
    exit;
    
    fi

while IFS= read -r line || [[ -n "$line" ]]
  do

    N=$(echo $line|wc -w)
    parameter=$(echo $line|cut -d ' ' -f 1);
    value=$(echo $line|cut -d ' ' -f 2-$N);

    if [[  "${parameter}" !=  "NV_REPORTING_TRACE_LEVEL"        &&
	 ( "${parameter}" ==  "NV_ASSIGN_MULTIDISK_TABLESPACE"  ||
           "${parameter}" ==  "MULTIDISK_TABLESPACE_INFO"       ||
	   "${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD" ||
           "${parameter}" == *"NV_REPORTING_"*                  ||
	   "${parameter}" == *"NV_AGG"* )  ]];
     then
	  if [[ "${parameter}" == *"NV_REPORTING_"*  ||
                "${parameter}" == *"NV_AGG"*  ||
                "${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD"  ]];
          then
                  run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'$slave_id'},{\$set:{'$parameter':'$value'}})"
          else
                  run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'$slave_id'},{\$push:{'$parameter':{'value':'$value'}}})"
          fi
     else
	     if [[ "${parameter}" ==  "BLACKLIST_RESOURCE_DOMAIN_URL"   || "${parameter}" ==  "URL_GROUP" ]]
	     then
		     run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'-1'},{\$push:{'$parameter':{'value':'$value'}}})"
	     else
		     run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'-1'},{\$set:{'$parameter':'$value'}})"
	     fi


	  #mongo "$connection_string" --eval "db.RUM_CONFIG_$client_id.update({'id':'-1'},{\$set:{'$parameter':'$value'}})"|awk ""
     fi


done < $file_name
echo -e "All keyword provided in the file has been updated in collection RUM_CONFIG_$client_id\n"
hpd;
fi


#-------------------------------------------------- #remove keyword mode --> remove one keyword at a time #------------



if [ ! -z "${remove}" ] && [ ! -z "${keyword}" ]
then
    
    parameter=$(echo $keyword);

    if [[  "${parameter}" !=  "NV_REPORTING_TRACE_LEVEL"        &&
	 ( "${parameter}" ==  "NV_ASSIGN_MULTIDISK_TABLESPACE"  ||
           "${parameter}" ==  "MULTIDISK_TABLESPACE_INFO"       ||
	   "${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD" ||
           "${parameter}" == *"NV_REPORTING_"*                  ||
	   "${parameter}" == *"NV_AGG"* )  ]];
     then
	        run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'$slave_id'},{\$unset:{'$parameter':''}})"
     else
          run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'-1'},{\$unset:{'$parameter':''}})"
     fi

    echo -e "Keyword has been removed in collection RUM_CONFIG_$client_id\n"
    hpd;
  

fi



#-------------------------------------------------- #keyword mode --> change one keyword at a time #---------------

if [ ! -z "${keyword}" ] && [ -z "${remove}" ] 
  then
  
  line=$(echo $keyword)
  N=$(echo $line|wc -w)
  parameter=$(echo $line|cut -d ' ' -f 1);
  value=$(echo $line|cut -d ' ' -f 2-$N);
  
  if [[  "${parameter}" !=  "NV_REPORTING_TRACE_LEVEL"        &&
       ( "${parameter}" ==  "NV_ASSIGN_MULTIDISK_TABLESPACE"  ||
         "${parameter}" ==  "MULTIDISK_TABLESPACE_INFO"       ||
	 "${parameter}" ==  "NV_MULTIDISK_OVERLOAD_THRESHOLD" ||
         "${parameter}" == *"NV_AGG"*                         ||
	 "${parameter}" == *"NV_REPORTING_"* )  ]];
     then
          run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'$slave_id'},{\$set:{'$parameter':'$value'}})"

     else
          run_mongo_query "db.RUM_CONFIG_$client_id.update({'id':'-1'},{\$set:{'$parameter':'$value'}})"
     fi


  echo -e "Keyword has been updated in collection RUM_CONFIG_$client_id\n"
  hpd;
  fi
#-------------------------------------------------------------------------------------
