\section*{Appendix: Postinstall Scripts and Cloud-Init Configuration}
\addcontentsline{toc}{section}{Appendix: Postinstall Scripts and Cloud-Init Configuration}

This appendix includes the scripts and cloud-init configuration used to automate the setup of the farmer portal environment, including USB mounting and Wi-Fi hotspot broadcasting. Noted, this file can only be supported with Ubuntu Core OS.

\subsection*{postinstall.sh}
This script is designed to be executed on the host device (e.g., Raspberry Pi or Linux box) after the system is installed. It mounts a USB device labeled \texttt{MINIO\_USB} and starts a Wi-Fi hotspot using NetworkManager.

\begin{lstlisting}[language=bash, caption={Postinstall Script for Mounting USB and Broadcasting Hotspot}]
#!/bin/bash

# === Mount USB Drive ===
USB_LABEL="MINIO_USB"
MOUNT_PATH="/mnt/usb2/minio-data"
DEVICE=$(lsblk -o LABEL,NAME | grep "$USB_LABEL" | awk '{print "/dev/" $2}')

if [ -z "$DEVICE" ]; then
  echo "❌ USB with label $USB_LABEL not found."
  exit 1
fi

echo "✅ Found USB device: $DEVICE"

sudo mkdir -p "$MOUNT_PATH"
sudo mount "$DEVICE" "$MOUNT_PATH"
sudo chown -R $USER:$USER "$MOUNT_PATH"

# === Setup Wi-Fi Hotspot ===
SSID="FarmerPortal"
PASSWORD="farmer123"
INTERFACE="wlan0"

echo "Starting Wi-Fi hotspot on $INTERFACE..."
nmcli device wifi hotspot ifname $INTERFACE ssid $SSID password $PASSWORD

echo "✅ Postinstall complete. Portal available at http://192.168.50.1:8080 or http://localhost:8080"
\end{lstlisting}

\subsection*{cloud-init.yaml}
This file automates the postinstall process on first boot. It installs required packages, mounts the USB device, and enables the Wi-Fi hotspot automatically.

\begin{lstlisting}[language=yaml, caption={Cloud-Init Configuration for Automated Setup}]
#cloud-config

package_update: true
package_upgrade: true
packages:
  - network-manager
  - dnsmasq
  - wireless-tools
  - net-tools

write_files:
  - path: /usr/local/bin/postinstall.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      USB_LABEL="MINIO_USB"
      MOUNT_PATH="/mnt/usb2/minio-data"
      DEVICE=$(lsblk -o LABEL,NAME | grep "$USB_LABEL" | awk '{print "/dev/" $2}')
      if [ -n "$DEVICE" ]; then
        mkdir -p "$MOUNT_PATH"
        mount "$DEVICE" "$MOUNT_PATH"
        chown -R ubuntu:ubuntu "$MOUNT_PATH"
      fi

      SSID="FarmerPortal"
      PASSWORD="farmer123"
      INTERFACE="wlan0"
      nmcli dev wifi hotspot ifname $INTERFACE ssid $SSID password $PASSWORD

runcmd:
  - [ bash, /usr/local/bin/postinstall.sh ]
\end{lstlisting}

This setup ensures that the system is accessible both over Ethernet and via a self-hosted Wi-Fi hotspot, and that persistent object storage is available to services like MinIO on the mounted USB drive.
