#!/bin/sh
# Merged from luci-app-taskplan — quickactions edition

# Use bash if available, else fall back to busybox sh
if command -v bash >/dev/null 2>&1; then
    SHELL_BIN=/bin/bash
else
    SHELL_BIN=/bin/sh
fi

# Author: sirpdboy (original) / quickactions merge

TMP_T=/var/quickactions-taskplan/taskplan.tmp
LOG=/etc/quickactions-taskplan/taskplan.log
CUSTOM_DIR=/etc/quickactions-taskplan

log(){
    echo "$(date +'%Y-%m-%d %H:%M:%S') $*" >> $LOG
}

limit_log() {
    local logf=$1
    [ ! -f "$logf" ] && return
    local sc=100
    [ -n "$2" ] && sc=$2
    local count=$(grep -c "" $logf 2>/dev/null || echo 0)
    if [ $count -gt $sc ]; then
        let count=count-$sc
        sed -i "1,$count d" $logf 2>/dev/null
    fi
}

remwan3() {
    while true; do
        status=$(/usr/sbin/mwan3 status 2>/dev/null | grep -i error)
        if [ -z "$status" ]; then
            log "Mwan3 is OK"
            exit 0
        else
            log "mwan3 restarting ..."
            /usr/sbin/mwan3 restart
        fi
        sleep 60
    done
}

disreconnwan() {
    i=0
    PingA=114.114.114.114
    PingB=223.5.5.5
    PingErr=1

    while [ $i -lt 5 ]; do
        if ping -c 1 -W 3 $PingA >/dev/null 2>&1; then
            PingErr=0
            break
        elif ping -c 1 -W 3 $PingB >/dev/null 2>&1; then
            PingErr=0
            break
        else
            i=$((i + 1))
            sleep 10
        fi
    done
}

trigger() {
    case "$1" in
        reboot) reboot ;;
        poweroff) poweroff ;;
        network) /etc/init.d/network restart ;;
        clearmem) sync && echo 3 > /proc/sys/vm/drop_caches ;;
        sysfree)
            cd /overlay
            rm -rf $(ls | egrep -v '(upper|.fs_state)') 2>/dev/null
            cd upper 2>/dev/null
            rm -rf $(ls | egrep -v '(etc)') 2>/dev/null
            cd etc 2>/dev/null
            rm -rf $(ls | egrep -v '(config|ssr|bench.log|shadow)') 2>/dev/null
            rm -rf /var/luci-modulecache 2>/dev/null
            rm -rf /var/luci-indexcache 2>/dev/null
            rm -rf /tmp/cache/* 2>/dev/null
            reboot ;;
        restartsamba)
            /etc/init.d/ksmdb restart 2>/dev/null
            /etc/init.d/samba restart 2>/dev/null
            /etc/init.d/samba4 restart 2>/dev/null
            log "Samba restarted" ;;
        restartlan) ifdown lan && ifup lan; log "restart lan succeeded!" ;;
        restartwan) ifup wan; log "restart wan succeeded!" ;;
        closewan) ifdown wan ;;
        disreconn)
            disreconnwan
            if [ $PingErr = 1 ]; then ifup wan; log "Redial succeeded!"; fi ;;
        disrereboot)
            disreconnwan
            if [ $PingErr = 1 ]; then log "Reboot succeeded!"; reboot; fi ;;
        restartmwan3) [ -x /usr/sbin/mwan3 ] && remwan3 ;;
        customscript)
            if [ -s $CUSTOM_DIR/taskplancustomscript ]; then
                $SHELL_BIN $CUSTOM_DIR/taskplancustomscript
                log "CustomScript succeeded!"
            else log "CustomScript run Error!"; fi ;;
        customscript2)
            if [ -s $CUSTOM_DIR/taskplancustomscript2 ]; then
                $SHELL_BIN $CUSTOM_DIR/taskplancustomscript2
                log "CustomScript2 succeeded!"
            else log "CustomScript2 run Error!"; fi ;;
        upwifi)
            ifconfig wlan0 up 2>/dev/null
            uci set wireless.radio0.disabled=0 2>/dev/null
            uci set wireless.radio1.disabled=0 2>/dev/null
            uci commit wireless 2>/dev/null
            wifi up ;;
        downwifi)
            ifconfig wlan0 down 2>/dev/null
            uci set wireless.radio0.disabled=1 2>/dev/null
            uci set wireless.radio1.disabled=1 2>/dev/null
            uci commit wireless 2>/dev/null
            wifi down ;;
    esac
}

limit_log $LOG 500
PingErr=0
log "Scheduled operation $2: $1" && trigger $1
