#!/bin/bash

INSTALLER_UUID=fabe1d65-a675-421e-a7ca-dd56ed4e543b
INSTALLERPART=`blkid -U $INSTALLER_UUID`

# load the installer kernel and start it
start_installer() {
  rae-itx-lcd 1= "Launching" 2= "installer"

  # load live kernel and initrd
  mount UUID=${INSTALLER_UUID} /mnt

  # load the live kernel
  kexec -l /mnt/vmlinuz --initrd=/mnt/initrd.img --append="root=UUID=${INSTALLER_UUID} ro net.ifnames=0 biosdevname=0 console=tty0 console=ttyS0,115200"

  # Unmount live partition
  umount UUID=${INSTALLER_UUID}

  # Go. Boot to installer image!
  kexec -e
}

# check if the installer is present
if [[ -n "$INSTALLERPART" ]]; then
  echo "Installer found on $INSTALLERPART"
  rae-itx-lcd 1= "Installer found" 2= "  on $INSTALLERPART"
  beep -f 2093  -l 200 -r 6
  sleep 2
  start_installer

  echo "[`date`] Critical ERROR: Installer found, but not bootable!"
  rae-itx-lcd 1= "Error launching" 2= "installer"
  exit 1
fi
