#!/bin/sh
# wifi-mkconf SSID [PSK] -> wpa_supplicant.conf on stdout. SSID is hex-encoded so
# any character (quotes, spaces, unicode) is safe; PSK quoted (wpa_supplicant takes the last ").
hex=$(printf '%s' "$1" | hexdump -v -e '/1 "%02x"')
[ -n "$hex" ] || exit 1
echo "ctrl_interface=/var/run/wpa_supplicant"
echo "update_config=0"
echo "network={"
echo "    #ssid=\"$(printf '%s' "$1" | tr -d '\n')\""
echo "    ssid=$hex"
echo "    scan_ssid=1"
if [ -n "$2" ]; then echo "    psk=\"$2\""; else echo "    key_mgmt=NONE"; fi
echo "}"
