#!/bin/sh /etc/rc.common
# Copyright (c) 2026 Dab Co. Limited. All rights reserved.
#
# This software and its documentation are the confidential and
# proprietary information of Dab Co. Limited. Redistribution or use
# in source or binary forms, with or without modification, is not
# permitted without the express written consent of Dab Co. Limited.
# Sibling of /etc/init.d/pps-vpn — runs the route-loop monitor / hung-tunnel
# watchdog (G14 / G2 / G5). Independent procd instance so a crash in the
# monitor does not take down the VPN, and so prerm can stop it BEFORE the
# main service (otherwise it would auto-restart pps-vpn during uninstall).

USE_PROCD=1
START=96
STOP=11

INSTALL_DIR="${PPS_VPN_HOME:-/usr/lib/pps-vpn}"
BIN="/bin/sh"
SCRIPT="$INSTALL_DIR/scripts/pps-vpn-netmon.sh"

start_service() {
    [ -x "$SCRIPT" ] || [ -r "$SCRIPT" ] || return 1
    procd_open_instance
    procd_set_param command "$BIN" "$SCRIPT"
    # retry=0 means UNLIMITED in procd (instance.c gives up only when
    # respawn_retry > 0 and respawn_count exceeds it). netmon deliberately
    # exit(0)s to hand off a restart (see pps-vpn-netmon.sh), and procd counts
    # clean exits against the budget, so the old `5` retired the watchdog
    # itself after 5 recovery events in an hour — taking the route fix, the
    # liveness ping AND the dead-process recovery down with it, under
    # exactly the flaky-bearer conditions they exist to survive. A watchdog
    # that can be permanently retired is not a watchdog.
    procd_set_param respawn 3600 10 0
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_close_instance
}

reload_service() {
    stop
    start
}
