notice_uboot() {
    echo "A new U-Boot version needs to be flashed our install drive. Please use lsblk to determine your drive, before proceeding."
    echo "You can do this by running:"
    echo "# dd if=/boot/u-boot.bin of=/dev/mmcblkX conv=fsync,notrunc bs=442 count=1"
    echo "# dd if=/boot/u-boot.bin of=/dev/mmcblkX conv=fsync,notrunc bs=512 skip=1 seek=1"
}

create_config() {
  mkdir -p /boot/extlinux
  echo "LABEL Manjaro ARM
KERNEL /Image
FDT /dtbs/amlogic/meson-sm1-khadas-vim3l.dtb
APPEND initrd=/initramfs-linux.img root=LABEL=ROOT_MNJRO rootflags=data=writeback rw console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0 quiet splash plymouth.ignore-serial-consoles" \
    > /boot/extlinux/extlinux.conf
}

post_install() {
  notice_uboot
  if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
    mv /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
  fi
  if [ -f /boot/extlinux/extlinux.conf ]; then
    echo "Keeping old extlinux.conf file..."
  else
    create_config
  fi
}

post_upgrade() {
  notice_uboot
  if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
    mv /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
  fi
  if [ -f /boot/extlinux/extlinux.conf ]; then
    echo "Keeping old extlinux.conf file..."
  else
    create_config
  fi
}

post_remove() {
  rm -f /boot/extlinux/extlinux.conf
}
