#!/usr/bin/bash

. /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=syslog
debug=$(echo $@ | grep DEBUG && echo 1)

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 
}


for CHRON in ntpd chronyd ;do
    ASK=$(which $CHRON 2>&1)
    ret=$?
    if [ "$ret" != "1" ];then
	NTP="$(echo $ASK | sed -e 's|.*bin/||' -e 's|d$||')"
	if [ "$NTP" = "ntp" ];then
	    DAEMON=ntpd
	elif [ "$NTP" = "chrony" ];then
	    DAEMON=chronyd
	fi
        ON=$(systemctl is-enabled $DAEMON)
        if [ "$ON" = "enabled" ];then
	    CHRON=$NTP
	fi
	[ -n "$NTP" ] && break
    fi
done

if [ ! -n "$NTP" ];then
    NTP=systemd-timesyncd
fi

if [ ! -n "$DAEMON" ];then
    DAEMON=$NTP
fi

if [ -n "$NTP" ] ;then
    ON=$(systemctl is-enabled $DAEMON)
    echo $ON
    STAT=$(systemctl is-active $DAEMON)
    echo $STAT
    if [ "$ON" != "enabled" ];then
	echo "Service enabling.."
	systemctl -q enable $DAEMON.service
    fi
    dbg "======> DateTime Starter ( Daemon is: [$DAEMON] Service is: [$ON] Status is: [$STAT] )"
fi

if [ ! -f /ignorentpd ];then
 if [ -e /sys/fs/cgroup/systemd ]; then
        case "$2" in
                up)
                    if [ "$STAT" = "active" ];then
                	 systemctl -q restart $DAEMON.service
                    else
                	 systemctl -q start $DAEMON.service
                    fi      
                ;;
                down)
                              systemctl -q stop $DAEMON.service
                ;;
        esac
 else
        if [ -e "/etc/rc.d/ntpd" ]; then
                case "$2" in
                        up)
                                rc.d start $DAEMON
                        ;;
                        down)
                                rc.d stop $DAEMON
                        ;;
                esac
        fi

 fi
fi