#!/bin/bash
set -e

if test -f /opt/digitalwatchdog/mediaserver/var/port_forwarding.json; then
sed -i 's!\}\]!\},!g' /opt/digitalwatchdog/mediaserver/var/port_forwarding.json
else echo -e "[" >> /opt/digitalwatchdog/mediaserver/var/port_forwarding.json
fi

while true ; do
	echo -n "Enter remote access service name (SSH, RDP, VNC, GoToMeeting, TeamViewer): "
	read raname
	
	echo -n "Enter remote access service port (22, 3389, 5900, 443, 5938): "
	read raport
	
	while ! [[ "$raport" =~ ^[0-9]+$ ]]; do
		echo -n "Only enter numbers: "
		read raport
	done
	
	echo -n "Enter server local account: "
	read ralogin
	
	echo -n "Enter server local password: "
	read rapassword
	
	echo -e "{
\"name\" : \""$raname"\",
\"port\" : "$raport",
\"login\" : \""$ralogin"\",
\"password\" : \""$rapassword"\"" >> /opt/digitalwatchdog/mediaserver/var/port_forwarding.json

	echo -n "Add another remote access? Y/N: "
	read yn
	
	while [[ -z "$yn" ]]; do
		echo -n "Enter Y or N only: "
		read yn
	done
	
case $yn in
[Yy] ) echo -e "}," >> /opt/digitalwatchdog/mediaserver/var/port_forwarding.json;;
[Nn] ) echo -e "}]" >> /opt/digitalwatchdog/mediaserver/var/port_forwarding.json;
break
esac

done

sudo systemctl restart digitalwatchdog-mediaserver.service
