#!/bin/sh

wget -P /tmp/ports ftp://ftp.epygi.com/ecQX_Hosted/ports_configuration >/dev/null
if [[ $? -ne 0 ]]; then
	wget -P /tmp/ports ftp://ftp.epygi.com/ecQX_Hosted/ports_configuration >/dev/null
	if [[ $? -ne 0 ]]; then
		logger -s "Auto Configuration Failed. Problem getting ecQX ports configuration from FTP Server"
		exit 1
	fi
fi

# Apply Hosted ecQX Configuration in case when hostname is created in /mnt/factory/factorysafe directory.
# Get default ports for this instance
. /etc/fiad-conf/sysconfig/network-scripts/ifcfg-lan0
FOURTH=$(echo $IPADDR |cut -d "." -f 4)
IPADDR=192.168.1.$FOURTH
file="/tmp/ports/ports_configuration"

IP_HEX=$(printf '%.2X%.2X%.2X%.2X\n' `echo $IPADDR | sed -e 's/\./ /g'`)
NEXT_IP_HEX=$(printf %.8X `echo $(( 0x$IP_HEX + 1 ))`)
NEXT_IP=$(printf '%d.%d.%d.%d\n' `echo $NEXT_IP_HEX | sed -r 's/(..)/0x\1 /g'`)

cat ${file} | sed -n "/"$IPADDR"_/,/"$NEXT_IP"_/p" | head -n-1 >/tmp/tempports
sed 1d /tmp/tempports  > /tmp/currentports
. /tmp/currentports
if [ "$SIPUDP" = "" ]; then
	logger -s "Auto Configuration Failed. Problem parsing ecQX ports configuration"
	rm -f "file"
	exit 1
fi

# Get Main Hostname , Current device Hostname and Redundant device Hostname
MAIN_HOSTNAME=$(cat /mnt/factory/factorysafedir/hostname)
MAIN_HOSTNAME=$(echo ${MAIN_HOSTNAME%-r*})
CURRENT_HOSTNAME=$(cat /mnt/factory/factorysafedir/hostname)

SUBSTR="-r1"
if [[ "$CURRENT_HOSTNAME" = *"$SUBSTR"* ]]; then
	REDUNDANT_HOSTNAME="$MAIN_HOSTNAME"-r2
else
	REDUNDANT_HOSTNAME="$MAIN_HOSTNAME"-r1
fi

# Get Main FQDN , Current device FQDN and Redundant device FQDN
MAIN_FQDN=$MAIN_HOSTNAME".epygicloud.com"
CURRENT_FQDN=$CURRENT_HOSTNAME".epygicloud.com"
REDUNDANT_FQDN=$REDUNDANT_HOSTNAME".epygicloud.com"

# Get Current/Redundant device router IP addresses
CURRENT_DEVICE_IP=$(ping -c1 -t1 -W0 $CURRENT_FQDN 2>&1 | tr -d '():' | awk '/^PING/{print $3}')
REDUNDANT_DEVICE_IP=$(ping -c1 -t1 -W0 $REDUNDANT_FQDN 2>&1 | tr -d '():' | awk '/^PING/{print $3}')

if [ "$REDUNDANT_DEVICE_IP" = "" ] || [ "$CURRENT_DEVICE_IP" = "" ]; then
	logger -s "Auto Configuration Failed. Problem resolving Redundant device or Current device FQDN"
	rm -f "file"
	exit 1
fi

