#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot; invoke with: sv once networking-stop
#Comment: ifdown -a (ifupdown2)

exec 2>&1

ebegin() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
eend() { r=${1:-0}; shift || true; [ "$r" -eq 0 ] || [ "${VERBOSE:-}" = no ] || echo "$*" >&2; return "$r"; }
einfo() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
ewarn() { echo "$@" >&2; }
eerror() { echo "$@" >&2; }

[ -x /sbin/ifdown ] || exit 0
EXTRA_ARGS=
[ -f /etc/default/networking ] && . /etc/default/networking
[ "$VERBOSE" = yes ] && EXTRA_ARGS=-v
[ "$DEBUG" = yes ] && EXTRA_ARGS="$EXTRA_ARGS -d"
[ "$SYSLOG" = yes ] && EXTRA_ARGS="$EXTRA_ARGS --syslog"
if [ "${SKIP_DOWN_AT_SYSRESET:-}" = "yes" ]; then
  einfo "Skipping ifdown at sysreset"
  exit 0
fi
if [ -e /etc/iscsi/iscsi.initramfs ]; then
  ewarn "not deconfiguring network interfaces: iSCSI root is mounted."
  exit 0
fi
exclusions=""
if [ -n "${EXCLUDE_INTERFACES:-}" ]; then
  for d in $EXCLUDE_INTERFACES; do exclusions="-X $d $exclusions"; done
fi
[ ! -e /run/network ] && mkdir -p /run/network
ebegin "Deconfiguring network interfaces"
ifdown -a $EXTRA_ARGS $exclusions
eend $?
exit $?
