flash_instructions() {
  echo "# dd if=/boot/u-boot/images/MLO of=/dev/mmcblk0 count=1 seek=1 conv=notrunc bs=128k"
  echo "# dd if=/boot/u-boot/images/u-boot.img of=/dev/mmcblk0 count=6 seek=2 conv=notrunc bs=128k"
}

extlinux_warning() {
  echo "==> WARNING: Remember to set up your kernels in /boot/extlinux/extlinux.conf"
}

flash_uboot() {
  root=$(mount | awk '/ on \/ / { print $1; }')
  if [[ $root =~ ^/dev/mmcblk.*$ ]]; then
    root=${root:0:12}
    echo "A new U-Boot version needs to be flashed onto $root."
    echo "Do you want to do this now? [y|N]"
    read -r shouldwe
    if [[ $shouldwe =~ ^([yY][eE][sS]|[yY])$ ]]; then
      dd if=/boot/u-boot/images/MLO of=$root bs=128k seek=1 count=1 conv=notrunc
      dd if=/boot/u-boot/images/u-boot.img of=$root bs=128k seek=2 count=6 conv=notrunc
    else
      echo "You can do this later by running:"
      flash_instructions
    fi
  else
    echo "Flash the new U-Boot version onto your boot device. For example:"
    flash_instructions
  fi
  extlinux_warning
}

post_install() {
  flash_uboot
}

post_upgrade() {
  flash_uboot
}
