#!/bin/sh

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/mwan3/common.sh

INTERFACE=""
DEVICE=""

IFDOWN_EVENT=0
IFUP_EVENT=0
RELOAD_EVENT=0
WRAP_LIB=/lib/mwan3/libwrap_mwan3_sockopt.so.1.0

stop_subprocs() {
	local killpids="$(jobs -p)"
	[ -n "$killpids" ] && kill $killpids
}

WRAP() {
	# shellcheck disable=SC2048
	FAMILY=$FAMILY DEVICE=$DEVICE SRCIP=$SRC_IP FWMARK=$MMX_DEFAULT LD_PRELOAD=$WRAP_LIB $*
}

clean_up() {
	LOG notice "Stopping mwan3track for interface \"${INTERFACE}\". Status was \"${STATUS}\""
	stop_subprocs
	exit 0
}

if_down() {
	LOG info "Detect ifdown event on interface ${INTERFACE} (${DEVICE})"
	IFDOWN_EVENT=1
	stop_subprocs
}

if_up() {
	LOG info "Detect ifup event on interface ${INTERFACE} (${DEVICE})"
	IFDOWN_EVENT=0
	IFUP_EVENT=1
	started
	stop_subprocs
}

config_reload() {
	LOG info "Received configuration reload signal for interface ${INTERFACE}"
	RELOAD_EVENT=1
	stop_subprocs
}

ping_test_host() {
	if [ "$FAMILY" = "ipv6" ]; then
		echo "::1"
	else
		echo "127.0.0.1"
	fi
}

get_ping_command() {
	if [ -x "/usr/bin/ping" ] && /usr/bin/ping -${FAMILY#ipv} -c1 -q $(ping_test_host) &>/dev/null; then
		# -4 option added in iputils c3e68ac6 so need to check if we can use it
		# or if we must use ping and ping6
		echo "/usr/bin/ping -${FAMILY#ipv}"
	elif [ "$FAMILY" = "ipv6" ] && [ -x "/usr/bin/ping6" ]; then
		echo "/usr/bin/ping6"
	elif [ "$FAMILY" = "ipv4" ] && [ -x "/usr/bin/ping" ]; then
		echo "/usr/bin/ping"
	elif [ -x "/bin/ping" ]; then
		echo "/bin/ping -${FAMILY#ipv}"
	else
		return 1
	fi
}

validate_track_method() {
	case "$1" in
		ping)
			PING=$(get_ping_command)
			if [ $? -ne 0 ]; then
				LOG warn "Missing ping. Please enable BUSYBOX_DEFAULT_PING and recompile busybox or install iputils-ping package."
				return 1
			fi
			;;
		arping)
			command -v arping 1>/dev/null 2>&1 || {
				LOG warn "Missing arping. Please install iputils-arping package."
				return 1
			}
			;;
		httping)
			command -v httping 1>/dev/null 2>&1 || {
				LOG warn "Missing httping. Please install httping package."
				return 1
			}
			;;
		nping-*)
			command -v nping 1>/dev/null 2>&1 || {
				LOG warn "Missing nping. Please install nping package."
				return 1
			}
			;;
		nslookup)
			command -v nslookup 1>/dev/null 2>&1 || {
				LOG warn "Missing nslookup. Please install busybox package."
				return 1
			}
			;;
		*)
			LOG warn "Unsupported tracking method: $track_method"
			return 2
			;;
	esac
}

validate_wrap() {
	[ -f "$WRAP_LIB" ] && return
	LOG error "Missing libwrap_mwan3_sockopt. Please reinstall mwan3." && exit 1
}

disconnected() {
	local status
	readfile status ${MWAN3TRACK_STATUS_DIR}/${INTERFACE}/STATUS

	STATUS='offline'
	echo "offline" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
	get_uptime > $MWAN3TRACK_STATUS_DIR/$INTERFACE/OFFLINE
	echo "0" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/ONLINE
	score=0
	[ "$1" = 1 ] && return

	# Only execute disconnectd action if status was online or disconnecting
	if [ "$status" = "online" ] || [ "$status" = "disconnecting" ]; then
		LOG notice "Interface $INTERFACE ($DEVICE) is offline"
		env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
	else
		LOG debug "Skip disconnected event for $INTERFACE ($DEVICE)"
	fi
}

