#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh
. /lib/mwan3/common.sh

help()
{
	cat <<EOF
Syntax: mwan3 [command]

Available commands:
	start           Load iptables rules, ip rules and ip routes
	stop            Unload iptables rules, ip rules and ip routes
	restart         Reload iptables rules, ip rules and ip routes
	ifup <iface>    Load rules and routes for specific interface
	ifdown <iface>  Unload rules and routes for specific interface
	interfaces      Show interfaces status
	policies        Show currently active policy
	connected       Show directly connected networks
	rules           Show active rules
	status          Show all status

EOF
}


ifdown() {
	if [ -z "$1" ]; then
		echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>"
		exit 0
	fi

	if [ -n "$2" ]; then
		echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>"
		exit 0
	fi

	mwan3_interface_hotplug_shutdown "$1" 1
}

ifup() {
	. /etc/init.d/mwan3

	if [ -z "$1" ]; then
		echo "Expecting interface. Usage: mwan3 ifup <interface>"
		exit 0
	fi

	if [ -n "$2" ]; then
		echo "Too many arguments. Usage: mwan3 ifup <interface>"
		exit 0
	fi

	mwan3_ifup "$1"
}

interfaces()
{
	config_load mwan3

	echo "Interface status:"
	config_foreach mwan3_report_iface_status interface
	echo
}

policies()
{
	echo "Current ipv4 policies:"
	mwan3_report_policies_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Current ipv6 policies:"
	mwan3_report_policies_v6
	echo
}

connected()
{
	echo "Directly connected ipv4 networks:"
	mwan3_report_connected_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Directly connected ipv6 networks:"
	mwan3_report_connected_v6
	echo
}

rules()
{
	echo "Active ipv4 user rules:"
	mwan3_report_rules_v4
	echo
	[ $NO_IPV6 -ne 0 ] && return
	echo "Active ipv6 user rules:"
	mwan3_report_rules_v6
	echo
}

status()
{
	interfaces
	policies
	connected
	rules
}

start() {
	/etc/init.d/mwan3 enable
	/etc/init.d/mwan3 start
}

stop() {
	/etc/init.d/mwan3 disable
	/etc/init.d/mwan3 stop
}

restart() {
	/etc/init.d/mwan3 enable
	/etc/init.d/mwan3 stop
	/etc/init.d/mwan3 start
}

case "$1" in
	ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
		mwan3_init
		# shellcheck disable=SC2048
		$*
	;;
	*)
		help
	;;
esac

exit 0
