#!/bin/bash


if [ $# -ge 1 ]; then
	OUTFILE=$1
else
	echo Usage $0 NETWORK_SETUP_OUTFILENAME[/etc/network/interfaces] DHCPD_CONFIG_FILE[/etc/default/isc-dhcpd-server]  UFW_BEFORE_SCRIPT[/etc/ufw/before.rules]
	echo "NOTE: if you like to update only UFW_BEFORE_SCRIPT, provide '/dev/null' for first two files."
	exit 1
fi

if [ $# -ge 2 ]; then
	DHCPDCONFIG_FILE=$2
else
	echo WARNING: DHCPD_CONFIG_FILE was not provided, DHCPD will not be configured.
	DHCPDCONFIG_FILE=/dev/null
fi

if [ $# -ge 3 ]; then
	UFWCONFIG_FILE=$3
else
	echo WARNING: UFW_BEFORE_SCRIPT was not provided, UFW will not be configured.
	UFWCONFIG_FILE=/dev/null
fi

#/home/ROULETTE/initial_config/etc/network/interfaces_TEST

echo "#This file was generated by OneAmazingScript.sh" > $OUTFILE
echo "#This file was generated by OneAmazingScript.sh" > $DHCPDCONFIG_FILE

echo "auto lo" >> $OUTFILE
echo "iface lo inet loopback" >> $OUTFILE
echo >>$OUTFILE

ifcount=0
array_test=()
for iface in $(ifconfig | cut -d ' ' -f1 | grep -v ":1" | tr ':' '\n' | awk NF)
do
	if [[ $iface == e* ]]; then
    		array_test+=("$iface")
		let ifcount+=1
	fi
done

firstif=${array_test[$ifcount-1]}
secondif=${array_test[0]}

if [ $ifcount -eq 1 ]; then
	#if we have only one network interface, configure to listen on it
	#Product name will determine if isc-dhcp-server will run or stop
	echo "INTERFACES=\"$firstif\"" >> $DHCPDCONFIG_FILE

	firstif="$firstif:1"

#ce imamo samo eno mrezno, ni potrebno nastavljati ip-ja 192.168.67.1, ker itak ne bomo mogli boot-ati
	echo "#Static IP interface $firstif"  >> $OUTFILE
	echo "auto $firstif" >> $OUTFILE
	echo "iface $firstif inet static"  >> $OUTFILE
	echo "   address 10.5.10.254"  >> $OUTFILE
	echo "   netmask 255.255.255.0"  >> $OUTFILE
	echo  >> $OUTFILE

else
	#ce imamo dve mrezne, menjamo vrstni red, da je na ASROCKu internet pri zvocni kartici
	TEMPIF=$firstif
	firstif=$secondif
	secondif=$TEMPIF

	#vpisemo v dhcpd interface
	echo "INTERFACES=\"$firstif\"" >> $DHCPDCONFIG_FILE

	cat $UFWCONFIG_FILE|grep "These options were added by OneAmazingScript.sh"
	if [ $? -ne 0 ]; then
		echo "#These options were added by OneAmazingScript.sh" >> $UFWCONFIG_FILE

		#ce so dve, rutamo trafic iz terminalov na internet interface, sicer to ni potrebno
		echo "-A POSTROUTING -s 10.5.10.0/24 -o $secondif -j MASQUERADE" >> $UFWCONFIG_FILE
		echo "-A POSTROUTING -s 192.168.67.0/24 -o $secondif -j MASQUERADE" >> $UFWCONFIG_FILE
		#we add also all wlan interfaces found in system
		for iface in $(ifconfig | cut -d ' ' -f1 | grep -v ":1" | tr ':' '\n' | awk NF)
		do
			if [[ $iface == wl* ]]; then
				echo "-A POSTROUTING -s 10.5.10.0/24 -o $iface -j MASQUERADE" >> $UFWCONFIG_FILE
				echo "-A POSTROUTING -s 192.168.67.0/24 -o $iface -j MASQUERADE" >> $UFWCONFIG_FILE
			fi
		done
		echo "COMMIT" >> $UFWCONFIG_FILE
	else
		echo "WARNING: "
		echo "WARNING: "
		echo "UFW configuration is already patched by OneAmazingScript. Can not be applied twice."
		echo "SOLUTION: Remove the patch manually and retry. Skipping patch for $UFWCONFIG_FILE"
		echo
	fi
#ce sta dve mrezni, nastavimo na interno mrezo 192.168.67.X in 10.5.10.X
	echo "auto $firstif:1" >> $OUTFILE
	echo "iface $firstif:1 inet static"  >> $OUTFILE
	echo "   address 10.5.10.254"  >> $OUTFILE
	echo "   netmask 255.255.255.0"  >> $OUTFILE
	echo  >> $OUTFILE

	echo "auto $firstif" >> $OUTFILE
	echo "iface $firstif inet static"  >> $OUTFILE
	echo "   address 192.168.67.1"  >> $OUTFILE
	echo "   netmask 255.255.255.0"  >> $OUTFILE
	echo  >> $OUTFILE
fi


echo "#DHCP Enabled interface $secondif"  >> $OUTFILE
echo "auto $secondif" >> $OUTFILE
echo "iface $secondif inet dhcp"  >> $OUTFILE
echo  >> $OUTFILE


sudo ip addr flush $firstif
sudo ip addr flush $secondif
sudo systemctl restart networking


#naredimo se rocno dodajanje IP-ja, ker kaksenkrat restart netwoking ne uspe.
if [ $ifcount -eq 1 ]; then
	ifconfig $firstif 10.5.10.254
else
	ifconfig $firstif 10.5.10.254
	ifconfig $firstif:1 192.168.67.1
fi
echo
echo $OUTFILE
echo "*******************************************************"
cat $OUTFILE
echo "*******************************************************"
echo
echo
echo $DHCPDCONFIG_FILE
echo "*******************************************************"
cat $DHCPDCONFIG_FILE
echo "*******************************************************"
echo
echo
echo $UFWCONFIG_FILE
echo "*******************************************************"
tail -n 5 $UFWCONFIG_FILE
echo "*******************************************************"


