#!/bin/sh
set -eu

# guessing parameters
case "$IFACE" in
	*:*) exit 0 ;;
	*.*) exit 0 ;;
	vlan*) exit 0 ;;
	*#[0-9]*) GUESSED_RAW_DEVICE="${IFACE%#*}" ;;
	*) [ -n "${IF_MVLAN_RAW_DEVICE:-}" ] || exit 0 ;;
esac

RAW_DEVICE="${IF_MVLAN_RAW_DEVICE:-${GUESSED_RAW_DEVICE:-}}"

if [ -z "$RAW_DEVICE" ]; then
	echo "RAW_DEVICE for $IFACE is not set"
	exit 1
fi

if ! ip link show dev "$RAW_DEVICE" >/dev/null; then
	echo "Device $RAW_DEVICE for $IFACE does not exist"
	exit 1
fi

ip link delete dev "$IFACE" type macvlan