# Replace SIP UDP/TCP ports in nat.cfg
SED_CMD=$(echo "s/self_port_range_start=5060/self_port_range_start="$SIPUDP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/self_port_range_end=5060/self_port_range_end="$SIPUDP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_start=5060/mapped_port_range_start="$SIPUDP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_end=5060/mapped_port_range_end="$SIPUDP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

# Replace SIP TLS ports in nat.cfg
SED_CMD=$(echo "s/self_port_range_start=5061/self_port_range_start="$SIPTLS"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/self_port_range_end=5061/self_port_range_end="$SIPTLS"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_start=5061/mapped_port_range_start="$SIPTLS"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_end=5061/mapped_port_range_end="$SIPTLS"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

# Replace RTP ports in nat.cfg
SED_CMD=$(echo "s/self_port_range_start=6000/self_port_range_start="$RTPMIN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/self_port_range_end=8399/self_port_range_end="$RTPMAX"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_start=6000/mapped_port_range_start="$RTPMIN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/mapped_port_range_end=8399/mapped_port_range_end="$RTPMAX"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

#Configure Mapped IP Host address in nat.cfg
SED_CMD=$(echo "s/mapped_host=/mapped_host="$CURRENT_DEVICE_IP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg

SED_CMD=$(echo "s/use_stun=1/use_stun=0/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/nat.cfg
# Insert auto_detect_host
sed '/^mapped_host.*/a auto_detect_host=0' /mnt/flashfs/configs/current/telephony/nat.cfg >/mnt/flashfs/configs/current/telephony/nattemp.cfg
mv -f /mnt/flashfs/configs/current/telephony/nattemp.cfg /mnt/flashfs/configs/current/telephony/nat.cfg

# Replace SIP UDP/TCP/TLS ports in sip.cfg
SED_CMD=$(echo "s/port=5060/port="$SIPUDP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/sip.cfg

SED_CMD=$(echo "s/port=5061/port="$SIPTLS"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/sip.cfg

# Configure sip aliases in sip.cfg
SED_CMD=$(echo "s/aliases=/aliases="$MAIN_FQDN,$CURRENT_FQDN,$REDUNDANT_FQDN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/sip.cfg

SED_CMD=$(echo "s/agent=2/agent=1/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/sip.cfg

SED_CMD=$(echo "s/stun=1/stun=0/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/sip.cfg

#insert   ignore to header.
sed '/^user.*/a ignore\tto header=0' /mnt/flashfs/configs/current/telephony/sip.cfg >/mnt/flashfs/configs/current/telephony/siptemp.cfg
mv -f /mnt/flashfs/configs/current/telephony/siptemp.cfg /mnt/flashfs/configs/current/telephony/sip.cfg

# Replace RTP ports in rtp.cfg
SED_CMD=$(echo "s/min_rtp_port=6000/min_rtp_port="$RTPMIN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/rtp.cfg

SED_CMD=$(echo "s/max_rtp_port=7799/max_rtp_port="$RTPMAX"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/telephony/rtp.cfg

# Replace webrtc ports and mapped host address in janus.cfg
SED_CMD=$(echo "s/40000/"$WEBRTCMAX"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/janus/janus.jcfg

SED_CMD=$(echo "s/20000/"$WEBRTCMIN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/janus/janus.jcfg

#insert nat_1_1_mapping
sed '/^  ice_ignore_list.*/a nat_1_1_mapping = "ABCDEF";' /mnt/flashfs/configs/current/janus/janus.jcfg >/mnt/flashfs/configs/current/janus/janustemp.jcfg
mv -f /mnt/flashfs/configs/current/janus/janustemp.jcfg /mnt/flashfs/configs/current/janus/janus.jcfg
SED_CMD=$(echo "s/ABCDEF/"$CURRENT_DEVICE_IP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/janus/janus.jcfg

# Replace OpenVPN port in server.conf
SED_CMD=$(echo "s/1194/"$OpenVPN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/openvpn/conf/server.conf

# Add RSYNC port in syncd.conf
echo "backup_mode=active">>/mnt/flashfs/configs/current/syncd.conf
echo "rsync_port=$RSYNC">>/mnt/flashfs/configs/current/syncd.conf

# Configure redundant_ip in syncd.conf file
SED_CMD=$(echo "s/redundant_ip=/redundant_ip="$REDUNDANT_FQDN"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/syncd.conf

# Configure hosts_allow in rsyncd.conf
SED_CMD=$(echo "s/allow=/allow="$REDUNDANT_DEVICE_IP"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/rsyncd.conf

# Configure network.conf
grep -v "KEEPALIVED" /mnt/flashfs/configs/current/sysconfig/network.conf >/mnt/flashfs/configs/current/sysconfig/network1.conf
grep -v "ROUTER" /mnt/flashfs/configs/current/sysconfig/network1.conf >/mnt/flashfs/configs/current/sysconfig/network.conf
rm -f /mnt/flashfs/configs/current/sysconfig/network1.conf
echo "KEEPALIVED_METHOD=true">>/mnt/flashfs/configs/current/sysconfig/network.conf
echo "START_KEEPALIVED=true">>/mnt/flashfs/configs/current/sysconfig/network.conf
echo "ROUTER_IP=$CURRENT_DEVICE_IP">>/mnt/flashfs/configs/current/sysconfig/network.conf

# Configure rc.conf
SED_CMD=$(echo "s/HOSTNAME=epygiqx/HOSTNAME="$MAIN_HOSTNAME"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/sysconfig/rc.conf

SED_CMD=$(echo "s/DOMAINNAME=epygi-config.loc/DOMAINNAME=epygicloud.com/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/sysconfig/rc.conf

# Enable Dyndns in rc.conf
SED_CMD=$(echo "s/START_DYNDNS=.*/START_DYNDNS="true"/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/sysconfig/rc.conf

# Configure firewall
SED_CMD=$(echo "s/SECURITY_LEVEL=1/SECURITY_LEVEL=3/")
/bin/sed -i $SED_CMD /mnt/flashfs/configs/current/firewall/fw.cfg

echo "section ServiceInHTTP {" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "target=ACCEPT" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "name=HTTP" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "chain=ServiceIn" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "ip=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "restip=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "descr=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "state=1" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "}" >>/mnt/flashfs/configs/current/firewall/fw.serports

echo "section ServiceAllowedALL212.34.248.234 {" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "target=ACCEPT" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "name=ALL" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "chain=ServiceAllowed" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "ip=212.34.248.234" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "restip=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "descr=Epygi TSS Team" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "state=1" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "}" >>/mnt/flashfs/configs/current/firewall/fw.serports

echo "section ServiceAllowedALL$CURRENT_DEVICE_IP {" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "target=ACCEPT" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "name=ALL" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "chain=ServiceAllowed" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "ip=$CURRENT_DEVICE_IP" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "restip=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "descr=Epygi TSS Team, Redundancy" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "state=1" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "}" >>/mnt/flashfs/configs/current/firewall/fw.serports

echo "section ServiceAllowedALL$REDUNDANT_DEVICE_IP {" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "target=ACCEPT" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "name=ALL" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "chain=ServiceAllowed" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "ip=$REDUNDANT_DEVICE_IP" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "restip=" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "descr=Epygi TSS Team, Redundancy" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "state=1" >>/mnt/flashfs/configs/current/firewall/fw.serports
echo "}" >>/mnt/flashfs/configs/current/firewall/fw.serports

# configue dyndns.conf
echo "password=3pyg1@2019" >/mnt/flashfs/configs/current/dyndns.conf
echo "login=mj.cuello@epygi.com" >>/mnt/flashfs/configs/current/dyndns.conf
echo "provider=noip" >>/mnt/flashfs/configs/current/dyndns.conf
echo "host=$MAIN_FQDN" >>/mnt/flashfs/configs/current/dyndns.conf
echo "system=dyndns" >>/mnt/flashfs/configs/current/dyndns.conf
echo "wildcard=1" >>/mnt/flashfs/configs/current/dyndns.conf
echo "file=/var/log/dyndns/" >>/mnt/flashfs/configs/current/dyndns.conf


if [ -f /etc/image_features ] ; then
	. /etc/image_features
else
	echo
	echo
	echo Fatal error: Could not find 'image_features' file. Forced FEATURE_QXSC to 0.
	echo
	FEATURE_QXSC="0"
fi

if [ "$FEATURE_QXSC" != "1" ]; then
IPPHONECOUNT=$(cat /mnt/factory/factorysafedir/ipPhoneFile)
if [ "$IPPHONECOUNT" = "" ]; then
	IPPHONECOUNT=5
fi
NEXTLINENUMBER=$(($IPPHONECOUNT+1))
# Configure ecQX feature keys
if [ ! -f /mnt/flashfs/cloudfeatures ] ; then
	echo "1,6,1" >/mnt/flashfs/cloudfeatures
	echo "2,3,$IPPHONECOUNT" >>/mnt/flashfs/cloudfeatures
	echo "2,4,5" >>/mnt/flashfs/cloudfeatures
	echo "3,22,100" >>/mnt/flashfs/cloudfeatures
	echo "3,24,100" >>/mnt/flashfs/cloudfeatures
	echo "3,26,$IPPHONECOUNT" >>/mnt/flashfs/cloudfeatures
fi
else
	echo "1,6,1" >/mnt/flashfs/cloudfeatures
fi
# Configure as a Epygi Hosted ecQX device
touch /mnt/factory/factorysafedir/ishosted

# Configure hosts
echo "127.0.0.1 $CURRENT_FQDN $CURRENT_HOSTNAME" localhost.localdomain localhost>/mnt/flashfs/configs/current/hosts
echo "::1		localhost6.localdomain6 localhost6" >>/mnt/flashfs/configs/current/hosts

# Suadmin pswd
echo -n "35f1073e6a351f6178d3be6c3ea4dfdc" >/mnt/factory/factorysafedir/sapwd

echo Done
/bin/reboot
