LOG_FILE=/root/postinstall-nbde-network-flush.log
[ -n "@NO_SAVE@" ] && LOG_FILE=/dev/null

exec < /dev/tty3 > /dev/tty3
chvt 3
echo "#######################"
echo "# %nbde-network-flush #"
echo "#######################"
(
  cat << EOF > /usr/bin/nbde-network-flush
#!/bin/sh

# do_flush() flushes every active network interface. It is intended to
# run before NetworkManager starts, so that when it does it will be able
# to set up the network using the regular host configuration.
do_flush() {
  for f in /sys/class/net/*; do
    iface="\${f##*/}"
    [ "\${iface}" = "lo" ] && continue
    echo "Preparing to flush interface \${iface}" >&2
    ip -statistics address flush dev "\${iface}"
  done
}

# reset_autoconn_prio() will reset the autoconnect priority
# of the existing NM connections to zero.
reset_autoconn_prio() {
  nmcli -t -f NAME connection show 2>/dev/null | while read -r _c; do
    if ! _prio="\$(nmcli -t connection show "\${_c}" \
           | grep connection.autoconnect-priority: \
           | cut -d: -f2)" || [ -z "\${_prio}" ]; then
      continue
    fi
    [ "\${_prio}" -ge 0 ] && continue
    echo "Setting autoconnect-priority of connection \${_c} to zero" >&2
    nmcli connection modify "\${_c}" connection.autoconnect-priority 0
  done
}

case "\${1}" in
reset-autoconn-prio)
  reset_autoconn_prio;;
flush)
  do_flush;;
esac

# vim:set ts=2 sw=2 et:
EOF
  chmod 755 /usr/bin/nbde-network-flush

  cat << EOF > /etc/systemd/system/nbde-network-flush.service
[Unit]
Description=NBDE Network flush service
Before=network-pre.target
Wants=network-pre.target

[Service]
ExecStart=/usr/bin/nbde-network-flush flush

[Install]
WantedBy=default.target
EOF

  chmod 644 /etc/systemd/system/nbde-network-flush.service

  mkdir -p /usr/lib/dracut/modules.d/60nbde_flushing/
  chmod 755 /usr/lib/dracut/modules.d/60nbde_flushing/

  cat << EOF > /usr/lib/dracut/modules.d/60nbde_flushing/module-setup.sh
#!/bin/sh

depends() {
  echo network-manager
  return 255
}

install() {
  inst_multiple nmcli grep cut

  # \$moddir is a dracut variable.
  # shellcheck disable=SC2154
  inst_hook initqueue/online 60 "\$moddir/nbde_client-hook.sh"
  inst_hook initqueue/settled 60 "\$moddir/nbde_client-hook.sh"
}

# vim:set ts=2 sw=2 et:
EOF

  chmod 644 /usr/lib/dracut/modules.d/60nbde_flushing/module-setup.sh

  cat << EOF > /usr/lib/dracut/modules.d/60nbde_flushing/nbde_client-hook.sh
#!/bin/sh

# disable_initrd_connections() will disable autoconnect for the active
# connections within the initramfs, so that these will not clash with
# the system network connections once the flush is performed and
# the network configuration is applied to the system.
disable_initrd_connections() {
  nmcli -t -f NAME connection show --active 2>/dev/null | while read -r _c; do
  if ! _enabled="\$(nmcli -t connection show "\${_c}" \
             | grep connection.autoconnect: \
             | cut -d: -f2)" || [ -z "\${_enabled}" ]; then
    continue
  fi
  [ "\${_enabled}" = "no" ] && continue

  echo "[10mt] Disabling autoconnect for connection \${_c}" >&2
  nmcli connection modify "\${_c}" connection.autoconnect no
  done
}

disable_initrd_connections

# vim:set ts=2 sw=2 et:
EOF
  chmod 755 /usr/lib/dracut/modules.d/60nbde_flushing/nbde_client-hook.sh

systemctl enable nbde-network-flush
dracut -f --regenerate-all

) 2>&1 | tee "${LOG_FILE}"
chvt 1

# vim:set ts=2 sw=2 et:

