#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot; invoke with: sv once networking
#Comment: ifup -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/ifup ] || exit 0
[ -x /sbin/ifdown ] || exit 0
CONFIGURE_INTERFACES=yes
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"
RUN_DIR="/run/network"
IFSTATE_LOCKFILE="${RUN_DIR}/ifstatelock"
STATE_DIR="/var/tmp/network"
IFSTATE_FILE="${STATE_DIR}/ifstatenew"
[ ! -e ${IFSTATE_LOCKFILE} ] && rm -f ${IFSTATE_FILE}
[ ! -e /run/network ] && mkdir -p /run/network
[ ! -e /etc/network/run ] && ln -sf /run/network /etc/network/run
if [ "$CONFIGURE_INTERFACES" = no ]; then
  einfo "Not configuring network interfaces, see /etc/default/networking"
  exit 0
fi
exclusions=""
if [ -n "${EXCLUDE_INTERFACES:-}" ]; then
  for d in $EXCLUDE_INTERFACES; do exclusions="-X $d $exclusions"; done
fi
perfoptions=""
[ ! -f ${IFSTATE_LOCKFILE} ] && perfoptions="--perfmode"
ebegin "Configuring network interfaces"
ifup -a $EXTRA_ARGS $exclusions $perfoptions
eend $?
exit $?
