#! /bin/sh

dev="$1"
ip="$2"
delay="$3"

if [ -n "$dev" -a -n "$ip" -a -n "$delay" ]; then
	echo "Device: $dev"
	echo "IP address: $ip"
	echo "Delay: ${delay}ms"

	tc qdisc del dev "$dev" root 2>/dev/null
	tc qdisc add dev "$dev" root handle 1: prio
	tc qdisc add dev "$dev" parent 1:1 handle 2: netem delay "${delay}ms"
	tc filter add dev "$dev" parent 1:0 protocol ip pref 55 handle ::55 u32 match ip dst "${ip}" flowid 2:1
elif [ -n "$dev" -a "$ip" = "off" ]; then
	echo "Device: $dev"
	echo "Delay: off"
	tc qdisc del dev "$dev" root
else
	echo "Usage: $0 DEVICE [ IP DELAY | off ]"
	echo "Example: $0 eth0 12.3.4.5 65"
	exit 1
fi
