1#!/bin/sh
2# Detects bootable Haiku OS on BeFS partitions and FUSE mounted BeFS too.
3# If it doesn't find anything, try mounting the BeFS volumes in Linux.
4# Discussion of improvements and development history at
5# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732696
6# Adapted from version 42 (dated 20150811) updated by Jeroen Oortwijn at
7# https://bazaar.launchpad.net/~idefix/ubuntu/trusty/os-prober/HaikuPM/files/head:/os-probes/mounted/x86
8# Latest version now at https://git.haiku-os.org/haiku/tree/3rdparty/os_probe
9
10. /usr/share/os-prober/common.sh
11
12partition="$1"
13mpoint="$2"
14type="$3"
15
16# Weed out stuff that doesn't apply to us, needs to be a Be file system.
17case "$type" in
18	bfs|befs) debug "$partition is a BeFS partition." ;;
19	fuse|fuseblk) debug "$partition is a FUSE partition, maybe with BeFS on it." ; mpoint="$mpoint/myfs" ;;
20	*) exit 1 ;; # Be quiet and just quit for irrelevant partitions.
21esac
22
23if head -c 512 "$partition" | grep -qs "haiku_loader"; then
24	debug "Haiku stage 1 bootloader found."
25else
26	debug "Haiku stage 1 bootloader not found: exiting"
27	exit 1
28fi
29
30if system="$(item_in_dir "system" "$mpoint")" &&
31	packages="$(item_in_dir "packages" "$mpoint/$system")" &&
32		item_in_dir -q "haiku_loader-r[0-9].*\.hpkg" "$mpoint/$system/$packages" &&
33		rev="$(item_in_dir "haiku-r[0-9].*\.hpkg" "$mpoint/$system/$packages")"
34then
35	debug "Haiku Package Manager stage 2 bootloader and kernel ($rev) found."
36	label="$(count_next_label Haiku)"
37	# Convert kernel file name like haiku-r1~beta3_hrev55181_56-1-x86_64.hpkg
38	# or haiku-r1~alpha4_pm_hrev49856-1-x86_gcc2.hpkg into a readable version,
39	# like "Haiku R1 beta3 (hrev55181_56) 64".  Seems to use a travelling space
40	# technique in the sed stream editor.
41	rev="$(echo "$rev" | sed 's/haiku-//;s/^\(r[0-9]\+\)./\U\1\E /;s/ \([a-z]\+[0-9]\+\)[_-]/ \1 /;s/ [a-z]*_\?\(hrev[0-9_]\+\)\+-/ (\1) /;s/) [^_]\+_\([a-z0-9]\+\)\.hpkg/) \1 /;s/[^ ]\+.hpkg//;s/ $//')"
42	long="Haiku $rev"
43	result "$partition:$long:$label:chain"
44	exit 0
45elif system="$(item_in_dir "system" "$mpoint")" &&
46	item_in_dir -q "haiku_loader" "$mpoint/$system" &&
47	item_in_dir -q "kernel_.*" "$mpoint/$system"
48then
49	debug "Older non-package manager Haiku stage 2 bootloader and kernel found."
50	label="$(count_next_label Haiku)"
51	result "$partition:Haiku:$label:chain"
52	exit 0
53else
54	debug "Haiku stage 2 bootloader and kernel not found: exiting"
55	exit 1
56fi
57