#!/bin/bash

OLD_PATH="/usr/local/telescope/"
NEW_PATH="/usr/local/uniagent/extension/install/telescope/"
USER_CONF_NAME='dc-nqa-user-conf.json'

MYDIR=`pwd`
txt_path=$MYDIR'/user.txt'

if [  -f "new.txt" ]
then
    rm -rf new.txt
fi


grep -v '^\s*$' $txt_path>new.txt


path_num=0
num=0
for line in `cat new.txt | sed 's/ //g'`
do
	result=$(echo ${line} | grep "https")
    if [[ "$result" != "" ]]
    then
		collector_path=`echo ${line}`
		if ! [[ "$collector_path" == https://*collector ]]
		then
		    echo "please provide the right collector_path."
			exit 1
		else
			((path_num++))
		fi 	
	else  
        resource=$(echo ${line} | grep ",")
		if [[ "$resource" != "" ]]
		then
			count=` echo $resource|tr -cd ','|wc -c`
		    if [  $count != 1 ]
		    then
		        echo "Invalid input. Please provide one vif_or_dc_id and one remote_ip."
				exit 1
			else
				((num++))
		    fi			
		fi 	
	fi
done


if [  $path_num != 1 ]
then
    echo "Invalid input. Please provide one download path of the dc plugin." 
	exit 1
fi


collector_name=${collector_path##*/}
download_path=${collector_path%/*}
nqa_conf_path=$download_path"/dc-nqa-conf.json"

if  [[ "$flag" == "false" ]]
then
    echo "please provide the right collector_name."
    exit 1
fi


USER_CONF_NAME='dc-nqa-user-conf.json'
vif_or_dc='virtual_interface_id'
if [  $collector_name != 'dc-nqa-collector' ]
then
    USER_CONF_NAME='history-dc-nqa-user-conf.json'
    vif_or_dc='history_direct_connect_id'
fi


i=0
resource_conf=""
max_ind=`expr $num - 1`
for line in `cat new.txt | sed 's/ //g'`
do
	result=$(echo ${line} | grep ",")
    if [[ "$result" != "" ]]
    then
        resource=`echo ${line}`
	    resource=${resource//,/ }  
		
		arr1=($resource)
		vif_or_dc_id=${arr1[0]}
		vif_or_dc_id=`echo $vif_or_dc_id`
		remote_ip=${arr1[1]}
		remote_ip=`echo $remote_ip`
		
		user_conf="{ \"$vif_or_dc\": \"$vif_or_dc_id\", \n \"remote_ip\": \"$remote_ip\",\n \"packet_size\": 1024 }"
	
		if [ $i == $max_ind ]
		then
            resource_conf="$resource_conf""$user_conf"""   
        else
            resource_conf="$resource_conf""$user_conf"",\n"  
        fi
		 ((i++))
	
		if ! [[ $vif_or_dc_id =~ ^([a-z0-9]{8})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{12})$ ]] && ! [[ $vif_or_dc_id =~ ^([a-z0-9]{32})$ ]]
        then
            echo "The "$vif_or_dc" $vif_or_dc_id is error, please try again."
            exit 1
        fi
		
		if ! [[ $remote_ip =~ ^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])$ ]]
		then
		    echo "please provide the right remote_ip "$remote_ip"."
			exit 1
		fi		
		
		echo "Start to check the connectivity of the remote_ip "$remote_ip"."
		ping -c 5 -w 30 $remote_ip
		if [[ $? != 0 ]]
		then
		    echo "Ping fail, please make sure that the route of the remote_ip "$remote_ip" is reachable."
			exit 1
		else
			echo "Ping success. Continue to install "$collector_name
		fi

    fi
	
done
  
 
if [[ -f /usr/local/uniagent/extension/install/telescope/bin/telescope ]]
then
/usr/local/uniagent/extension/install/telescope/bin/telescope -v
path=$NEW_PATH
else
path=$OLD_PATH
fi


cd $path
if [[ $? != 0 ]]
	then
	    echo "Failed to enter the directory. Please make sure that the telescope agent is installed."
	    exit 1
fi

mkdir -p plugins
cd plugins
mkdir -p dc
cd dc


if [  -f "$path"plugins/dc/"$collector_name" ]
then
    rm -rf $path"plugins/dc/"$collector_name
fi
if [  -f "$path"plugins/dc/dc-nqa-conf.json"" ]
then
    rm -rf $path"plugins/dc/dc-nqa-conf.json"
fi

wget  --no-check-certificate $collector_path
if [  $? != 0 ]
then
echo  "wget "$collector_path" is wrong."
exit 1
fi
wget  --no-check-certificate $nqa_conf_path
if [  $? != 0 ]
then
echo  "wget "$nqa_conf_path" is wrong."
exit 1
fi


chmod 755 $collector_name


touch $USER_CONF_NAME
echo -e "[\n $resource_conf\n ]"  > $USER_CONF_NAME
mv $USER_CONF_NAME $path"plugins/dc/"$USER_CONF_NAME


conf_has_path=`cat $path"plugins/conf.json"  | grep -E "path"`
if [  $? != 0 ]
then
  touch conf.json
  echo -e "{\n \"plugins\": [\n{\n\"path\": \"$path"plugins/dc/"$collector_name\", \n\"crontime\": 60, \n\"type\": \"Custom Monitor\",\n \"max_timeout_proc_num\": 5\n}\n]\n}" > conf.json
  mv conf.json  $path"plugins/conf.json"
else
  conf_has_this_path=`cat $path"plugins/conf.json"  | grep -E "$path"plugins/dc/"$collector_name"`
  if [  $? != 0 ]
  then
    sed -i -e '/}/s/$/,/'  -e '$s/,/ /' -e  "/]/i{\n\"path\": \"\\$path"plugins/dc/"\\$collector_name\",\n\"crontime\":60,\n\"type\":\"Custom Monitor\",\n\"max_timeout_proc_num\":5\n}"  $path'plugins/conf.json'
  fi
fi


cd  $path'log/'
mkdir -p dc


conf_ces_path=`cat $path'bin/conf_ces.json'  | grep -E "EnablePlugin"`
if [  $? != 0 ]
then
    sed  -i -e  '/Endpoint/s/$/,/'  -e '/Endpoint/a\"EnablePlugin\": true'  $path'bin/conf_ces.json'
else
    sed -i 's/"false"/"true"/' $path'bin/conf_ces.json'
fi


$path/telescoped restart


rm -rf /usr/local/dc-installer.sh
rm -rf /usr/local/new.txt

echo "ok, "$collector_name" install success!"