#!/bin/sh
# SPDX-License-Identifier: GPL-3.0+
# Copyright 2020-2023 Lukas F. Hartmann <lukas@mntre.com>
# Copyright 2022-2025 Johannes Schauer Marin Rodrigues <josch@mister-muffin.de>

set -e

usage() {
  echo "On the i.MX8MQ, choose whether to use the single or dual display device tree" >&2
  echo "by writing the correct value into /etc/flash-kernel/machine and re-generating" >&2
  echo "the initramfs and running flash-kernel." >&2
  echo >&2
  echo "Usage: $0 [--help] [--emmc] dual|single" >&2
  echo >&2
  echo "Options:" >&2
  echo "  --help           Display this help and exit." >&2
  echo "  --emmc           Record choice on /boot on eMMC." >&2
}

if [ "$#" -gt 0 ] && [ "$1" = "--help" ]; then
  usage
  exit 0
fi

if [ "$(id -u)" -ne 0 ]; then
  echo "reform-display-config has to be run as root / using sudo."
  exit
fi

case "$(cat /proc/device-tree/model)" in
  "MNT Reform 2" | "MNT Reform 2 HDMI") : ;;
  *)
    echo "Sorry, reform-display-config is only supported on MNT Reform 2 with i.MX8MQ processor module."
    exit 1
    ;;
esac

CHOICE="$1"
BOOTPART="mmcblk1p1"
BOOTDISK="SD card"

if [ "--emmc" = "$1" ]; then
  CHOICE="$2"
  BOOTPART="mmcblk0p1"
  BOOTDISK="eMMC"

  if [ ! -b "/dev/mmcblk0p1" ]; then
    echo "/dev/mmcblk0p1 doesn't exist -- run reform-flash-rescue" >&2
    exit 1
  fi
fi

# even if u-boot-menu is not installed, create the config file so that once it
# is installed, it will immediately work without having to re-run
# reform-display-config
mkdir -p "/etc/u-boot-menu/conf.d/"
case "$CHOICE" in
  dual)
    echo "MNT Reform 2 HDMI" >/etc/flash-kernel/machine
    cat <<END >/etc/u-boot-menu/conf.d/reform.conf
# the content of this file is auto-generated by reform-display-config
U_BOOT_FDT=/freescale/imx8mq-mnt-reform2-hdmi.dtb
END
    ;;
  single)
    echo "MNT Reform 2" >/etc/flash-kernel/machine
    cat <<END >/etc/u-boot-menu/conf.d/reform.conf
# the content of this file is auto-generated by reform-display-config
U_BOOT_FDT=/freescale/imx8mq-mnt-reform2.dtb
END
    ;;
  *)
    echo "Usage: "
    echo "  reform-display-config dual                Select dual-display support (internal + HDMI)."
    echo "  reform-display-config single              Select only internal display (turns off HDMI)."
    echo "  reform-display-config --emmc dual|single  Record choice on eMMC."
    exit
    ;;
esac

if ! findmnt --noheadings --source /dev/$BOOTPART --mountpoint /boot >/dev/null; then
  echo "Assuming boot files are on $BOOTDISK, but your system doesn't have it mounted." >&2
  echo "Mounting /dev/$BOOTPART on /boot." >&2

  if mountpoint --quiet /boot; then
    echo "/boot already has something mounted on it" >&2
    exit 1
  fi

  if [ -n "$(lsblk --noheadings --output=MOUNTPOINT /dev/$BOOTPART)" ]; then
    echo "/dev/$BOOTPART is still in use" >&2
    exit 1
  fi

  trap "umount /boot" EXIT INT TERM
  mount /dev/$BOOTPART /boot
fi

if command -v flash-kernel >/dev/null; then
  flash-kernel
fi
if command -v u-boot-update >/dev/null; then
  u-boot-update
fi

if [ "--emmc" = "$1" ]; then
  echo "Your /boot partition is on emmc (/dev/$BOOTPART)." >&2
else
  echo "Your /boot partition is on your SD-Card (/dev/$BOOTPART)." >&2
fi

echo "Restart MNT Reform (type: reboot) after saving your work to activate the changes."

# exit trap will automatically unmount /boot if necessary once the script exits
