#!/bin/sh

BOOT="LNX boot KERNEL"
INITRD="/usr/lib/ubuntu-touch-generic-initrd/initrd.img-touch"

# we dont want to run if FLASH_KERNEL_SKIP is set, the ubuntu
# image build system uses this
if [ -n "$FLASH_KERNEL_SKIP" ]; then
    exit 0
fi

# if there is "recovery" on the kernel cmdline, we are most
# likely running in dual boot mode, do nothing then, else we
# trash the android boot.img
grep -q recovery /proc/cmdline && exit 0

case "$1" in
    /*)
        [ ! -e "$1" ] && echo "E: No initrd at $1" && exit 1
        INITRD="$1"
    ;;
    -h|--help)
        echo "usage: $(basename $0) [path to initrd]"
        exit 0
    ;;
esac

for i in $BOOT; do
    path=$(find /dev -name "$i"|grep disk| head -1)
    [ -n "$path" ] && break
done

[ -z "$path" ] && echo "E: No boot partition found !" && exit 1

abootimg -u $path -r $INITRD
