#!/bin/bash
target_ip=$1
IP_host_first="192.168.4."
config_files=/home/config_files
script_path=/home/qa-deploy-utils/qa_shell_script
pwd_path="/home/qa-deploy-utils/qa_shell_script"
config_server="$pwd_path/config_server"
project_path=`cat $config_server/get_env_ip.sh`


if [[ $target_ip == "ALL" ]]; then
	for ip_last_num in  $project_path
    do
        expect -c "
	        set timeout 10
	        spawn ssh root@${IP_host_first}${ip_last_num}
	        expect \"]#\"
	        send \"umount -l $script_path\\ \r\"
		    set timeout -1
		    expect \"]#\"
		    send \"rm -rf $script_path\r\"
		    set timeout -1
		    expect \"]#\"
	        send \"mkdir -p $script_path\r\"
	        set timeout -1
		    expect \"]#\"
		    send \"mount -t nfs 192.168.4.3:$script_path $script_path\r\"
		    set timeout -1
		    expect \"]#\"
		    send \"umount -l $config_files\\ \r\"
		    set timeout -1
		    expect \"]#\"
		    send \"rm -rf $config_files\r\"
		    set timeout -1
		    expect \"]#\"
		    send \"mkdir $config_files\r\"
		    set timeout -1
		    expect \"]#\"
		    send \"mount -t nfs 192.168.4.3:/home/sub_config_file_dont_rm/${IP_host_first}${ip_last_num}/ $config_files\r\"
		    set timeout -1
		    expect \"]#\"
		    send \"exit\r\"
	     expect eof;"
    done
else
	expect -c "
	    set timeout 10
	    spawn ssh root@${IP_host_first}${target_ip}
	    expect \"]#\"
	    send \"umount -l /home/qa-deploy-utils/qa_shell_script\r\"
	    expect \"]#\"
	    send \"mkdir -p /home/qa-deploy-utils/qa_shell_script\r\"
	    set timeout -1
		expect \"]#\"
		send \"mount -t nfs 192.168.4.3:/home/qa-deploy-utils/qa_shell_script /home/qa-deploy-utils/qa_shell_script\r\"
		set timeout -1
		expect \"]#\"
		send \"umount -l $config_files\r\"
		set timeout -1
		expect \"]#\"
		send \"rm -rf $config_files\r\"
		set timeout -1
		expect \"]#\"
		send \"mkdir $config_files\r\"
		set timeout -1
		expect \"]#\"
		send \"mount -t nfs 192.168.4.3:/home/sub_config_file_dont_rm/${IP_host_first}${target_ip}/ $config_files\r\"
		set timeout -1
		expect \"]#\"
		send \"exit\r\"
	expect eof;"
fi
