#!/bin/sh
#
# Maximilian Wilhelm <max@sdn.clinic>
#  --  Wed 26 Aug 2020 08:15:58 PM CEST
#
# This executor is responsible for setting up the main B.A.T.M.A.N. adv. interface (eg. bat0)
# as well as managing settings of the underlying interfaces (hardifs).
#
# See interfaces-batman(5) for a list of supported options for hardifs as well as meshifs.
#
if [ "$VERBOSE" ]; then
	set -x
fi

if ! which batctl >/dev/null 2>&1; then
	echo "Error: batctl utility not found!" >&2
	exit 1
fi

#
# Compatiblity glue: Newer versions of batctl print a deprecation
# warning when called with -m <batif>. Avoid spamming the log and
# producting SPAM by silently handling this here.
mesh_if_param="-m"
if batctl -h 2>&1 | grep -q "meshif"; then
        mesh_if_param="meshif"
fi

#
# Functions to manage main B.A.T.M.A.N. adv. interface
batctl_if () {
	for iface in ${IF_BATMAN_IFACES}; do
		${MOCK} batctl "${mesh_if_param}" "${IFACE}" interface "$1" "${iface}"
	done
}

set_meshif_options () {
	# We only care for options of format IF_BATMAN_<OPTION_NAME>
	env | grep '^IF_BATMAN_[A-Z0-9_]\+' | while IFS="=" read opt value; do
		# Members, ignore-regex, routing-algo, and throughput_override are handled seperately
		if [ "${opt}" = "IF_BATMAN_IFACES" -o \
		     "${opt}" = "IF_BATMAN_IFACES_IGNORE_REGEX" -o \
		     "${opt}" = "IF_BATMAN_ROUTING_ALGO" -o \
		     "${opt}" = "IF_BATMAN_THROUGHPUT_OVERRIDE" ]; then
			continue
		fi

		# Convert options for the actual name
		real_opt=$(echo "${opt}" | sed -e 's/^IF_BATMAN_\([A-Z0-9_]\+\)/\1/' | tr '[A-Z]' '[a-z]')

		${MOCK} batctl "${mesh_if_param}" "${IFACE}" "${real_opt}" "${value}"
	done

}

set_routing_algo () {
	if [ "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_IV" -a "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_V" ]; then
		echo "Invalid routing algorithm \"$1\"."
		return
	fi

	batctl ra "${IF_BATMAN_ROUTING_ALGO}"
}


#
# Functions to manage B.A.T.M.A.N. adv. underlay interfaces (hardifs)
set_hardif_options () {
	# Query hardif parameter manually for now
	for hardif in ${IF_BATMAN_IFACES}; do
		penalty=$(ifquery -p "batman-hop-penalty" "${hardif}")
		if [ "${penalty}" ]; then
			${MOCK} batctl hardif "${hardif}" hop_penalty "${penalty}"
		fi

		throughput=$(ifquery -p "batman-throughput-override" "${hardif}")
		if [ "${throughput}" ]; then
			${MOCK} batctl hardif "${hardif}" throughput_override "${througput}"
		fi
	done
}


case "${PHASE}" in
	depend)
		if [ "${IF_BATMAN_IFACES}" ]; then
			echo "${IF_BATMAN_IFACES}"
		fi
		;;

	create)
		# Main B.A.T.M.A.N. adv. interface
		if [ "${IF_BATMAN_IFACES}" ]; then
			if [ "${IF_BATMAN_ROUTING_ALGO}" ]; then
				set_routing_algo
			fi

			if [ ! -d "/sys/class/net/${IFACE}" ]; then
				batctl "${mesh_if_param}" "${IFACE}" interface create || true
			fi
		fi
		;;

	pre-up)
		# Main B.A.T.M.A.N. adv. interface (meshif)
		if [ "${IF_BATMAN_IFACES}" ]; then
			batctl_if add
			set_meshif_options
			set_hardif_options
		fi
		;;

	destroy)
		if [ "${IF_BATMAN_IFACES}" -a -d "/sys/class/net/${IFACE}" ]; then
			# Newer versions of batctl provide the "interface destroy" API, try to use it
			if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then
				# Fall back to old style member interface removal
				batctl_if del
			fi
		fi
		;;

	*)
		exit 0
		;;
esac