#!/bin/sh

BOOT="LNX boot KERNEL"
KERNEL="/boot/vmlinuz-$(uname -r)"

# 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 kernel at $1" && exit 1
        KERNEL="$1"
    ;;
    -h|--help)
        echo "usage: $(basename $0) [path to kernel]"
        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
[ ! -e "$KERNEL" ] && echo "E: No kernel at $KERNEL please provide a proper path" && exit 1

abootimg -u $path -k $KERNEL
