#!/bin/sh

kver="$(rpm -qa 'kernel-image*' \
	    --qf '%{installtime} %{version}-%{name}-%{release}\n' \
	| sort -n \
	| tail -n 1 \
	| cut -f 2 -d ' ' \
	| sed 's/kernel-image-//')"

[ -h /boot/vmlinuz ] || ln -s vmlinuz-$kver /boot/vmlinuz
[ -h /boot/initrd.img ] || ln -s initrd-$kver.img /boot/initrd.img

# NB: mkinitrd-3.0.16+ has rootsubdir support which is critical
#     for TWRP based ARM "VE" chroots (e.g. for nexus7);
#     should be implemented in make-initrd either

### rootfs type should become configurable
if rpm -q mkinitrd; then
	mkinitrd --with ext4 -f /boot/initrd-$kver.img "$kver"
fi

# NB: if the adresses ever become different this "API" _will_ change
# NB: initrd compression (-C) might change, armadaxp handles gzip
umk() {
	[ $# = 4 ] || exit 1
	cd /boot
	umkimage \
		-A arm \
		-O linux \
		-T "$1" \
		-C none \
		-a "$2" \
		-e "$2" \
		-n "`readlink "$3"`" \
		-d "$3" \
		"$4"
}

### the addresses will become variables some day probably
if rpm -q uboot-tools; then
	umk kernel 0x00008000 vmlinuz uimage
	umk ramdisk 0x00800000 initrd.img uinitrd
	if [ -s boot ]; then
		umk script 0 boot boot.scr
	fi
fi

: