#!/bin/sh /etc/rc.common
# copyright by sirpdboy
# Converted for OpenWrt 25.10

START=99
STOP=10
TMP=/etc/taskplan
LOG=$TMP/taskplan.log
TT=/etc/taskplan/taskplanrun
CR=/etc/crontabs/root

# 确保目录存在
[ ! -d $TMP ] && mkdir -p $TMP 2>/dev/null
[ ! -s $LOG ] && echo '' > $LOG

run_taskplan()
{
    [ -n "$(uci -q get taskplan.@global[0].customscript)" ] && \
        uci -q get taskplan.@global[0].customscript > /etc/taskplan/taskplancustomscript && \
        sed -i 's/\r$//' /etc/taskplan/taskplancustomscript
    
    [ -n "$(uci -q get taskplan.@global[0].customscript2)" ] && \
        uci -q get taskplan.@global[0].customscript2 > /etc/taskplan/taskplancustomscript2 && \
        sed -i 's/\r$//' /etc/taskplan/taskplancustomscript2
    
    ssum=$(uci -q show taskplan | grep -c "stime\[[0-9]\]" || echo 0)
    lsum=$(uci -q show taskplan | grep -c "ltime\[[0-9]\]" || echo 0)
    
    # 清空crontab中的taskplan条目
    sed -i '/taskplanhandler/d' $CR >/dev/null 2>&1
    
    # 处理定时任务
    for i in $(seq 0 $((ssum-1)) 2>/dev/null); do
        enable=$(uci -q get taskplan.@stime[$i].enable)
        if [ "x$enable" = "x1" ]; then
            month=$(uci -q get taskplan.@stime[$i].month || echo "*")
            stype=$(uci -q get taskplan.@stime[$i].stype)
            week=$(uci -q get taskplan.@stime[$i].week || echo "*")
            minute=$(uci -q get taskplan.@stime[$i].minute || echo "0")
            hour=$(uci -q get taskplan.@stime[$i].hour || echo "*")
            
            # 格式化时间
            [ "$hour" = "0" ] && hour="0"
            [ "$minute" = "0" ] && minute="0"
            
            case "$stype" in
                1) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler reboot Scheduled_task" ;;
                2) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler poweroff Scheduled_task" ;;
                3) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler network Scheduled_task" ;;
                4) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartsamba Scheduled_task" ;;
                5) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartwan Scheduled_task" ;;
                6) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler closewan Scheduled_task" ;;
                7) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler clearmem Scheduled_task" ;;
                8) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler sysfree Scheduled_task" ;;
                9) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler disreconn Scheduled_task" ;;
                10) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler disrereboot Scheduled_task" ;;
                11) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartmwan3 Scheduled_task" ;;
                12) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler customscript Scheduled_task" ;;
                13) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler upwifi Scheduled_task" ;;
                14) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler downwifi Scheduled_task" ;;
                15) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler customscript2 Scheduled_task" ;;
                16) cmd="$minute $hour * $month $week /usr/bin/taskplanhandler restartlan Scheduled_task" ;;
            esac
            echo "$cmd" >> $CR
        fi
    done

    # 创建启动任务脚本
    echo "#!/bin/sh" > $TT
    chmod +x $TT
    
    for i in $(seq 0 $((lsum-1)) 2>/dev/null); do
        enable=$(uci -q get taskplan.@ltime[$i].enable)
        if [ "x$enable" = "x1" ]; then
            stype=$(uci -q get taskplan.@ltime[$i].stype)
            delay=$(uci -q get taskplan.@ltime[$i].delay || echo 10)
            
            case "$stype" in
                1) echo "(sleep $delay && /usr/bin/taskplanhandler reboot Startup_task) &" >>$TT ;;
                2) echo "(sleep $delay && /usr/bin/taskplanhandler poweroff Startup_task) &" >>$TT ;;
                3) echo "(sleep $delay && /usr/bin/taskplanhandler network Startup_task) &" >>$TT ;;
                4) echo "(sleep $delay && /usr/bin/taskplanhandler restartsamba Startup_task) &" >>$TT ;;
                5) echo "(sleep $delay && /usr/bin/taskplanhandler restartwan Startup_task) &" >>$TT ;;
                6) echo "(sleep $delay && /usr/bin/taskplanhandler closewan Startup_task) &" >>$TT ;;
                7) echo "(sleep $delay && /usr/bin/taskplanhandler clearmem Startup_task) &" >>$TT ;;
                8) echo "(sleep $delay && /usr/bin/taskplanhandler sysfree Startup_task) &" >>$TT ;;
                9) echo "(sleep $delay && /usr/bin/taskplanhandler disreconn Startup_task) &" >>$TT ;;
                10) echo "(sleep $delay && /usr/bin/taskplanhandler disrereboot Startup_task) &" >>$TT ;;
                11) echo "(sleep $delay && /usr/bin/taskplanhandler restartmwan3 Startup_task) &" >>$TT ;;
                12) echo "(sleep $delay && /usr/bin/taskplanhandler customscript Startup_task) &" >>$TT ;;
                13) echo "(sleep $delay && /usr/bin/taskplanhandler upwifi Startup_task) &" >>$TT ;;
                14) echo "(sleep $delay && /usr/bin/taskplanhandler downwifi Startup_task) &" >>$TT ;;
                15) echo "(sleep $delay && /usr/bin/taskplanhandler customscript2 Startup_task) &" >>$TT ;;
                16) echo  "(sleep $delay && /usr/bin/taskplanhandler restartlan Startup_task) &" >>$TT ;;
            esac
        fi
    done

    echo 'exit 0' >> $TT
}

start()
{
    del_cru
    run_taskplan
    /etc/init.d/cron reload
    echo "$(date): taskplan started" >> $LOG
}

boot()
{
    # 启动时执行
    del_cru
    run_taskplan
    /etc/init.d/cron reload

    if [ -f $TT ] && [ -s $TT ]; then
        echo "$(date +'%Y-%m-%d %H:%M:%S'): Executing taskplanrun at boot" >> $LOG
        bash $TT >> $LOG 2>&1 &
    else
        echo "$(date +'%Y-%m-%d %H:%M:%S'): taskplanrun file not found or empty at boot" >> $LOG
    fi
}

stop()
{
    del_cru
    /etc/init.d/cron reload
}

restart()
{
    stop
    sleep 2
    start
}
del_cru()
{   
    sed -i '/taskplanhandler/d' $CR >/dev/null 2>&1 || true
    echo "#!/bin/sh" > $TT  # 清空文件但保留基本结构
    chmod +x $TT
}
