#!/bin/sh

# Copyright (C) 2006 OpenWrt.org
# Copyright 2022-2025 sirpdboy <herboy2008@gmail.com>
NAME=eqosplus
IDLIST="/var/$NAME.idlist"
TMPID="/var/$NAME.tmpid"

idlist=`uci show $NAME | grep "enable='1'" | grep "device" | grep -oE '\[.*?\]' | grep -o '[0-9]' `

check_list() {
	i=$1
	checki='0'
	start_time=$(uci -q get $NAME.@device[$i].timestart 2>/dev/null)
	end_time=$(uci -q get $NAME.@device[$i].timeend 2>/dev/null)
	wweek=`uci -q get $NAME.@device[$i].week `
	current_time=$(date +%H:%M)
	current_weekday=$(date +%u)
	for ww in `echo $wweek | sed 's/,/ /g' `; do 
		if [ $current_weekday = $ww ] || [ 'x0' = x$ww ] ;then 
		       [ "$start_time" = "$end_time" ] && checki='1' || { 
		       	   if  [ "$start_time" \< "$end_time" ]; then
			       if  [ "$current_time" \> "$start_time" ]  &&  [ "$current_time" \< "$end_time" ] ; then
		       	           checki='1'
		       	       fi
		       	   else 
			       if [ "$start_time" \> "$end_time" ] ; then
			          if [ "$current_time" \< "$start_time" ] && [ "$current_time" \> "$end_time" ] ; then
		       	              checki='1'
		       	          fi
			       fi
			   fi
		       	}
		fi
	done
	return
}

idlistusr(){
   checki='0'
   [ -s $IDLIST ] || touch $IDLIST
   for list in $idlist ;do
	check_list $list
	if [ $checki == '1' ] ; then
	     [ `cat $IDLIST  2>/dev/null | grep "!${list}!" | wc -l ` -gt 0 ] || { 
	         eqosplus add $list
	         echo "!${list}!" >> $IDLIST ;  cat $IDLIST | sort | uniq  > $TMPID ;cat $TMPID >$IDLIST ;rm -rf $TMPID
	     }
        else
	     [ `cat $IDLIST  2>/dev/null | grep "!${list}!" | wc -l ` -gt 0 ] && {
	         eqosplus del $list
		 sed -i "/!$list!/d" $IDLIST >/dev/null 2>&1
	     }
	fi
   done
}


while :;do
	sleep 30
	idlistusr
	sleep 30
done