#!/bin/sh
#
# Script for patch and build. See notes below.
#
# Usage:
#		sh compile
# or
#		./compile

if [ "$2" != "" ] || [ "$1" != "" -a "$1" != patchonly -a "$1" != untaronly -a "$1" != makedist ]; then
	echo "Usage:"
	echo "        [ chinese=1 ] [ scdrom=1 ] [ bioscdrom=1 ] $0"
	echo "        [ chinese=1 ] [ scdrom=1 ] [ bioscdrom=1 ] $0  untaronly"
	echo "        [ chinese=1 ] [ scdrom=1 ] [ bioscdrom=1 ] $0  patchonly"
	echo "        [ chinese=1 ] [ scdrom=1 ] [ bioscdrom=1 ] $0  makedist"
	exit 1
fi

GRUB_ver=grub-0.97

if [ "$chinese" = 1 ]; then
	COMPILE_WITH_chinese=1
else
	COMPILE_WITH_chinese=0
fi

if [ "$scdrom" = 1 ]; then
	COMPILE_WITH_scdrom=1
else
	COMPILE_WITH_scdrom=0
fi

if [ "$bioscdrom" = 1 ]; then
	COMPILE_WITH_bioscdrom=1
else
	COMPILE_WITH_bioscdrom=0
fi

function untar_gnu_grub()
{
	if [ -f ${GRUB_ver}.tar.gz ]; then
		tar -xzf ${GRUB_ver}.tar.gz || exit $?
	elif [ -f ../${GRUB_ver}.tar.gz ]; then
		tar -xzf ../${GRUB_ver}.tar.gz || exit $?
	elif [ -f ~/${GRUB_ver}.tar.gz ]; then
		tar -xzf ~/${GRUB_ver}.tar.gz || exit $?
	else
		echo file not found: ${GRUB_ver}.tar.gz
		echo You may download it from GNU GRUB site and place it in your home dir.
		exit 1
	fi
}

function apply_patches()
{
	echo
	echo Applying patch ${GRUB_ver}-patch1-startups
	echo
	patch -p1 < ../${GRUB_ver}-patch1-startups  || exit $?
	echo
	echo Applying patch ${GRUB_ver}-patch2-findroot
	echo
	patch -p1 < ../${GRUB_ver}-patch2-findroot  || exit $?
	echo
	echo Applying patch ${GRUB_ver}-patch3-ntfs
	echo
	patch -p1 < ../${GRUB_ver}-patch3-ntfs      || exit $?
	echo
	echo Applying patch ${GRUB_ver}-patch4-emulation
	echo
	patch -p1 < ../${GRUB_ver}-patch4-emulation || exit $?
	echo
	echo Applying patch ${GRUB_ver}-patch5-splash
	echo
	patch -p1 < ../${GRUB_ver}-patch5-splash    || exit $?
	if [ "$COMPILE_WITH_chinese" = 1 -o "$COMPILE_WITH_scdrom" = 1 -o "$COMPILE_WITH_bioscdrom" = 1 ]; then
		echo
		echo Applying patch ${GRUB_ver}-patch6-chinese
		echo
		patch -p1 < ../${GRUB_ver}-patch6-chinese    || exit $?
	fi
	
	if [ "$COMPILE_WITH_scdrom" = 1 -o "$COMPILE_WITH_bioscdrom" = 1 ]; then
		echo
		echo Applying patch ${GRUB_ver}-patch7-scdrom
		echo
		patch -p1 < ../${GRUB_ver}-patch7-scdrom    || exit $?
	fi

	if [ "$COMPILE_WITH_bioscdrom" = 1 ]; then 
		echo
		echo Applying patch ${GRUB_ver}-patch8-bioscdrom
		echo
		patch -p1 < ../${GRUB_ver}-patch8-bioscdrom || exit $?
	fi
}

if [ "$1" = "" ] && [ -e ${GRUB_ver} ]; then
	set makedist
