#!/usr/bin/sh

. /etc/blackPanther-default-apps.conf
####### 
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2019********
##   blackPanther DNSFix Configuration
#####
#debug=$(echo $@ | grep DEBUG && echo 1)
debug=syslog

export TEXTDOMAIN="default-apps"
export TEXTDOMAINDIR="/usr/share/locale"

if [ "$BP_DEFAULT_APPS" = "yes" ]; then

    if [ ! -n "$(echo "$PATH" | grep "/usr/lib/blackPanther-default-apps/bin" 2>/dev/null)" ]&&[ -d /usr/lib/blackPanther-default-apps/ ]; then
        export PATH="/usr/lib/blackPanther-default-apps/bin:$PATH"
    fi
    if [ ! -n "$(echo "$PATH" | grep "/usr/lib/blackPanther-default-serverapps/bin" 2>/dev/null)" ]&&[ -d /usr/lib/blackPanther-default-serverapps/ ]; then
        export PATH="/usr/lib/blackPanther-default-serverapps/bin:$PATH"
    fi
fi


dbg ()
{
    if [ "$debug" = "syslog" ];then
	enter=""
       	logger ${@}
    elif [ -n "$debug" ]; then
	enter="\n"
	quiet=-q
        space='echo'
       	$SETCOLOR_FAIL
       	echo -en "DBG: ${@} $enter"
       	$SETCOLOR_NORMAL
    fi 
}

inetctl() {

dbg "======> iNetCtl start..."

#if [ ! -n "`ping -c 1 google.com 2>/dev/null | grep 'PING google.com'`" ]; then 
[ ! -n "$(which default-netchk 2>/dev/null)" ]&& . /etc/blackPanther-default-apps.conf
if [ "$(default-netchk)" != "OK" ]; then 
    if [ "$1" = "2" ];then
	dbg "${YEL}DNSERROR${DEF}"
    fi
    
    iNET=DNSERROR
    
    if [ ! -n "`ping -c 1 8.8.8.8 | grep 'icmp_seq'`" ]; then 

	dbg "IP FAIL TOO, Exiting.."
	iNET=FAIL
	exit 1

     else
        
	dbg "IP RESPONSE OK! Requires Fix DNS Now..."
	# try fix DNS servers 
	if [ "$BP_AUTODNSFIX" = "yes" ]&&[ "$UID" = 0 ];then
	    mv -f /etc/resolv.conf  /etc/resolv.conf.DNSFIX
	    echo " ## Created blackPanther iDNS fixer on $(date) #######" > /etc/resolv.conf
	    echo "nameserver 8.8.8.8" >> /etc/resolv.conf
	    echo "nameserver 8.8.4.4" >> /etc/resolv.conf
	    dbg "======> iNetCtl DNS fixed!"
	fi
	
	if [ "$1" = "2" ];then
	    echo -e OK
	    DNS=FAIL
	    dbg "======> iNetCtl DNS FAIL!"
	    exit
	fi

	inetctl 2
    fi
fi

}


if [ "$2" = "up" ]; then
    export LC_ALL=C
    inetctl
fi
