#!/bin/bash

# Get the names of all interfaces.
# Filter interfaces with grep which you don't need.
# Could also be done with jq if one wants to spend more time on it.
mapfile -t interfaces < <(
  ip -json -pretty link show \
  | jq -r ".[].ifname" \
  | grep -vE "(bond|lo|tailscale)"
)

[[ -z "${interfaces[*]}" ]] && {
  echo "Error: No interfaces selected."
  exit 1
}

# Create upper part of the temporary service unit.
tmp_file_netplan="$(mktemp)"
tee "${tmp_file_netplan}" > "/dev/null" << EOF
network:
  version: 2
  renderer: NetworkManager
  ethernets:
EOF

for interface in "${interfaces[@]}"; do

  tee -a "${tmp_file_netplan}" > "/dev/null" << EOF
    ${interface}:
      wakeonlan: true
EOF

done

# Install the file and apply the new configuration
sudo mv -v "${tmp_file_netplan}" "/etc/netplan/02-wol.yaml"
