#!/bin/ksh

# Copyright 2005 Sun Microsystems, Inc.  All rights reserved. 
# Use is subject to license terms.
#pragma ident	"@(#)mkmenu.sh	1.1	05/09/06 SMI"

entry()
{
	root=$1
	title=$2

	grep -v ^# $GRUBMENU | grep $root > /dev/null
	if [ $? = 1 ] ; then
		printf "\ntitle $title\n" >> $GRUBMENU
		printf "\trootnoverify $root\n" >> $GRUBMENU
		printf "\tchainloader +1\n" >> $GRUBMENU
	fi
}

handle_fat()
{
	root=$3

	entry $root Windows
}

handle_ifs()
{
	dev=$1
	rdev=$2
	root=$3

	sig=`strings $rdev 2> /dev/null | head -1 | awk '{ print $1 }'`
	if [ "$sig" = "NTFS" ] ; then
		entry $root "Windows"
	else
		entry $root OS/2
	fi
}

handle_solaris()
{
	dev=$1
	rdev=$2
	root=$3

	tmp=/tmp/mnt$$
	mkdir $tmp

	for i in 0 1 3 4 5 6 7 ; do
		fs=`fstyp ${dev}s$i 2> /dev/null`
		if [ "$fs" = "ufs" ] ; then
			mount -o ro ${dev}s$i $tmp 2> /dev/null
			if [ $? != 0 ] ; then
				continue
			fi

			if [ -f $tmp/etc/release ] ; then
				if [ ! -f $tmp/platform/i86pc/multiboot ] &&
				    [ -f $tmp/boot/solaris/boot.bin ] ; then
					release=`head -1 $tmp/etc/release | \
					    sed "s/^[	 ]*//"`
					entry $root "$release"
				fi
			fi
			umount $tmp > /dev/null 2>&1
		fi
	done

	rmdir $tmp
}

handle_x86boot()
{
	dev=$1
	rdev=$2
	root=$3

	tmp=/tmp/mnt$$
	mkdir $tmp
	
	fs=`fstyp $rdev 2> /dev/null`
	device=""
	if [ "$fs" = "pcfs" ] ; then
		mount -o ro -F pcfs ${dev}p$partition $tmp 2> /dev/null
		device=`grep "^setprop bootpath " $tmp/solaris/bootenv.rc \
		    2> /dev/null | awk '{ print $3 }' | sed s/\'//`
		umount $tmp 2> /dev/null 2>&1
	fi

	if [ -z "$device" ] ; then
		rmdir $tmp
		return
	fi

	fs=`fstyp /devices/$device 2> /dev/null`
	if [ "$fs" = "ufs" ] ; then
		mount -o ro /devices/$device $tmp 2> /dev/null
		if [ $? != 0 ] ; then
			continue
		fi

		if [ -f $tmp/etc/release ] ; then
			if [ ! -f $tmp/platform/i86pc/multiboot ] &&
			    [ -f $tmp/boot/solaris/boot.bin ] ; then
				release=`head -1 $tmp/etc/release | \
				    sed "s/^[	 ]*//"`
				entry $root "$release"
			fi
		fi
		umount $tmp > /dev/null 2>&1
	fi

	rmdir $tmp
}

handle_diag()
{
	root=$3
	entry $root "Diagnostic Partition"
}

unhandled()
{
	disk=$1
	partition=$2
	root=$3
	id=$4

	grep $root $GRUBMENU > /dev/null
	if [ $? = 1 ] ; then
		printf "\n# Unknown partition of type $id found " >> $GRUBMENU
		printf "on $disk partition: $partition\n# It " >> $GRUBMENU
		printf "maps to the GRUB device: $root .\n" >> $GRUBMENU
	fi
}


if [ -f "$1" ] ; then
	GRUBMENU=$1
else
	printf "usage: $0 <grub menu file>\n" >&2
	exit 1
fi

if [ ! -f /var/run/solaris_grubdisk.map ] && 
    [ -x /boot/solaris/bin/create_diskmap ] ; then
	/boot/solaris/bin/create_diskmap
fi

for disk in /dev/rdsk/*p0 ; do
	typeset -i partition=1
	typeset -i gpart

	for id in `fdisk -W - $disk 2> /dev/null | grep -v "^*" | \
	    awk '{ print $1 }'` ; do
		dev=`echo $disk | sed s#/rdsk/#/dsk/# | sed s/p0$//`
		rdev=`echo $disk | sed s/p0$/p$partition/`
		gpart=$partition-1
		ctd=`basename $disk | sed s/p0$//`
		grep $ctd /var/run/solaris_grubdisk.map > /dev/null 2> /dev/null
		if [ $? = 0 ] ; then
			gdisk=`grep $ctd /var/run/solaris_grubdisk.map | \
			    awk '{ print $1 }'`
		else
			gdisk=0
		fi
		root="(hd$gdisk,$gpart)"

		case $id in
			7)	handle_ifs $dev $rdev $root ;;
			11)	handle_fat $dev $rdev $root ;;
			12)	handle_fat $dev $rdev $root ;;
			18)	handle_diag $dev $rdev $root ;;
			23)	handle_ifs $dev $rdev $root ;;
			28)	;; # hidden FAT32 must ignore
			130)	handle_solaris $dev $rdev $root ;;
			190)	handle_x86boot $dev $rdev $root ;;
			191)	handle_solaris $dev $rdev $root ;;
			222)	handle_diag $dev $rdev $root ;;
			*)	unhandled $disk $partition $root $id ;;
		esac

		partition=$partition+1
	done
done
