#!/bin/sh

. /etc/image_features

function printissue(){
clear
echo ""
netcount=$(/sbin/ifconfig | grep -o "inet")
if [[ $netcount > 1 ]]
then
# bold
  echo -e "\033[1m"
# green
  echo -e "\033[31m"
PRODUCTNAME=$(/bin/getproductname)
if [ ! -z $PRODUCTNAME ]
then
  #PRODUCTVERSION=$(cat /etc/version.txt | grep "Version" | awk '{print $3}')
  PRODUCTVERSION=$(cat /etc/version.txt | grep "Version")
  # print the product name
  # figlet will search first for fonts (the ".flf" files) in /usr/local/share/figlet
  # if no font specified the (standard.flf is used
  /bin/figlet $PRODUCTNAME
  echo  $PRODUCTVERSION

fi
# back to normal
  echo -e "\033[0m"
# bold
  echo -e "\033[1m"
if [ "$FEATURE_CLOUD_IPPBX_OWN_SERVICE" == "1" ] ; then
  echo "Please use a web browser" 
else
  echo "Please connect to Network Ethernet Port #1 and use a web browser"
fi
  echo "to navigate to" 
  #/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{ print $2 }' | awk -F: '{ print "http://"$2"/" }' 

  ISETH0=$(/sbin/ifconfig | grep eth0)
  if [ "$ISETH0" != "" ] ; then
    ip addr show eth0 | grep inet | grep -v inet6 | grep -v "eth0:" | awk '{print $2}'| awk -F/ '{ print "https://"$1"/" }'
  else
    ip addr show lan0 | grep inet | grep -v inet6 | grep -v "lan0:" | awk '{print $2}'| awk -F/ '{ print "https://"$1"/" }'
  fi
  echo "to access the unit"
  else
	echo "This appliance does not have networking configured." 
  fi
  #
  #Input a link to an external website to reference
  #
  echo "" 
  echo "For more information regarding this device, please visit" 
  echo "www.epygi.com" 
  echo "" 
}

# back to normal
echo -e "\033[0m"

pwd='$1$TuwIxhSD$8H9pZ69ho9PWTTlKw/5RA/'

# determine the version of kernel
K_VER_EXP=3.10.0-514.26.2.el7.centos.plus.i686
K_VER_CURRENT=$(uname -r)
min=$(echo -e $K_VER_EXP"\n"$K_VER_CURRENT|sort -V|head -n 1)

while true
  do
	printissue
	read -s
	result=$(perl -e 'print crypt($ARGV[0], $ARGV[1])' $REPLY $pwd 2> /dev/null)
	if [ "$REPLY" = "factory" ]; then
		read -p "Do you want to Factory Reset now?(y/n)" -n 1 -r
		echo
		if [[ $REPLY =~ ^[Yy]$ ]]
			then
			echo "factory reset ..."
			touch /etc/fiad-conf/restore_default.conf
			/bin/reboot
		fi
	fi
	if [ "$REPLY" = "firewall" ]; then
		read -p "Do you want to reset firewall settings now?(y/n)" -n 1 -r
	  echo
	  if [[ $REPLY =~ ^[Yy]$ ]]
		then
		echo "reset firewall settings ..."
		cp /etc/default-conf/firewall/fw.cfg /etc/fiad-conf/firewall/fw.cfg
		/bin/reboot
	  fi
	fi
	if [ "$REPLY" = "network" ]; then
		  read -p "Do you want to reset network settings now?(y/n)" -n 1 -r
	  echo
	  if [[ $REPLY =~ ^[Yy]$ ]]
		then
			echo "reset network settings ..."
			cp /etc/default-conf/sysconfig/network.conf /etc/fiad-conf/sysconfig/network.conf
			cp /etc/default-conf/sysconfig/network-scripts/ifcfg-eth0_1 /etc/fiad-conf/sysconfig/network-scripts/ifcfg-eth0_1
			cp /etc/default-conf/sysconfig/network-scripts/ifcfg-lan0 /etc/fiad-conf/sysconfig/network-scripts/ifcfg-lan0
			cp /etc/default-conf/sysconfig/network-scripts/ifcfg-lan1 /etc/fiad-conf/sysconfig/network-scripts/ifcfg-lan1
			rm -f /etc/fiad-conf/vlan.cfg
			cp  /etc/default-conf/syncdadvanced.conf /etc/fiad-conf/syncdadvanced.conf
			cp  /etc/default-conf/syncd.conf /etc/fiad-conf/syncd.conf
			cp  /etc/default-conf/rsyncd.conf /etc/fiad-conf/rsyncd.conf
			/bin/reboot
		fi
	fi
	if [ "$result" = "$pwd" ]; then
	    #break from loop if kernel version is >= 3.10
	    #the gettty will be called by systemd after this (rungetty) script
	    if [ "$min" = "$K_VER_EXP" ]; then 
		break 
	    fi
	    /sbin/agetty -f /tmp/issue tty1 9600
	fi
  done

exit 0