# Configuration file
# 
# script: rc.inet1.conf
#
# This file contains the configuration settings for network interfaces.
# If USE_DHCP[interface] is set to "yes", this overrides any other settings.
# If you don't have an interface, leave the settings null ("").
#
# You can configure network interfaces other than eth0,eth1... by setting
# IFNAME[interface] to the interface's name. If IFNAME[interface] is unset
# or empty, it is assumed you're configuring eth<interface>.
#
# LimeTech - modified for Unraid OS
# Bergware - modified for Unraid OS, February 2025

# Adapted by Bergware for use in Unraid OS - December 2023
# - added functions to remove leading zeros in IPv4 and IPv6 addresses

# Adapted by Bergware for use in Unraid OS - February 2025
# - fixed DNS entries get removed when configuring interface other then eth0

# change this to "yes" for debugging output to stdout.
# Unfortunately, /sbin/hotplug seems to disable stdout so you'll only see debugging output when rc.inet1 is called directly.

DEBUG_ETH_UP=no
DHCP_DEBUG=no

# function to remove leading zeros in IPv4 address
unzero(){
  local M Q
  echo -n $(for Q in ${1//./ }; do printf "$M%x" "0x$Q"; M=.; done)
}

# function to remove leading zeros in IPv6 address
unzero6(){
  local A M Q
  A=${1/::/:-:}
  echo -n $(for Q in ${A//:/ }; do [[ $Q != - ]] && printf "$M%x" "0x$Q" || printf ":"; M=:; done)
}

# Bergware - use associative format for multi-dimensional arrays
declare -A VLANID USE_DHCP IPADDR NETMASK GATEWAY METRIC USE_DHCP6 IPADDR6 NETMASK6 GATEWAY6 METRIC6 PRIVACY6 DESCRIPTION PROTOCOL

# LimeTech - read settings from config file
CFG=/boot/config/network.cfg
if [[ -s $CFG ]]; then
  . <(fromdos <$CFG)
else
  # default legacy settings
  IPADDR=
  NETMASK=
  GATEWAY=
  PROTOCOL=ipv4
  USE_DHCP=yes
  USE_DHCP6=
  DHCP_KEEPRESOLV=no
  DHCP6_KEEPRESOLV=no
  BONDING=yes
  BRIDGING=yes
fi

# init DHCP_KEEPRESOLV appropriately if not defined in network.cfg
if [[ $USE_DHCP == yes ]]; then
  DHCP_HOSTNAME=$(hostname)
  [[ -z $DHCP_KEEPRESOLV ]] && DHCP_KEEPRESOLV=no
else
  DHCP_KEEPRESOLV=yes
fi
if [[ $USE_DHCP6 == yes ]]; then
  DHCP_HOSTNAME=$(hostname)
  [[ -z $DHCP6_KEEPRESOLV ]] && DHCP6_KEEPRESOLV=no
else
  DHCP6_KEEPRESOLV=yes
fi

make_cfg(){
  [[ -s $CFG ]] && return
  for VAR in "$@"; do
    if [[ ${VAR:0:1} == '#' ]]; then
      # add comment
      echo -ne "$VAR\r\n" >>$CFG
    elif [[ ${VAR: -1} == '#' ]]; then
      # add global variable
      VAR=${VAR::-1}
      echo -ne "$VAR=\"${!VAR}\"\r\n" >>$CFG
    else
      # add interface variable
      echo -ne "$VAR[0]=\"${!VAR}\"\r\n" >>$CFG
    fi
  done
}

if [[ -n $SYSNICS ]]; then
  # Bergware - set number of interfaces as present in the system
  MAXNICS=$SYSNICS
else
  # Bergware - legacy configuration of first interface
  if [[ $BONDING == yes && $BRIDGING == yes ]]; then
    # both bonding and bridging selected
    BONDNAME=bond0
    BONDNICS=${BONDNICS:-eth0 eth1 eth2 eth3}
    BONDING_MODE=${BONDING_MODE:-1}
    BONDING_MIIMON=${BONDING_MIIMON:-100}
    BRNAME=br0
    BRNICS=bond0
    BRSTP=no
    BRFD=0
    IFNAME=$BRNAME
    SETTINGS="BONDNAME BONDNICS BONDING_MODE BONDING_MIIMON BRNAME BRNICS BRSTP BRFD"
  elif [[ $BONDING == yes ]]; then
    # bonding selected
    BONDNAME=bond0
    BONDNICS=${BONDNICS:-eth0 eth1 eth2 eth3}
    BONDING_MODE=${BONDING_MODE:-1}
    BONDING_MIIMON=${BONDING_MIIMON:-100}
    IFNAME=$BONDNAME
    SETTINGS="BONDNAME BONDNICS BONDING_MODE BONDING_MIIMON"
  elif [[ $BRIDGING == yes ]]; then
    # bridging selected
    BRNAME=br0
    BRNICS=eth0
    BRSTP=no
    BRFD=0
    IFNAME=$BRNAME
    SETTINGS="BRNAME BRNICS BRSTP BRFD"
  else
    # normal interface
    IFNAME=eth0
  fi
  SYSNICS=1
  MAXNICS=$SYSNICS
  make_cfg "# Generated settings:" IFNAME DHCP_KEEPRESOLV# DHCP6_KEEPRESOLV# $SETTINGS PROTOCOL USE_DHCP USE_DHCP6 SYSNICS#
fi
