#!/bin/bash

PARABLU_COMPONENT_CONFIG_SCRIPT_PATH=../../config/parablu_component_config.sh

echo "WHAT KIND OF SSH CONNECTIVITY IS THIS"
tput clear
        tput cup 7 15
        echo "WHAT KIND OF SSH CONNECTIVITY IS THIS:"

        tput cup 10 20
        echo "1.  KEY File Based"
        tput cup 11 20
        echo "2.  Normal SSH"
        tput cup 13 20
        echo "Enter your choice number:"
        tput cup 13 50
        read ssh_type

if [ $ssh_type == 1 ]; then
	read -p "Enter SSH key file name with complete path: " ssh_keyfile_path
        read -p "Enter SSH USER: " sshglobal_user

        sed -i "s|SSH_KEY_FILE.*|SSH_KEY_FILE='"$ssh_keyfile_path"' |" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH
        sed -i "s|SSH_USERNAME=.*|SSH_USERNAME=\"$sshglobal_user\" |" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH

        sed -i "/ssh-agent/d" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH
        sed -i "/ssh-add/d" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH
        sed -i "/KEY_BASED_SSH=/d" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH
        echo "KEY_BASED_SSH=\"true\"" >> $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH

elif [ $ssh_type  == 2 ]; then

	sudo apt-get install sshpass
	#ssh_global_user="parablu"
	#ALLIPS="172.104.22.451"
	SSH_PORT=22
	#SSH_PASSWORD="P@rablu123"
	#SUDO_BASH_PASSWORD="P@rablu123"
	cat /dev/zero | ssh-keygen -q -N "" > /dev/null
	#mkdir -p /home/$ssh_global_user/.ssh
	SCRIPT_DIR=`pwd`
	sleep 1s
	tput clear
	echo "PERFORMING SSH CONNECTIVITY..............."
	sleep 2s
	read -p "Enter the SSH User [ Should be same across all Machines ]:" ssh_global_user
	read -p "Enter the IP's of Machines [If multiple give a space as separator ]:" ALLIPS

	sleep 2s
	sed -i "s|SSH_USERNAME=.*|SSH_USERNAME=\"$ssh_global_user\" |" $PARABLU_COMPONENT_CONFIG_SCRIPT_PATH

	if [ "$ssh_global_user" != "root" ]; then
		mkdir -p /home/$ssh_global_user/.ssh
	fi

	for eachip in ${ALLIPS[@]}
	do
		read -p "Enter the SSH Password for [ $eachip ] with User [ $ssh_global_user ]:" SSH_PASSWORD
		sudo sshpass -p $SSH_PASSWORD ssh-copy-id -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no $ssh_global_user@$eachip
		sleep 3s
	
		read -p "Enter the Sudo Bash Password for User [ $ssh_global_user ] in [ $eachip ]:" SUDO_BASH_PASSWORD
		ssh -t $ssh_global_user@$eachip  "cd $SCRIPT_DIR && echo $SUDO_BASH_PASSWORD | sudo -S bash -x slave_ssh_config.sh $ssh_global_user"

	done
else
       echo "invalid input"
       exit
fi

