#!/bin/sh
port=$1

#ssh_port=1498 # set 1498 as the default port, 22 as the secondary port

ssh_termination_code=143
ssh_timeout_code=255
primary_ssh_port=1498
secondary_ssh_port=22
ssh_port=$primary_ssh_port


handle_timeout() {
if [ $error_code -eq $ssh_timeout_code ]
then
#echo "error code is $error_code" 

	if [ $ssh_port -eq $primary_ssh_port ]
        then
                echo `date`"error code $error_code. Retrying with port $secondary_ssh_port"
                ssh_port=$secondary_ssh_port
        else
                echo `date`"error code $error_code. Retrying with port $primary_ssh_port"
                ssh_port=$primary_ssh_port
        fi

#ssh_port=$secondary_ssh_port
echo $ssh_port > /tmp/os/ssh_port
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem
else
echo "error is $error_code"
fi
}

if test -s /tmp/os/ssh_port
then
	ssh_port=`cat /tmp/os/ssh_port`
else
	touch /tmp/os/ssh_port
	sudo chmod 755 /tmp/os/ssh_port
	echo $primary_ssh_port > /tmp/os/ssh_port
	ssh_port=`cat /tmp/os/ssh_port` # default SSH port
fi

if ! [ $ssh_port -eq $ssh_port ] 2> /dev/null || [ -z $ssh_port ] 2> /dev/null
then
        ssh_port = $primary_ssh_port
fi

## build tunnel
cloud_ip=34.210.248.103
external_ssh=$port
internal_ssh=`dc -e "$external_ssh 16000 + p"`
external_http=`dc -e "$internal_ssh 16000 + p"`
internal_http=`dc -e "$external_http 16000 + p"`
echo $external_ssh $internal_ssh $external_http $internal_http
#pkill -f "ssh -o StrictHostKeyChecking=no -R"
#pkill -f "ssh -o StrictHostKeyChecking=no -f -i"
t=`curl --connect-timeout 30 --max-time 10 -s https://mysiteconnect.com:$external_http/common/tct.html`
echo $t
if test ! -z $t
then
        if test $t = "success"
        then
                exit 0
        fi
fi
curl --connect-timeout 30 --max-time 10 -s https://phason.ca > /tmp/curlxxx
curl_return=$?
echo "network check = " $curl_return
if test $curl_return -ne 0
then
sudo service networking restart
d=`date`
echo $d " networking restart by cloud"
sleep 5
fi

pkill -f "ssh -o StrictHostKeyChecking=no"

#ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p 1498 -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem

ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem 2> /tmp/os/log.txt
error_code=$?

#if [ $error_code -eq $ssh_timeout_code ]
#then
#ssh_port=$secondary_ssh_port # use port 22 instead
#echo $ssh_port > /tmp/os/ssh_port
#ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem
#fi

if [ $error_code -eq $ssh_timeout_code ]
then
	if grep -q "closed by remote" /tmp/os/log.txt
	then
		echo "SSH connection closed by remote host. Retrying SSH command"
		ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem 2> /tmp/os/log.txt
		error_code=$?
	elif grep -q "reset by peer" /tmp/os/log.txt
	then
		echo "SSH connection reset by peer. Retrying SSH command"
                ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem 2> /tmp/os/log.txt
                error_code=$?
	fi
fi

handle_timeout

if [ $error_code -eq $ssh_termination_code ]
then
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_cert > /tmp/apache.pem # retry using the same port
error_code=$?
handle_timeout
fi

if test -e /tmp/os/log.txt
then
sudo rm /tmp/os/log.txt
fi

#ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p 1498 -i cloud_id phason@$cloud_ip sudo /root/get_ssl_key > /tmp/apache.key

ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -i cloud_id phason@$cloud_ip sudo /root/get_ssl_key > /tmp/apache.key

if test -s /tmp/apache.pem -a -s /tmp/apache.key
then
        if test -e /home/pi/SSD
        then
        sudo cp /tmp/apache.pem /etc/ssl/localcerts/apache.pem
        sudo cp /tmp/apache.key /etc/ssl/localcerts/apache.key
        else
        cp /tmp/apache.key /tmp/config/apache.key
        cp /tmp/apache.pem /tmp/config/apache.pem
        fi
        sudo /etc/init.d/apache2 restart
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=120 -p $ssh_port -o ServerAliveInterval=30 -o ServerAliveCountMax=5 -R $internal_ssh:localhost:22 -R $external_http:localhost:443 -N -f -i cloud_id phason@34.210.248.103
t=`curl --connect-timeout 30 -s https://mysiteconnect.com:$external_http/common/tct.html`
if test -z $t
then
        t="failed"
fi
curl --connect-timeout 30 --max-time 10 -s http://$cloud_ip/cgi-bin/status.cgi?CON,`date +%D_%T`,`hostname`,`cat /tmp/os/cloud_port`,$t
fi
