SUMMARY = "WiFi Auto-Connect Configuration"
DESCRIPTION = "systemd-networkd and wpa_supplicant configuration for automatic WiFi connection"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://25-wireless.network \
           file://wpa_supplicant-wlp2s0.conf"

S = "${WORKDIR}"

do_install() {
    # Install systemd-networkd configuration
    install -d ${D}${sysconfdir}/systemd/network
    install -m 0644 ${WORKDIR}/25-wireless.network ${D}${sysconfdir}/systemd/network/

    # Install wpa_supplicant configuration
    install -d ${D}${sysconfdir}/wpa_supplicant
    install -m 0600 ${WORKDIR}/wpa_supplicant-wlp2s0.conf ${D}${sysconfdir}/wpa_supplicant/

    # Create systemd service for generic WiFi interface discovery
    install -d ${D}${sysconfdir}/systemd/system
    cat > ${D}${sysconfdir}/systemd/system/wifi-connect.service << EOF
[Unit]
Description=WiFi Auto-Connect Service
After=network.target
Wants=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c 'for iface in /sys/class/net/wlp*; do [ -e "\$iface" ] && systemctl start wpa_supplicant@\$(basename \$iface).service; done'
ExecStop=/bin/sh -c 'for iface in /sys/class/net/wlp*; do [ -e "\$iface" ] && systemctl stop wpa_supplicant@\$(basename \$iface).service; done'

[Install]
WantedBy=multi-user.target
EOF
    
    # Enable the generic WiFi service
    install -d ${D}${sysconfdir}/systemd/system/multi-user.target.wants
    ln -sf ${sysconfdir}/systemd/system/wifi-connect.service ${D}${sysconfdir}/systemd/system/multi-user.target.wants/wifi-connect.service
}

FILES:${PN} = "${sysconfdir}/systemd/network/* \
               ${sysconfdir}/wpa_supplicant/* \
               ${sysconfdir}/systemd/system/wifi-connect.service \
               ${sysconfdir}/systemd/system/multi-user.target.wants/*"

RDEPENDS:${PN} = "systemd wpa-supplicant"