#!/bin/sh
# Detects Haiku on BeFS partitions.

. /usr/share/os-prober/common.sh

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

# Weed out stuff that doesn't apply to us
case "$type" in
	befs|befs_be) debug "$partition is a BeFS partition" ;;
	*) debug "$partition is not a BeFS partition: exiting"; exit 1 ;;
esac

if head -c 512 "$partition" | grep -qs "system.haiku_loader"; then
	debug "Stage 1 bootloader found"
else
	debug "Stage 1 bootloader not found: exiting"
	exit 1
fi

if system="$(item_in_dir "system" "$mpoint")" &&
	item_in_dir -q "haiku_loader" "$mpoint/$system" &&
	(item_in_dir -q "kernel_x86" "$mpoint/$system" ||
		item_in_dir -q "kernel_x86_64" "$mpoint/$system")
then
	debug "Stage 2 bootloader and kernel found"
	label="$(count_next_label Haiku)"
	result "$partition:Haiku:$label:chain"
	exit 0
else
	debug "Stage 2 bootloader and kernel not found: exiting"
	exit 1
fi
