#!/usr/bin/env bash
# $Id: dudhya-device-update 121 2026-09-05 17:26:26Z peter $
#
# Copyright (C) 2026 Péter Felecan
# Assisted by Claude Sonnet 5 (Anthropic)
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# dudhya-device-update --- run ON the dudhya device itself: set up
# internet access for whichever connection type is in use, then
# update/upgrade packages (or install specific ones). Symmetric
# counterpart: Provisioning/dudhya-host-connect, which runs on the
# host and must be run first (or already be active) for either mode
# here to actually reach the internet.
#
# Usage:
#   dudhya-device-update cable|radio               # apk update && apk upgrade
#   dudhya-device-update cable|radio <pkg> [pkg...] # apk update && apk add <pkg...>
#
#   cable --- host reachable at 172.16.42.2 over the USB link's
#     usb0 interface (see Dudhya.org's "Getting SSH working at all"
#     and "Internet access via the host" sections).
#
#   radio --- host reachable over wlan0, at whichever address the
#     device's own dnsmasq (running as part of dudhya-wifi-mode ap)
#     leased it. Read from dnsmasq's own lease file when available
#     (the accurate, current answer); falls back to 192.168.4.2 (the
#     first address in WiFi/dnsmasq.conf.example's DHCP range, a
#     reasonable default if only one host has ever connected, but
#     genuinely a guess if the lease file is missing or empty).
#
# NOT YET VERIFIED against real hardware in either mode --- see
# ResearchJournal.org and the equivalent caution in
# Provisioning/dudhya-host-connect.

set -euo pipefail

usage()
{
	echo "usage: $0 cable|radio [package ...]" >&2
	exit 1
}

if [ "$#" -lt 1 ]
then
{
	usage
}
fi

mode="$1"
shift

detect_ap_gateway()
{
	local lease_file="/var/lib/misc/dnsmasq.leases"
	local fallback_gateway="192.168.4.2"

	if [ -s "${lease_file}" ]
	then
	{
		# Lease file format: <expiry> <mac> <ip> <hostname> <client-id>,
		# one lease per line. Most recent line, third field.
		awk '{print $3}' "${lease_file}" | tail -1
	}
	else
	{
		echo "${fallback_gateway}"
	}
	fi
}

set_default_route()
{
	local gateway="$1"
	local iface="$2"

	# ip route replace, not add: safe to re-run without first needing
	# to check whether a default route already exists.
	ip route replace default via "${gateway}" dev "${iface}"
	echo "nameserver 1.1.1.1" > /etc/resolv.conf
}

case "${mode}" in
	cable)
	{
		set_default_route "172.16.42.2" "usb0"
		echo "dudhya-device-update: routing via 172.16.42.2 (usb0)"
	}
	;;
	radio)
	{
		gateway="$(detect_ap_gateway || true)"
		set_default_route "${gateway}" "wlan0"
		echo "dudhya-device-update: routing via ${gateway} (wlan0)"
	}
	;;
	*)
	{
		usage
	}
	;;
esac

if [ "$#" -eq 0 ]
then
{
	apk update
	apk upgrade
}
else
{
	apk update
	apk add "$@"
}
fi

# Local Variables:
# mode: shell-script
# End:
