#!/bin/bash
# qemu.build
# Build qemu for chrooting into an ARMedslack installation
# <stuart@armedslack.org>

CWD=$PWD
TMP=/tmp/build-qemu
PKG=/qemu-arm      # this is the location in which qemu will be installed inside the chroot
AS=/mnt/armedslack # this is where ARMedslack will be installed

rm -rf $TMP $PKG
mkdir -p $TMP

# Extract the CVS source:
cd $TMP
#tar jxvvf $CWD/sources/qemu*bz2
tar zxvvf $CWD/sources/qemu-0.8.0.tar.gz
cd qemu*
chown -R root.root .

# I thought I should adjust qemu to think that it is armv3 but
# I must have been smoking crack when I thought about this because
# the RiscPC *is* armv4l! (it just gets its knickers in a twist when
# you try and use armv4l instructions).
#fgrep -lr -- 'armv4l' . | xargs sed -i 's?armv4l?armv3l?g'

# Fix uname syscall. This is important because otherwise things get confused
# and think they're compiling for x86 rather than ARM.
#patch -p0 < $CWD/sources/uname.patch

# Fix signal handling:
#patch -p0 < $CWD/sources/qemu-signal.patch

# When chrooting into an installation binfmt_misc uses 
# relative pathnames to the chroot (/mnt/armedslack is / as far as
# binfmt_misc is concerned).
# I like putting the qemu-arm directory in / as it makes it easy to find :-)
# I should also make this into a package so I don't need to rebuild it every time.
./configure \
   --target-list=arm-user \
   --static \
   --prefix=$PKG \
   --interp-prefix=/

# Build:
make

# Install:
make install

# Delete any existing qemu installation inside our ARMedslack chroot:
cd $AS
rm -rf qemu-arm
# Copy the installed stuff into the ARMedslack chroot:
cp -a $PKG .
# We don't need it on our host x86 system - it was only there so that
# it'd be installed with the correct paths relative to the 'root' 
# in our ARMedslack chroot:
rm -rf $PKG