fi

if [ "$1" != makedist ]; then

[ -f ${GRUB_ver}-patch1-startups  ] || exit $?
[ -f ${GRUB_ver}-patch2-findroot  ] || exit $?
[ -f ${GRUB_ver}-patch3-ntfs      ] || exit $?
[ -f ${GRUB_ver}-patch4-emulation ] || exit $?
[ -f ${GRUB_ver}-patch5-splash    ] || exit $?
[ -f ${GRUB_ver}-patch6-chinese   ] || exit $?
[ -f ${GRUB_ver}-patch7-scdrom    ] || exit $?
[ -f ${GRUB_ver}-patch8-bioscdrom ] || exit $?

if [ -e ${GRUB_ver} ]; then
	echo $GRUB_ver already exists. Please remove it first. exit...
	exit 1
fi

untar_gnu_grub

[ "$1" = untaronly ] && exit

cd ${GRUB_ver} || exit $?

apply_patches

[ "$1" = patchonly ] && exit

echo
echo Running aclocal ...
echo
aclocal  || exit $?
echo
echo Running autoconf ...
echo
autoconf || exit $?
echo
echo Running automake ...
echo
automake || exit $?
echo
echo Running ./configure ...
echo
#
# patch2-findroot now includes a preset menu (by Chris Semler. Thanks!)
# FINDROOT has merged into FIND. Example:
#
#	find --set-root /boot/grub/menu.lst
#
# This command will set the device which contains /boot/grub/menu.lst
# as the new root device.
#
./configure --enable-preset-menu=preset_menu.lst || ./configure || exit $?
echo
echo Running make ...
echo
make || exit $?

echo
echo make succeeded.
echo

# echo
# echo Now copying stage1, stage1.5 and stage2 files to boot/grub ...
# echo
#
# if ! cmp stage1/stage1 ../boot/grub/stage1 >&- 2>&- ; then
#	cp -f stage1/stage1 ../boot/grub/stage1
# fi
#
# cp -f stage2/*1_5 stage2/stage2 ../boot/grub/

echo
echo Copying grub.exe, grldr, grldr.mbr, bootlace.com to the top dir ...
echo

cp -f stage2/grub.exe stage2/grldr stage2/grldr.mbr stage2/bootlace.com ../

echo
echo "Now apply the \"chinese\" and \"scdrom\" patches..."
echo

if [ "$COMPILE_WITH_scdrom" = 1 -o "$COMPILE_WITH_bioscdrom" = 1 ]; then

	echo
	echo "The chinese and scdrom patches already applied."
	echo
	
#	if ! cmp stage1/stage1 ../chinese/boot/grub/stage1 >&- 2>&- ; then
#		cp -f stage1/stage1 ../chinese/boot/grub/stage1
#	fi
#	
#	cp -f stage2/*1_5 stage2/stage2 ../chinese/boot/grub/

	cp -f stage2/grub.exe stage2/grldr ../chinese/

	echo
	echo All done.
	echo
	
	exit

fi

cd ..

rm -rf ${GRUB_ver}_tmp
cp -a ${GRUB_ver} ${GRUB_ver}_tmp

cd ${GRUB_ver}_tmp

if [ "$COMPILE_WITH_chinese" != 1 ]; then
	if ! patch -p1 < ../${GRUB_ver}-patch6-chinese ; then
		echo "failed patch ${GRUB_ver}-patch6-chinese"
		exit 1
	fi
fi

if ! patch -p1 < ../${GRUB_ver}-patch7-scdrom ; then
	echo "failed patch ${GRUB_ver}-patch7-scdrom"
	exit 1
fi

make || exit $?

# if ! cmp stage1/stage1 ../chinese/boot/grub/stage1 >&- 2>&- ; then
#	cp -f stage1/stage1 ../chinese/boot/grub/stage1
# fi
#
# cp -f stage2/*1_5 stage2/stage2 ../chinese/boot/grub/