connected() {
	STATUS='online'
	echo "online" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
	echo "0" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/OFFLINE
	get_uptime > $MWAN3TRACK_STATUS_DIR/$INTERFACE/ONLINE
	score=$((down+up))
	host_up_count=0
	lost=0
	turn=0
	loss=0
	LOG notice "Interface $INTERFACE ($DEVICE) is online"
	env -i FIRSTCONNECT=$1 ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
}

disconnecting() {
	if [ "$STATUS" != "disconnecting" ] ; then
		STATUS="disconnecting"
		echo "disconnecting" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
		LOG notice "Interface $INTERFACE ($DEVICE) is disconnecting"
		env -i ACTION="disconnecting" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
	fi
}

connecting() {
	if [ "$STATUS" != "connecting" ] ; then
		STATUS="connecting"
		echo "connecting" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
		LOG notice "Interface $INTERFACE ($DEVICE) is connecting"
		env -i ACTION="connecting" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface
	fi
}

disabled() {
	STATUS='disabled'
	echo "disabled" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
	stopped
}

firstconnect() {
	local true_iface
	network_flush_cache

	mwan3_get_true_iface true_iface $INTERFACE
	network_get_device DEVICE $true_iface

	if [ "$STATUS" != "online" ]; then
		config_get STATUS $INTERFACE initial_state "online"
	fi

	if ! network_is_up $true_iface || [ -z "$DEVICE" ]; then
		disabled
		return
	fi

	mwan3_get_src_ip SRC_IP $INTERFACE

	LOG debug "firstconnect: called on $INTERFACE/$true_iface ($DEVICE). Status is $STATUS. SRC_IP is $SRC_IP"

	started
	if [ "$STATUS" = "offline" ]; then
		disconnected 1
	else
		connected 1
	fi
}

stopped() {
	STARTED=0
	echo 0 > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STARTED
}

started() {
	STARTED=1
	echo 1 > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STARTED
}

update_status() {
	local track_ip=$1

	echo "$2" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/TRACK_${track_ip}
	[ -z "$3" ] && return
	echo "$3" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/LATENCY_${track_ip}
	echo "$4" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/LOSS_${track_ip}
}

load_tracking_config() {
	config_get_bool MWAN3_VERBOSE_LOGGING globals verbose_logging 0
	config_get FAMILY $INTERFACE family ipv4
	config_get track_method $INTERFACE track_method ping
	config_get_bool httping_ssl $INTERFACE httping_ssl 0
	validate_track_method $track_method || {
		track_method=ping
		if validate_track_method $track_method; then
			LOG warn "Using ping to track interface $INTERFACE avaliability"
		else
			LOG err "No track method avaliable"
			exit 1
		fi
	}
	config_get reliability $INTERFACE reliability 1
	config_get count $INTERFACE count 1
	config_get timeout $INTERFACE timeout 4
	config_get interval $INTERFACE interval 10
	config_get down $INTERFACE down 5
	config_get up $INTERFACE up 5
	config_get size $INTERFACE size 56
	config_get max_ttl $INTERFACE max_ttl 60
	config_get failure_interval $INTERFACE failure_interval $interval
	config_get_bool keep_failure_interval $INTERFACE keep_failure_interval 0
	config_get recovery_interval $INTERFACE recovery_interval $interval
	config_get_bool check_quality $INTERFACE check_quality 0
	config_get failure_latency $INTERFACE failure_latency 1000
	config_get recovery_latency $INTERFACE recovery_latency 500
	config_get failure_loss $INTERFACE failure_loss 40
	config_get recovery_loss $INTERFACE recovery_loss 10
	# arping/nslookup/nping don't produce latency/loss samples;
	# check_quality would trigger arithmetic errors on the empty values.
	case "$track_method" in
		ping|httping) ;;
		*)
			if [ "$check_quality" -eq 1 ]; then
				LOG notice "check_quality is not supported for track_method '$track_method' on interface $INTERFACE; treating as disabled"
				check_quality=0
			fi
		;;
	esac
}

