#!/bin/sh
# Fallback in case nothing else works. Look for vmlinu[xz] file in root and
# /boot, see if there is a matching initrd, and wing it.
. /usr/share/os-prober/common.sh
set -e

partition="$1"
bootpart="$2"
mpoint="$3"
type="$4"

mappedpartition=$(mapdevfs "$partition" 2>/dev/null) || mappedpartition="$partition"

exitcode=1
for kernfile in /vmlinuz /vmlinux /boot/vmlinuz /boot/vmlinux /boot/vmlinuz* \
	        /boot/vmlinux* /vmlinuz* /vmlinux*; do
	if echo "$kernfile" | grep -q boot/; then
		kernbootpart=$bootpart
	else
		kernbootpart=$partition
	fi
	if [ -f "$mpoint$kernfile" ]; then
		initrdname=$(echo "$kernfile" | sed "s/vmlinu[zx]/initrd\*/")
		foundinitrd=0
		for initrd in $(eval ls $mpoint$initrdname 2>/dev/null); do
			if [ -f "$initrd" ]; then
				initrd=$(echo "$initrd" | sed "s!^$mpoint!!")
				result "$partition:$kernbootpart::$kernfile:$initrd:root=$mappedpartition"
				exitcode=0
				foundinitrd=1
			fi
		done
		if [ "$foundinitrd" = 0 ]; then
			result "$partition:$kernbootpart::$kernfile::root=$mappedpartition"
			exitcode=0
		fi
	fi
done
exit $exitcode