cp -f stage2/grub.exe stage2/grldr ../chinese/

echo
echo All done.
echo

exit

else
	# makedist

	if [ ! -d ${GRUB_ver} ]; then
		echo "The dir ${GRUB_ver} does not exist. Cannot compile with makedist. exit"
		exit 1
	fi

	rm -rf new1.diff new2.diff
	rm -rf ${GRUB_ver}.old
	rm -rf *.old *~
	mv ${GRUB_ver} ${GRUB_ver}.old || exit $?

	untar_gnu_grub

	cd ${GRUB_ver} || exit $?

	apply_patches

	cd ..
	cp -a ${GRUB_ver} ${GRUB_ver}.new1
	cp -a ${GRUB_ver} ${GRUB_ver}.new2

	new1=0
	new2=0
	for f in dosstart.S grldrstart.S start_eltorito.S start.S mbrstart.S bootlacestart.S edd.S video.S bootlace.inc a20.inc linux/autoconf.h linux/compile.h linux/config.h linux/edd.h linux/linkage.h linux/version.h asm/boot.h asm/e820.h asm/linkage.h asm/page.h asm/segment.h asm/setup.h; do
		if ! cmp ${GRUB_ver}.old/stage2/$f ${GRUB_ver}.new1/stage2/$f; then
			new1=1
			cp -af ${GRUB_ver}.old/stage2/$f ${GRUB_ver}.new1/stage2/$f || exit $?
		fi
	done
	
	for f in asm.S apm.S nbloader.S pxeloader.S setjmp.S; do
		if ! cmp ${GRUB_ver}.old/stage2/$f ${GRUB_ver}.new2/stage2/$f; then
			new2=1
			cp -af ${GRUB_ver}.old/stage2/$f ${GRUB_ver}.new2/stage2/$f || exit $?
		fi
	done

	cd ${GRUB_ver}.old/stage2/ || exit $?
	
	for f in *.c *.h; do
		if [ "$f" != stage2_size.h ] && ! cmp $f ../../${GRUB_ver}.new2/stage2/$f; then
			new2=1
			cp -af $f ../../${GRUB_ver}.new2/stage2/$f || exit $?
		fi
	done

	cd ../../

	cd ${GRUB_ver}.old/grub/ || exit $?
	
	for f in *.c; do
		if ! cmp $f ../../${GRUB_ver}.new2/grub/$f; then
			new2=1
			cp -af $f ../../${GRUB_ver}.new2/grub/$f || exit $?
		fi
	done

	cd ../../

	if [ "$new1" = 0 ] && [ "$new2" = 0 ]; then
		rm -rf ${GRUB_ver} ${GRUB_ver}.new1 ${GRUB_ver}.new2
		mv ${GRUB_ver}.old ${GRUB_ver}
		echo no changes, exit normally
		exit
	fi

	[ "$new1" != 0 ] && diff -Naur ${GRUB_ver} ${GRUB_ver}.new1 > new1.diff
	[ "$new2" != 0 ] && diff -Naur ${GRUB_ver} ${GRUB_ver}.new2 > new2.diff
	
	rm -rf ${GRUB_ver} ${GRUB_ver}.new1 ${GRUB_ver}.new2
	
	untar_gnu_grub
	
	s=${GRUB_ver}
	for f in 1-startups 2-findroot 3-ntfs 4-emulation 5-splash 6-chinese 7-scdrom 8-bioscdrom ; do
		rm -rf ${GRUB_ver}_${f#*-}
		cp -a $s ${GRUB_ver}_${f#*-} || exit $?
		cd ${GRUB_ver}_${f#*-}
		echo
		echo "Applying ${GRUB_ver}-patch$f :"
		echo
		patch -p1 < ../${GRUB_ver}-patch$f
		if [ "$f" = "1-startups"  ]; then
			[ -f ../new1.diff ] && patch -p1 < ../new1.diff
		elif [ "$f" = "4-emulation"  ]; then
			[ -f ../new2.diff ] && patch -p1 < ../new2.diff
		fi
		if ls */*.rej 2>&- ; then
			echo
			echo "The patch is rejected: " */*.rej
			echo
			exit 1
		fi
		rm -f */*.orig
		cd ..
		
		s=${GRUB_ver}_${f#*-}
	done
	
	rm -rf new1.diff new2.diff
	
	s=${GRUB_ver}
	for f in 1-startups 2-findroot 3-ntfs 4-emulation 5-splash 6-chinese 7-scdrom 8-bioscdrom ; do
		mv -f ${GRUB_ver}-patch$f ${GRUB_ver}-patch$f.old || exit
		diff -Naur $s ${GRUB_ver}_${f#*-} > ${GRUB_ver}-patch$f
		rm -rf $s
		s=${GRUB_ver}_${f#*-}
	done
	rm -rf $s
	
	if ! sh compile; then
		echo "Error occurred while compile."
		exit 1
	fi

	while read w1 w2 w3 w4; do
		[ "$w1" = "Current" ] && [ "$w2" = "Version:" ] && break
	done < README.txt

	if [ "$w1" != "Current" -o "$w2" != "Version:" ]; then
		echo "Error: README.txt has no \"Current Version:\" line."
		exit 1
	fi
	# strip the trailing CR
	w3=${w3%%[^0-9a-zA-Z_.+-]*}
	
	rm -rf .old .grub_for_dos-$w3 old grub_for_dos-$w3* || exit 1
	rm -rf new1.diff new2.diff
	mkdir .old 2>&-
	mv ${GRUB_ver}*/ .old/ || exit 1
	mv -f tt* .old/ 2>&-
	mv -f ${GRUB_ver}.tar.gz .old/ 2>&-

	mv -f ${GRUB_ver}-patch*.old .old || exit 1
	mkdir .grub_for_dos-$w3/
	cp -a * .grub_for_dos-$w3/
	mv -f .grub_for_dos-$w3 grub_for_dos-$w3
	mv -f .old old
	tar -czf grub_for_dos-$w3.tar.gz grub_for_dos-$w3
	cp -f grub_for_dos-$w3.tar.gz ~/
	zip -q -r grub_for_dos-$w3.zip grub_for_dos-$w3 2>&-
	cp -f grub_for_dos-$w3.zip ~/ 2>&-
	mv old/${GRUB_ver}*/ .
	mv old/${GRUB_ver}.tar.gz . 2>&-
	cp -f grub.exe grldr grldr.mbr bootlace.com ~/
	
fi
exit

patch1 is for booting GRUB via DOS/NT/LILO/GRUB/KEXEC.
patch2 is optional. It adds an option '--set-root' for FIND after patch1.
patch3 is optional. It adds NTFS support after patch2.
patch4 is optional. It adds disk emulation after patch3.
patch5 is optional. It adds splash image support after patch4.
patch6 is optional. It adds Chinese support after patch5.
patch7 is experimental. It adds atapi cdrom support after patch6.
patch8 is initiative. It adds bios cdrom routine support after patch7.

This assumes autoconf-2.59(or later) and automake-1.9.4(or later) are installed on the system.

Also gcc-3.4.3 or later must be installed on the system.
Note: someone reports gcc-4.0.0 cannot compile grub4dos. Use 3.4.3 please.
Note: gcc-4 is now ok by VirusCamp. Thanks!

For more information on disk-emulation and DOS/NT/LILO/GRUB/KEXEC boot-method, go to GRUB for DOS pages at
http://newdos.yginfo.net/grubdos.htm
http://grub4dos.sourceforge.net/
http://sarovar.org/projects/grub4dos/
http://grub4dos.jot.com/
http://grub4dos.freespaces.com/