main() {
	local reliability count timeout interval failure_interval
	local recovery_interval down up size
	local keep_failure_interval check_quality failure_latency
	local recovery_latency failure_loss recovery_loss

	local max_ttl httping_ssl track_ips do_log

	INTERFACE=$1
	STATUS=""
	mkdir -p $MWAN3TRACK_STATUS_DIR/$INTERFACE
	exec 9>"$MWAN3TRACK_STATUS_DIR/$INTERFACE/LOCK"
	if ! flock -n 9; then
		local old_pid
		old_pid=$(cat $MWAN3TRACK_STATUS_DIR/$INTERFACE/PID 2>/dev/null)
		if [ -n "$old_pid" ] && [ "$old_pid" != "$$" ] && kill -0 "$old_pid" 2>/dev/null; then
			LOG notice "Terminating stale mwan3track instance (PID $old_pid) for interface $INTERFACE"
			kill "$old_pid" 2>/dev/null
		fi
		flock 9
	fi
	echo $$ > $MWAN3TRACK_STATUS_DIR/$INTERFACE/PID
	stopped
	PING_OUTPUT=$MWAN3TRACK_STATUS_DIR/$INTERFACE/PING_OUTPUT

	mwan3_init
	validate_wrap

	trap clean_up TERM
	trap if_down USR1
	trap if_up USR2
	trap config_reload HUP

	load_tracking_config
	local sleep_time result ping_status loss latency
	mwan3_list_track_ips()
	{
		track_ips="$track_ips $1"
	}
	mwan3_load_track_ips()
	{
		local track_gateway gw_ip f ip
		track_ips=""
		config_list_foreach "$INTERFACE" track_ip mwan3_list_track_ips
		config_get_bool track_gateway "$INTERFACE" track_gateway 0
		if [ "$track_gateway" -eq 1 ]; then
			gw_ip=$(cat "$MWAN3TRACK_STATUS_DIR/${INTERFACE}/GATEWAY" 2>/dev/null)
			if [ -n "$gw_ip" ]; then
				case " $track_ips " in
					*" $gw_ip "*) ;;
					*) track_ips="$gw_ip $track_ips" ;;
				esac
			fi
		fi
		for f in "$MWAN3TRACK_STATUS_DIR/$INTERFACE/TRACK_"*; do
			[ -f "$f" ] || continue
			ip="${f##*/TRACK_}"
			[ "$ip" = "OUTPUT" ] && continue
			case " $track_ips " in
				*" $ip "*) ;;
				*) rm -f "$MWAN3TRACK_STATUS_DIR/$INTERFACE/TRACK_${ip}" \
				         "$MWAN3TRACK_STATUS_DIR/$INTERFACE/LATENCY_${ip}" \
				         "$MWAN3TRACK_STATUS_DIR/$INTERFACE/LOSS_${ip}" ;;
			esac
		done
		local current_check_quality
		config_get_bool current_check_quality "$INTERFACE" check_quality 0
		# Mirror the clamp applied at startup so LATENCY_/LOSS_ files
		# are cleaned up when the effective check_quality is disabled
		# by virtue of an incompatible track_method.
		case "$track_method" in
			ping|httping) ;;
			*) current_check_quality=0 ;;
		esac
		if [ "$current_check_quality" -eq 0 ]; then
			rm -f $MWAN3TRACK_STATUS_DIR/$INTERFACE/LATENCY_* \
			      $MWAN3TRACK_STATUS_DIR/$INTERFACE/LOSS_* 2>/dev/null
		fi
	}
	mwan3_load_track_ips

	local score=$((down+up))
	local warn_threshold=$((up + (down + 2) / 3))
	local host_up_count=0
	local lost=0
	local turn=0
	local failed_hosts=""

	firstconnect
	while true; do
		[ $STARTED -eq 0 ] && {
			sleep $MAX_SLEEP &
			wait $!
		}
		# Process any pending interface events before starting a ping round.
		# This avoids pinging with stale DEVICE/SRC_IP after wakeup from
		# disabled state — a USR2 signal sets IFUP_EVENT but DEVICE is not
		# refreshed until firstconnect() runs below.
		if [ "${IFDOWN_EVENT}" -eq 1 ]; then
			LOG debug "Register ifdown event on interface ${INTERFACE} (${DEVICE})"
			disconnected
			disabled
			IFDOWN_EVENT=0
			continue
		fi
		if [ "${IFUP_EVENT}" -eq 1 ]; then
			LOG debug "Register ifup event on interface ${INTERFACE} (${DEVICE})"
			config_load mwan3
			mwan3_load_track_ips
			firstconnect
			IFUP_EVENT=0
			continue
		fi
		if [ "${RELOAD_EVENT}" -eq 1 ]; then
			LOG debug "Processing configuration reload for interface ${INTERFACE}"
			config_load mwan3
			load_tracking_config
			mwan3_load_track_ips
			if [ $score -gt $((down + up)) ]; then
				score=$((down + up))
			fi
			warn_threshold=$((up + (down + 2) / 3))
			RELOAD_EVENT=0
			continue
		fi
		sleep_time=$interval
		for track_ip in $track_ips; do
			if [ $host_up_count -lt $reliability ]; then
				case "$track_method" in
					ping)
						if [ $check_quality -eq 0 ]; then
							WRAP $PING -n -c $count -W $timeout -s $size -t $max_ttl -q $track_ip &> /dev/null &
							wait $!
							result=$?
						else
							WRAP $PING -n -c $count -W $timeout -s $size -t $max_ttl -q $track_ip 2>/dev/null > $PING_OUTPUT &
							wait $!
							ping_status=$?
							loss="$(sed $PING_OUTPUT -ne 's/.* \([0-9]\+\)% packet loss.*/\1/p')"
							if [ "$ping_status" -ne 0 ] || [ "$loss" -eq 100 ]; then
								latency=999999
								loss=100
							else
								latency="$(sed $PING_OUTPUT -ne 's%\(rtt\|round-trip\).* = [^/]*/\([0-9]\+\).*%\2%p')"
							fi
						fi
					;;
					arping)
						WRAP arping -I $DEVICE -c $count -w $timeout -q $track_ip &> /dev/null &
						wait $!
						result=$?
					;;
					httping)
						if [ $check_quality -eq 0 ]; then
							if [ "$httping_ssl" -eq 1 ]; then
								WRAP httping -c $count -t $timeout -q "https://$track_ip" &> /dev/null &
							else
								WRAP httping -c $count -t $timeout -q "http://$track_ip" &> /dev/null &
							fi
							wait $!
							result=$?
						else
							if [ "$httping_ssl" -eq 1 ]; then
								WRAP httping -c $count -t $timeout "https://$track_ip" 2> /dev/null > $PING_OUTPUT &
							else
								WRAP httping -c $count -t $timeout "http://$track_ip" 2> /dev/null > $PING_OUTPUT &
							fi
							wait $!
							ping_status=$?
							loss="$(sed $PING_OUTPUT -ne 's/.* \([0-9]\+\).*% failed.*/\1/p')"
							if [ "$ping_status" -ne 0 ] || [ "$loss" -eq 100 ]; then
								latency=999999
								loss=100
							else
								latency="$(sed $PING_OUTPUT -ne 's%\(rtt\|round-trip\).* = [^/]*/\([0-9]\+\).*%\2%p')"
							fi
						fi
					;;
					nping-*)
						WRAP nping -${FAMILY#ipv} -c $count $track_ip --${track_method#nping-} > $PING_OUTPUT &
						wait $!
						result=$(grep Lost $PING_OUTPUT | awk '{print $12}')
						result=${result:-1}
					;;
					nslookup)
						WRAP nslookup www.google.com $track_ip > $PING_OUTPUT &
						wait $!
						result=$?
					;;
				esac
				if [ $check_quality -eq 0 ]; then
					if [ $result -eq 0 ]; then
						let host_up_count++
						update_status "$track_ip" "up"

						[ $score -le $up ] && LOG debug "Check ($track_method) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
					else
						let lost++
						update_status "$track_ip" "down"

						failed_hosts="${failed_hosts:+$failed_hosts }$track_ip"
					fi
				else
					if [ "$loss" -ge "$failure_loss" ] || [ "$latency" -ge "$failure_latency" ]; then
						let lost++
						update_status "$track_ip" "down" $latency $loss

						failed_hosts="${failed_hosts:+$failed_hosts }$track_ip(${latency}ms/${loss}%)"
					elif [ "$loss" -le "$recovery_loss" ] && [ "$latency" -le "$recovery_latency" ]; then
						let host_up_count++
						update_status "$track_ip" "up" $latency $loss

						[ $score -le $up ] && LOG debug "Check (${track_method}: latency=${latency}ms loss=${loss}%) success for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score"
					else
						echo "skipped" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/TRACK_${track_ip}
					fi
				fi
			else
				echo "skipped" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/TRACK_${track_ip}
			fi
			if [ "${IFDOWN_EVENT}" -eq 1 ] || [ "${IFUP_EVENT}" -eq 1 ] || [ "${RELOAD_EVENT}" -eq 1 ]; then
				break
			fi
		done

		if [ "${IFDOWN_EVENT}" -eq 1 ] || [ "${IFUP_EVENT}" -eq 1 ] || [ "${RELOAD_EVENT}" -eq 1 ]; then
			host_up_count=0
			failed_hosts=""
			continue
		fi

		# Log failures only when the round actually failed (reliability not met).
		# Per-host failures are suppressed when a later host met the threshold,
		# since logging them would falsely imply the interface is degraded.
		# Also suppress when hotplug has already marked the interface offline
		# (e.g. manual ifdown) — the USR1 signal may still be in transit.
		if [ $host_up_count -lt $reliability ] && [ -n "$failed_hosts" ] && [ $score -gt $up ]; then
			local hp_state
			readfile hp_state "$MWAN3_STATUS_DIR/iface_state/$INTERFACE" 2>/dev/null
			if [ "$hp_state" != "offline" ]; then
				LOG info "Check ($track_method) failed for target(s) \"$failed_hosts\" on interface $INTERFACE ($DEVICE). Current score: $score"
			fi
		fi

		if [ $host_up_count -lt $reliability ]; then
			let score--

			if [ $score -lt $up ]; then
				score=0
				[ ${keep_failure_interval} -eq 1 ] && sleep_time=$failure_interval
			elif [ $score -eq $up ]; then
				disconnecting
				sleep_time=$failure_interval
				disconnected
			elif [ $score -gt $up ]; then
				[ $score -le $warn_threshold ] && disconnecting
				sleep_time=$failure_interval
			fi
		else
			if [ $score -lt $((down+up)) ] && [ $lost -gt 0 ]; then
				LOG debug "Lost $lost host(s) on interface $INTERFACE ($DEVICE). Current score: $score"
			fi

			let score++
			lost=0

			if [ $score -lt $up ]; then
				connecting
				sleep_time=$recovery_interval
			elif [ $score -eq $up ]; then
				connecting
				sleep_time=$recovery_interval
				connected
			elif [ $score -gt $up ]; then
				if [ "$STATUS" = "disconnecting" ]; then
					STATUS="online"
					LOG notice "Interface $INTERFACE ($DEVICE) is online"
				fi
				echo "online" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS
				score=$((down+up))
			fi
		fi

		let turn++
		mkdir -p "$MWAN3TRACK_STATUS_DIR/${1}"
		echo "${lost}" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/LOST
		echo "${score}" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/SCORE
		echo "${turn}" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/TURN
		get_uptime > $MWAN3TRACK_STATUS_DIR/$INTERFACE/TIME

		host_up_count=0
		failed_hosts=""
		if [ "${IFDOWN_EVENT}" -eq 0 ] && [ "${IFUP_EVENT}" -eq 0 ]; then
			sleep "${sleep_time}" &
			wait $!
		fi

		if [ "${IFDOWN_EVENT}" -eq 1 ]; then
			LOG debug "Register ifdown event on interface ${INTERFACE} (${DEVICE})"
			disconnected
			disabled
			IFDOWN_EVENT=0
		fi
		if [ "${IFUP_EVENT}" -eq 1 ]; then
			LOG debug "Register ifup event on interface ${INTERFACE} (${DEVICE})"
			config_load mwan3
			mwan3_load_track_ips
			firstconnect
			IFUP_EVENT=0
		fi
		if [ "${RELOAD_EVENT}" -eq 1 ]; then
			LOG debug "Processing configuration reload for interface ${INTERFACE}"
			config_load mwan3
			load_tracking_config
			mwan3_load_track_ips
			if [ $score -gt $((down + up)) ]; then
				score=$((down + up))
			fi
			warn_threshold=$((up + (down + 2) / 3))
			RELOAD_EVENT=0
		fi
	done
}

main "$@"
