#!/bin/sh
# Copyright 2026 ANNE Media - https://anne.media
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# AM·OS installer prompt.

# root only
[ "$(id -u)" = "0" ] || return 0

# interactive terminal only
[ -t 0 ] || return 0

# once per boot
[ -f /run/amos-prompted ] && return 0
mkdir -p /run
touch /run/amos-prompted

# Find or mount the installation media
ISO_MNT=""
for c in /var/log/mount /mnt/cdrom /media/cdrom /mnt/sr0 /mnt; do
  if [ -d "$c/amos64" ]; then
    ISO_MNT="$c"
    break
  fi
done

if [ -z "$ISO_MNT" ] && [ -b /dev/sr0 ]; then
  mkdir -p /mnt/cdrom
  if mount -t iso9660 -o ro /dev/sr0 /mnt/cdrom 2>/dev/null; then
    ISO_MNT=/mnt/cdrom
  fi
fi

if [ -z "$ISO_MNT" ] && [ -b /dev/cdrom ]; then
  mkdir -p /mnt/cdrom
  if mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom 2>/dev/null; then
    ISO_MNT=/mnt/cdrom
  fi
fi

if [ -z "$ISO_MNT" ]; then
  echo
  echo "AM·OS: installation media not found."
  echo "       Mount it manually, then run <mount>/install.sh"
  echo
  return 0
fi

INSTALLER="$ISO_MNT/install.sh"
if [ ! -f "$INSTALLER" ]; then
  echo
  echo "AM·OS: media mounted at $ISO_MNT, but install.sh is missing."
  echo
  return 0
fi

echo
echo "================================================================"
echo "  AM·OS installer"
echo
echo "  Installation media: $ISO_MNT"
echo "  This will WIPE /dev/sda and install AM·OS to it."
echo
echo "  Press Enter to continue."
echo "  Press Ctrl-C to cancel and get a shell."
echo "================================================================"
echo

if read -r _; then
  "$INSTALLER"
  echo
  echo "================================================================"
  echo "  Installation complete."
  echo "================================================================"
  echo
  printf "  Power off now? [Y/n] "
  read -r ans
  case "${ans:-Y}" in
    [Nn]*) echo; echo "  Run 'poweroff' or 'reboot' when ready."; echo ;;
    *)     echo; echo "  Powering off..."; sync; exec /sbin/poweroff ;;
  esac
else
  echo
  echo "Cancelled. Run $INSTALLER when you're ready."
  echo
fi