#!/bin/bash

# grub.brew
# Build GRUB for RISC OS
# by Stuart Winter <mozes@armedslack.org>
# Please see changelog.txt for revisions to this package's build script history.

PACKAGE=grub-riscos
#VERSION=2_cvs
VERSION=1.91
BUILD=1
# This is the last Kernel that has a working 'sleep':
#KERNELVERSION=2.6.14.6
KERNELVERSION=2.6.19.1
# This is useful for when building multiple versions with test Kernels:
#PKGFILE=slackb-$PACKAGE-$VERSION-$BUILD-linux$KERNELVERSION-$( date "+%d-%b-%Y" ).zip 
# This is the name of the release version:
PKGFILE=slackb.zip

# Drag in the build kit:
source /usr/share/slackdev/buildkit.sh

# We require Ruby since it's used in some of the makefiles
slackcheckpkgdeps ruby || installpkg $PKGSTORE/d/ruby-*.tgz
slackcheckpkgdeps ruby || exit 99

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
#tar jxvvf $PORTCWD/sources/grub2-20051007.tar.bz2
tar zxvvf $PORTCWD/sources/grub-$VERSION.tar.gz
cd grub*
slackhousekeeping

# Apply Timothy Baldwin's Acorn/RISC OS patches:
#zcat $PORTCWD/sources/grub2-patch3.diff.gz           | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch4.diff.gz           | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch5.diff.gz           | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch6.diff.gz           | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch6-remainder.diff.gz | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch7.diff.gz           | patch -p1  --verbose || failpatch
#zcat $PORTCWD/sources/grub2-patch8.diff.gz           | patch -p1  --verbose || failpatch
## Debugging stuff:
##zcat $PORTCWD/tim/grub2-patch_Stuart_Winter1.diff.gz | patch -p1 --verbose || failpatch
# Updated 03-May-2006:
zcat $PORTCWD/sources/grub2-patch17.diff.gz   | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch11.diff.gz   | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch-mg2.diff.gz | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch14c.diff.gz  | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch12c.diff.gz  | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch3c.diff.gz   | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch6c.diff.gz   | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch10b.diff.gz  | patch -p1  --verbose || failpatch
zcat $PORTCWD/sources/grub2-patch18.diff.gz   | patch -p1  --verbose || failpatch
# Fix the GRUB menu text - otherwise it says "use the eys to.."
patch --verbose -p0 < $PORTCWD/sources/cursorkeysmessage.patch || failpatch
# Remove a space from 'Booting ' <menuname>''. Details details :)
patch --verbose -p0 < $PORTCWD/sources/menu-booting.diff       || failpatch

# Configure:
sh autogen.sh
./configure \
   --prefix=/ \
   arm || failconfig

# Build:
make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Copy the Utils - useful for Debugging GRUB on RISC OS:
# Uncomment these two lines if anybody ever needs a debugging version of
# 'slackb'.
#mkdir -p $PKG/grub-debug-utils
#cp -fa util/arm/* $PKG/grub-debug-utils

# Make the directory structure for packaging under RISC OS:
cd $PKG

# Move the good stuff somewhere safe:
mv share/grub/arm-RISC_OS $TMPBUILD

# Delete the junk:
rm -rf share sbin bin

# Copy the pre-made RISC OS tree:
cp -favv $PORTCWD/riscos-tree/* .

# Copy GRUB into the installer and boot loader applications:
cp -favv $TMPBUILD/arm-RISC_OS '!SlackBt/!GRUB'

# Install the pre-made ARMedslack config files 
# and setting the RISC OS file type as &fff ('Text'):
mv -f slackboot.grub.cfg '!SlackBt/!GRUB/grub.cfg,fff'

# Grab the latest RiscPC Kernel from the Kernel package in the a/ series
# and install it into the RISC OS applications:
# We use 'Unix Ex' (&FE6) as the RISC OS file type.  We probably shouldn't
# since the Kernel isn't a UNIX executable, but it's the closest thing I 
# could find in the standard RISC OS 4.02 File$Type list that described the
# type of file.
( set -x
  tar-1.13 vvfOxz $PKGSTORE/a/kernel_riscpc-$KERNELVERSION-*.tgz 'boot/zImage-*' > '!SlackBt/zImage,fe6' )

# Copy the latest 'color' initrd image into the installer application:
cp -vvfa $PORTCWD/../../../rootdisks/color.gz '!SlackBt/'

# Create RISC OS zip file: this isn't the name we ship - we ship 'slackb.zip' so that
# it fits into the 10 character pre-RISC OS 4 FileCore filename length limit.
# The long file name is just for reference when testing and to avoid overwriting 
# a working version of the bootware.
# Note the additional , option to zip: this is the patched zip which has
# support for RISC OS file types.
rm -f $PKGSTORE/../bootware/riscpc/$PKGFILE
zip -,r9 $PKGSTORE/../bootware/riscpc/$PKGFILE .

echo "Now boot into RISC OS and try $PKGFILE"

# EOF
