#!/usr/bin/tclsh8.6

# Automatically setup dhcp for some interfaces in case we are not
# using NetworkManager ("wirelessaccesspoint"-hosttype) and not booting
# with PXE.  (note systemd-networkd might make this easier than this)

set dhcp_interfaces [exec puavo-conf puavo.networking.dhcp.interfaces]

if {$dhcp_interfaces eq ""} { exit 0 }

set interfaces_path    "/etc/network/interfaces"
set interfaces_tmppath "${interfaces_path}.tmp"

set if_file [open $interfaces_tmppath w]

puts -nonewline $if_file {# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
}

foreach interface_dir [glob "/sys/class/net/${dhcp_interfaces}"] {
  if {[file exists "${interface_dir}/phy80211"]} { continue }

  set interface [file tail $interface_dir]

  puts $if_file "
auto $interface
iface $interface inet dhcp
"
}

close $if_file

file rename -force $interfaces_tmppath $interfaces_path
