#!/bin/bash
DIR_CONF="/etc/CTparental"
DIR_SHARE="/usr/share/CTparental"
FILE_CONF="$DIR_CONF/CTparental.conf"
BINSYSTEMCTL=${BINSYSTEMCTL:="/usr/bin/systemctl"}
if [ -f "$DIR_CONF/CTparental.conf" ];then
    if [ $( grep -c "^LANG=" $FILE_CONF ) -ge 1 ] ;then
        export LANG=$( grep "^LANG" $FILE_CONF | cut -d "=" -f2 )
    fi
fi
DIRLOCALE=${DIRLOCALE:="/usr/share/locale"}
#chargement des locales.
set -a
source /usr/bin/gettext.sh
set +a
export TEXTDOMAINDIR="$DIRLOCALE"
export TEXTDOMAIN="ctparental"
CHEMINCTPARENTLE="$(readlink -f "$0")"

#### UID MINIMUM pour les UTILISATEURS
SED="/bin/sed -i"
SAFE_CONF="$DIR_CONF/CTsafe.conf"
FILE_GCTOFFCONF="$DIR_CONF/GCToff.conf"
FILE_HCONF="$DIR_CONF/CThours.conf"
FILE_HCOMPT="$DIR_CONF/CThourscompteur"
if [ -f "${DIR_CONF}/dist.conf" ];then 
TIMERALERT=$(grep ^TIMERALERT= "${DIR_CONF}/dist.conf" | sed -e "s/TIMERALERT=//g")
fi
TIMERALERT=${TIMERALERT:=10}
#PCUSER=$1
#HOMEPCUSER=$(getent passwd "$USER" | cut -d ':' -f6)
export UId=$( id -u $USER )
export XDG_RUNTIME_DIR=/run/user/$UId
export DISPLAY=:0.0 
export XAUTHORITY=$HOME/.Xauthority 
if [ -f $BINSYSTEMCTL ] ; then
dbus-update-activation-environment --all --systemd
fi
#/usr/bin/env > $HOME/.envnotify.txt
systemday=$(date -R | cut -d"," -f1)
hsys=$(date -R | cut -d" " -f5 | cut -d ":" -f1 | sed -e "s/^0//g")
msys=$(date -R | cut -d" " -f5 | cut -d ":" -f2 | sed -e "s/^0//g")
mtimesys=$(( hsys * 60 + msys ))
DAYSSYSTEMD=( Mon Tue Wed Thu Fri Sat Sun )

logout=0
countmin=1440
while read line
do
    if [ "$( echo "$line" | grep -E -c "^$USER=[0-6]=" )" -eq 1 ] ; then

        NumDAY=$(echo "$line" | cut -d"=" -f2)
        if [ ${DAYSSYSTEMD[$NumDAY]} = ${systemday} ] ; then
            var=$(grep "^$USER=" $FILE_HCOMPT | cut -d"=" -f2)
            varweb=$( grep "^$USER=" $FILE_HCOMPT | cut -d"=" -f3 )
            count=$(($( grep "^$USER=user=" $FILE_HCONF | cut -d "=" -f3 ) - var ))
            countweb=$(($( grep "^$USER=user=" $FILE_HCONF | cut -d "=" -f4 ) - varweb ))
            eval_gettext "Logout in \$count minutes"
            echo ""
            eval_gettext "Your remaining navigation time is \$countweb minutes"
            echo ""
        
            if [ $count -le $TIMERALERT ]; then
                countmin=$count
                logout=1
            fi
            if [ $countweb -le $TIMERALERT ]; then
                if [ "$countweb" -le 0 ];then
                    /usr/bin/notify-send -t 3000 -u critical "CTparental" "$(gettext 'Your surf time has expired!')"
                    gettext 'Your surf time has expired!'
                    echo ""
                else
                    /usr/bin/notify-send -t 3000 -u critical "CTparental" "$(eval_gettext "Your remaining navigation time is \$countweb minutes")" 
                    echo ""
                fi
            fi
            #echo "$line"
            h2=$(echo "$line" | cut -d"=" -f3 | cut -d":" -f2 | cut -d"h" -f1 | sed -e "s/^0//g")
            m2=$(echo "$line" | cut -d"=" -f3 | cut -d":" -f2 | cut -d"h" -f2 | sed -e "s/^0//g")
            h4=$(echo "$line" | cut -d"=" -f3 | cut -d":" -f4 | cut -d"h" -f1 | sed -e "s/^0//g")
            m4=$(echo "$line" | cut -d"=" -f3 | cut -d":" -f4 | cut -d"h" -f2 | sed -e "s/^0//g")
            difftime2=$(( h2 * 60 + m2 - mtimesys))
            count=$difftime2
            if [ $difftime2 -ge 0 ] ; then
                echo $(eval_gettext "Logout in \$count minutes")
                if [ $difftime2 -le $TIMERALERT ]; then
                    if [ $countmin -ge $count ];then
                        countmin=$count
                        logout=1
                    fi
                fi 
            fi 
            if [ "$(echo -n "$h4""$m4" | wc -c)" -gt 2 ]; then
                difftime4=$(( h4 * 60 + m4 - mtimesys ))
                count=$difftime4
                if [ $difftime4 -ge 0 ] ;then
                    echo $(eval_gettext "Logout in \$count minutes")
                    if [ $difftime4 -le $TIMERALERT ]; then
                        if [ $countmin -ge $count ];then
                            countmin=$count
                            logout=1
                        fi
                    fi
                fi
            fi
        fi
    fi
done < $FILE_HCONF

if [ $logout -eq 1 ]; then
    count=$countmin
    /usr/bin/notify-send -u critical "CTparental" "$(eval_gettext "Logout in \$count minutes")"
fi
