#!/bin/bash
#
# Sysadmin_assistance script
# Checks for an IP address on a given range
# If one does not exist a VPN connectin is started
# An ssh server is then started listening only on the IP address found. 
#

set -e

# start the ssh server 
ssh_server() {
ips=($(hostname -I))
for ip in ${ips[@]}; do
	if [[ $ip == *"192.168.106."* ]]; then
		systemctl is-active --quiet ssh || systemctl restart ssh
		echo "Please give your sysadmin the IP address of: $ip"	
	fi
done
}
# Ensure we have an office IP and start ssh server
vpn_check() {
if [[ $( hostname -I ) == *"192.168.106."* ]]; then
	ssh_server
	hold_close
	exit 0;
else
	# Connect to office VPN then	
	echo "Connecting to VPN..."
	nmcli c up collabora_sysadmin --ask
	ssh_server
	hold_close
	echo "Closing VPN connection..."
	nmcli c down collabora_sysadmin
	exit 0;
fi
}
# Wait for the sysadmin to do their work then close the connections
hold_close () {
	echo "----------------------"
	read -p "Press the enter key when the remote assistance is complete."
	#systemctl stop ssh.socket
}
# run the main function
vpn_check
