#!/bin/sh /etc/rc.common

START=90
STOP=90

SPEED_LIMIT_SWITCH=1
CURRENT_DATE=$(date +"%Y-%m-%d")
CHAP_SECRETS=/etc/ppp/chap-secrets
BACKUP_FILE=/var/pppoe-user/backup
mkdir -p "${BACKUP_FILE}"

setup_user() {
    local cfg="$1"
    config_get enabled "$cfg" enabled
    config_get username "$cfg" username
    config_get servicename "$cfg" servicename
    [ -n "$servicename" ] || servicename="*"
    config_get password "$cfg" password
    config_get ipaddr "$cfg" ipaddr
    config_get qos "$1" qos
    config_get urate "$1" urate
    config_get drate "$1" drate
    config_get connect "$1" connect
    config_get expires "$cfg" expires

    if [ "$expires" == "$CURRENT_DATE" ]; then
        uci set pppoe-user."$cfg".enabled=0
        uci commit pppoe-user
    fi

    # Do not append to the CHAP_SECRETS file if the user is not enabled
    if [ "$enabled" -eq 1 ] && [ "$expires" != "$CURRENT_DATE" ]; then
        echo "$username $servicename $password $ipaddr" >> "$CHAP_SECRETS"
    fi
}

daily_check() {
    (crontab -u root -l ; echo "59 6 * * * /etc/init.d/pppoe-user start") | sort - | uniq - | crontab -u root -
    (crontab -u root -l ; echo "59 23 * * * cp -f /etc/config/pppoe-user /var/pppoe-user/backup/\$pppoe-user.bak") | sort - | uniq - | crontab -u root -
}

enable_speed_limit() {
	nft add table inet pppoe
	nft add chain inet pppoe upload { type filter hook prerouting priority filter\; policy accept\; }
	nft add chain inet pppoe download { type filter hook postrouting priority filter\; policy accept\; }
	##nft add chain inet pppoe forward { type filter hook postrouting priority filter\; policy accept\; }
	uci set firewall.@zone[0].device='ppp+'
	uci commit firewall
}

disable_speed_limit() {
	nft delete table inet pppoe
	uci del firewall.@zone[0].device
	uci commit firewall
}

start() {
    cp -rf "$CHAP_SECRETS" /etc/ppp/lasttime-chap-secrets
    > "$CHAP_SECRETS"
    daily_check
    config_load pppoe-user
    config_foreach setup_user user
    chmod 600 "$CHAP_SECRETS"
    
    if [ "$SPEED_LIMIT_SWITCH" = "1" ]; then
        enable_speed_limit
    else
        disable_speed_limit
    fi
}

stop() {
    kill -q `pidof pppoe-user | sed "s/$$//g"`
}
