notice_uboot() {
    # Provide the update instructions
    echo "New version of U-Boot firmware can be flashed to your microSD card (mmcblk1)"
    echo "or eMMC module (mmcblk2).  You can do that by running these two commands:"
    echo "# dd if=/boot/idbloader.img of=/dev/mmcblkX seek=64 conv=notrunc,fsync"
    echo "# dd if=/boot/u-boot.itb of=/dev/mmcblkX seek=16384 conv=notrunc,fsync"
}

create_config() {
    # Create new extlinux.conf
    if [ ! -f /boot/extlinux/extlinux.conf ]; then
        echo "Creating new /boot/extlinux/extlinux.conf..."
        mkdir -p /boot/extlinux
        echo "LABEL Manjaro ARM
KERNEL /Image
FDT /dtbs/rockchip/rk3399-pinebook-pro.dtb
APPEND initrd=/initramfs-linux.img console=ttyS2,1500000 root=LABEL=ROOT_MNJRO rw rootwait audit=0 splash plymouth.ignore-serial-consoles" \
            > /boot/extlinux/extlinux.conf
    fi
}

update_config() {
    # Reconfigure extlinux.conf if needed
    if [ -f /boot/extlinux/extlinux.conf ]; then
        if grep -q -F 'bootsplash.bootfile=bootsplash-themes/manjaro/bootsplash' /boot/extlinux/extlinux.conf; then
            echo "Reconfiguring /boot/extlinux/extlinux.conf..."
            sed -i s!"bootsplash.bootfile=bootsplash-themes/manjaro/bootsplash"!"splash plymouth.ignore-serial-consoles"!g \
                /boot/extlinux/extlinux.conf
        else
            echo "Keeping current /boot/extlinux/extlinux.conf..."
        fi
    fi
}

post_install() {
    # Provide the instructions
    notice_uboot

    if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
        echo "Picking up /boot/extlinux/extlinux.conf.pacsave..."
        mv -f /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
    fi

    if [ -f /boot/extlinux/extlinux.conf ]; then
        update_config
    else
        create_config
    fi
}

post_upgrade() {
    # Provide the instructions
    notice_uboot

    if [ -f /boot/extlinux/extlinux.conf.pacsave ]; then
        echo "Picking up /boot/extlinux/extlinux.conf.pacsave..."
        mv -f /boot/extlinux/extlinux.conf.pacsave /boot/extlinux/extlinux.conf
    fi

    if [ -f /boot/extlinux/extlinux.conf ]; then
        update_config
    else
        create_config
    fi
}
