#!/bin/bash

# Copyright (C) 2013-2025 Marcin Krol <hawk@tld-linux.org>
# All Rights Reserved

. /lib/cri/vars

dialog_title="Network configurator"

. /lib/cri/functions

if_selection_menu () {
  interfaces=$(/lib/cri/netconfig.list $@)
  # bail out if no interfaces
  if [ -z "${interfaces}" ]; then
    dialog \
      --backtitle "${dialog_backtitle}" \
      --title "${dialog_title}" \
      --colors \
      --msgbox "\nNo suitable network interfaces for selected operation." \
      7 60
    return 1
  fi

  # create options file for dialog
  cat >/tmp/dialog.opts <<EOF
    --backtitle "${dialog_backtitle}"
    --title "${dialog_title}"
    --colors
    --menu "Select network interface:"
      20 50 17
    $(
      for int in ${interfaces}; do
        if echo "${int}" | grep -E -q "^lo$"; then
          echo "${int} \"Loopback interface\""
        elif echo "${int}" | grep -E -q "\.[0-9]"; then
          echo "${int} \"VLAN interface\""
        elif echo "${int}" | grep -E -q "^(en|eth)"; then
          echo "${int} \"Ethernet interface\""
        elif echo "${int}" | grep -E -q "^wl"; then
          echo "${int} \"Wireless interface\""
        elif echo "${int}" | grep -E -q "^ww"; then
          echo "${int} \"WWAN interface\""
        elif echo "${int}" | grep -E -q "^bond[0-9]"; then
          echo "${int} \"Bonding interface\""
        elif echo "${int}" | grep -E -q "^br[0-9]"; then
          echo "${int} \"Bridge interface\""
        fi
      done
    )
EOF

  # show interface seletion menu
  dialog --file /tmp/dialog.opts \
    2>/tmp/$(basename "${0}.dialog" 2>/dev/null)
  return $?
}

# if no arguments were passed show menu
while : ; do
  # create options file for dialog
  cat >/tmp/dialog.opts <<EOF
    --backtitle "${dialog_backtitle}"
    --title "${dialog_title}"
    --colors
    --cancel-label "Quit"
    --menu ""
      13 40 7
      1 "Configure interface"
      2 "Create VLAN interface"
      3 "Delete VLAN interface"
      4 "Create bonding interface"
      5 "Delete bonding interface"
      6 "Create bridge interface"
      7 "Delete bridge interface"
EOF

  # show interface seletion menu
  dialog --file /tmp/dialog.opts \
    2>/tmp/$(basename "${0}.dialog" 2>/dev/null)
  result=$?
  if [ "${result}" -ne 0 ]; then
    clear
    exit 1
  fi
  clear
  selection=$(cat /tmp/$(basename "${0}.dialog" 2>/dev/null) 2>/dev/null)
  # check selection and take appropriate actions
  case "${selection}" in
    1)
      if_selection_menu -t ether
      result=$?
      if [ "${result}" -eq 0 ]; then
        iface=$(cat /tmp/netconfig.dialog 2>/dev/null)
        /lib/cri/netconfig.ip "${iface}"
      fi
      ;;
    2)
      if_selection_menu -t ether -k "(ether|bond)"
      result=$?
      if [ "${result}" -eq 0 ]; then
        iface=$(cat /tmp/netconfig.dialog 2>/dev/null)
        /lib/cri/netconfig.vlan add "${iface}"
      fi
      ;;
    3)
      if_selection_menu -t ether -k vlan
      result=$?
      if [ "${result}" -eq 0 ]; then
        iface=$(cat /tmp/netconfig.dialog 2>/dev/null)
        /lib/cri/netconfig.vlan del "${iface}"
      fi
      ;;
    4)
      /lib/cri/netconfig.bond add
      ;;
    5)
      if_selection_menu -t ether -k bond
      result=$?
      if [ "${result}" -eq 0 ]; then
        iface=$(cat /tmp/netconfig.dialog 2>/dev/null)
        /lib/cri/netconfig.bond del "${iface}"
      fi
      ;;
    6)
      /lib/cri/netconfig.br add
      ;;
    7)
      if_selection_menu -t ether -k bridge
      result=$?
      if [ "${result}" -eq 0 ]; then
        iface=$(cat /tmp/netconfig.dialog 2>/dev/null)
        /lib/cri/netconfig.br del "${iface}"
      fi
      ;;
    *)
      ;;
  esac
done
