#!/bin/sh
# Detects all Microsoft OSes on a collection of partitions.

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

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

# This file is for UEFI platform only
if [ ! -d /sys/firmware/efi ]; then
	debug "Not on UEFI platform"
	exit 1
fi

# Weed out stuff that doesn't apply to us
case "$type" in
	vfat) debug "$1 is a FAT32 partition" ;;
	msdos) debug "$1 is a FAT16 partition" ;;
	*) debug "$1 is not a ESP partition: exiting"; exit 1 ;;
esac

# FIXME we should check that partition is actually ESP; how to do it?

efi=$(item_in_dir efi "$mpoint")
if [ -z "$efi" ]; then
	debug "$mpoint is not a ESP partition; exiting"
	exit 1
fi

ret=1
for test in /usr/lib/os-probes/mounted/efi/*; do
	debug "running subtest $test"
	if [ -f "$test" ] && [ -x "$test" ]; then
		entry=$("$test" "$mpoint/$efi")
		if [ -n "$entry" ]; then
			debug "os found by subtest $test"
			ret=0
			result "${partition}@/$efi/${entry}:efi"
		fi
	fi
done

exit $ret
