#!/bin/sh

# Ignore tailscale interface events to prevent restart loop.
case "$INTERFACE" in
	tailscale*) exit 0 ;;
esac
case "$DEVICE" in
	tailscale*) exit 0 ;;
esac

[ "$ACTION" = ifup ] || [ "$ACTION" = ifupdate ] || exit 0
[ "$ACTION" = ifupdate ] && [ -z "$IFUPDATE_ADDRESSES" ] && [ -z "$IFUPDATE_DATA" ] && exit 0

tailscale_enable="$(uci -q get tailscale.settings.enabled)"
[ "$tailscale_enable" = "1" ] || exit 0

# Start tailscale if service is enabled but tailscaled isn't running yet
# (e.g. after wan comes up for the first time post-boot).
#
# Match on the full path: busybox `pgrep -x` compares against argv[0], so the
# bare name `tailscaled` never matched and this guard never fired — every ifup
# and ifupdate event re-ran `start` on an already-running service.
pgrep -x /usr/sbin/tailscaled >/dev/null 2>&1 && exit 0

/etc/init.d/tailscale start >/tmp/tailscale.log 2>&1 &
