#!/bin/sh

# /* ******** netUstad: Network Ustadi (Network Master) *********/
# /* This software is Copyright (C) 2004 by Ozkan KIRIK.        */
# /* Permission is granted to redistribute and modify this      */
# /* software under the terms of the GNU General Public License */
# /* available at http://www.gnu.org/                           */
# /**************************************************************/

cd $work_path

confdir=/usr/local/etc/netustad
nat_enable=`${cat_cmd} /etc/rc.conf | ${grep_cmd} ^natd_enable | ${tail_cmd} -n1 | ${sed_cmd} s/natd_enable=//`

#==================
#== Send headers ==
#==================
./header

#================
#== Print Data ==
#================
echo '<script language="JavaScript" type="text/javascript">'
echo "function nat_del(dest)"
echo "{ if (confirm('`$gettext_cmd "NAT"`: '+dest+' `$gettext_cmd "will be deleted. Is it Okay?"`')) window.location='nat_del?'+dest+'"'$'$server_sid"' }"
echo "function static_add() {"
echo " publichost=document.getElementById('static_ip').value;"
echo " privatehost=document.getElementById('local_ip').value;"
echo " if (publichost!='' && privatehost!='') {"
echo " 		window.location='nat_add?static&publichost='+publichost+'&privatehost='+privatehost+'$"$server_sid"';"
echo " } else {"
echo "	alert('`$gettext_cmd "Please Fill All Fields"`');"
echo " } }"
echo "function vserv_add() {"
echo " proto=document.getElementById('proto').value;"
echo " publichost=document.getElementById('publichost').value;"
echo " publicport=document.getElementById('publicport').value;"
echo " privatehost=document.getElementById('privatehost').value;"
echo " privateport=document.getElementById('privateport').value;"
echo " if (proto!='' && publichost!='' && privatehost!='' && publicport!='' && privateport!='') {"
echo " 		window.location='nat_add?vserver&proto='+proto+'&publichost='+publichost+'&publicport='+publicport+'&privatehost='+privatehost+'&privateport='+privateport+'$"$server_sid"';"
echo " } else {"
echo "	alert('`$gettext_cmd "Please Fill All Fields"`');"
echo " } }"
echo "</script>"
echo " "
case ${nat_enable} in \"[Yy][Ee][Ss]\")
	nat_flags=`${cat_cmd} /etc/rc.conf | ${grep_cmd} natd_flags | ${tail_cmd} -n1 | ${sed_cmd} s/natd_flags=//`
	$fw_cmd add 65534 divert 1234 esp from me to me > /dev/null
	if test $? -eq 0; then
		divert_enable=1
		$fw_cmd del 65534
	else 
		divert_enable=0
	fi
	if test ${divert_enable} -eq 1; then
#		echo "IPDIVERT Mevcut, natd'ye yonlendirme yapilabilir.<br>"
		${cat_cmd} ${confdir}/netustad.natd | ${awk_cmd} -f nat-parse
	else
		echo "Cekirdek'te IPDIVERT yok. Nat Calismaz."
#		exit 1
	fi
	;;
*)
	echo "/etc/rc.conf dosyasinda natd aktif degil.<br>"
	echo "Lutfen /etc/rc.conf dosyasinda natd_enable=\"YES\" tanimini yapin"
#	exit 1
	;;
esac


#=================
#== Send footer ==
#=================
./footer
