#!/bin/sh

[ -n "$DEVICE" ] || {
	echo "This script should only be run on a device."
	exit 0
}

[ -n "$ACTION" ] || {
	echo "No action specified. Use 'ifup' or 'ifdown'."
	exit 0
}

# CHeck if DEVICE contains "mesh"
if ! echo "$DEVICE" | grep -q "mesh"; then
	echo "This script is only applicable to mesh interfaces."
	exit 0
fi

# Check if interface is a WiFi mesh interface
if [ -d "/sys/class/net/$DEVICE/device/ieee80211" ]; then
	echo "Device is a WiFi mesh interface: $DEVICE"
else
	echo "This script is only applicable to the mesh interface."
	exit 0
fi

case "$ACTION" in
	ifup)
		# Set up the mesh interface with QoS
		# ToDo: Test better values here. I'm just starting the blueprint, okay?
		tc qdisc add dev $DEVICE root fq_codel limit 2048 memory_limit 5M flows 256
		;;
	ifdown)
		# Remove the QoS settings when the interface goes down
		tc qdisc del dev $DEVICE root || true
		;;
	*)
		# For any other action, do nothing
		;;
esac

