#!/bin/sh
#
# This script is based on:
#  InsQEMU - version 0.1 (2005-12-15) by
#    Nando Florestan -- http://oui.com.br/n (nandoflorestan@gmail.com)

# Script configuration
# ====================
BASE_DIR=/tmp/qemu-cvs
KQEMUVERSION="kqemu-1.3.0pre5"

echo "=> Installing the dependencies..."
# Ubuntu Breezy 5.10 and Debian Sid come with GCC 4.0 by default, but
# Qemu 0.72 will not compile with GCC 4.0 so we need to install GCC 3.4
# The SDL packages should provide sound support
# zlib is a compression library
# checkinstall is for generating .deb packages
sudo apt-get install gcc-3.4 g++-3.4 libsdl1.2debian libsdl1.2debian-all \
  build-essential libsdl1.2-dev zlib1g-dev checkinstall \
  texi2html cvs linux-headers-`uname -r` || exit 2

mkdir -p $BASE_DIR
cd $BASE_DIR || exit 1

###wget -c "http://fabrice.bellard.free.fr/qemu/${KQEMUVERSION}.tar.gz"

if [ -d "qemu" ]; then
  echo "=> I'm assuming you've already fetched the cvs sources previously."
  echo "   If not, then delete `pwd`/qemu"
  cd qemu
  ###&& cvs update
else
  cvs -z3 -d:pserver:anonymous@cvs.savannah.nongnu.org:/sources/qemu co qemu
  cd qemu
fi

echo "=> Cleaning source tree..."
###make clean

echo "=> Configuring..."
###sh ./configure --prefix=/usr --cc=gcc-3.4 --host-cc=gcc-3.4 || exit 13

echo "=> Start building..."
###make

if [ $? != 0 ]; then
	echo "PROBLEM IN MAKE: ${EXITCODE}"
	exit 14
fi

echo "=> removing qemu package if it is installed..."
###sudo apt-get remove qemu  || exit 15

echo "=> Installing the new qemu package..."
###sudo checkinstall -y --pkgname=qemu --pkgversion=$(cat VERSION) --pkgrelease=1 \
#  --pkglicense=Restricted --pkggroup "Miscellaneous - Text Based" \
#  --pkgsource=http://fabrice.bellard.free.fr/qemu/kqemu-1.3.0pre5.tar.gz || exit 18

cd ..

if [ -d "$KQEMUVERSION" ]; then
  echo "=> I'm assuming that you've already extracted KQemu."
  echo "   If not, then delete `pwd`/$KQEMUVERSION"
else
  tar xzf $KQEMUVERSION.tar.gz
fi

cd $KQEMUVERSION && \
  make clean && ./configure && make && sudo make install

echo "=> Unloading old module and loading new module..."
lsmod | grep -q kqemu && sudo rmmod kqemu
sudo modprobe kqemu



exit 99

# Now make the PACKAGE. Using checkinstall is a great way to
# install applications because it makes '.deb' packages.
# This means Qemu can later be removed via apt-get or Synaptic.
echo "QEMU is a generic processor emulator. 
This package was compiled by InstQEMU.sh and also contains the KQEMU accellerator.
http://fabrice.bellard.free.fr/qemu" > description-pak || exit 16
# The reason for overwriting install.sh so it is empty is
# to ensure the package made with checkinstall can install cleanly
cat /dev/null > kqemu/install.sh                || exit 17
# Create the .deb package

echo "QEMU is now installed."
echo
echo "Loading the KQEMU kernel module..."
mkdir -p /lib/modules/$(uname -r)/misc && cp kqemu/kqemu.ko /lib/modules/$(uname -r)/misc && depmod -a
modprobe kqemu           || exit 19 # Load the kernel module
mknod /dev/kqemu c 250 0 || exit 20 # Create the KQEMU device
chmod 666 /dev/kqemu     || exit 21 # Make it accessible to all users    
echo

echo "When you reboot, the KQEMU accellerator will not load anymore."
echo "But you can add a few lines to a boot script to fix this."
echo "For more details, please refer to:"
echo "http://fabrice.bellard.free.fr/qemu/kqemu-doc.html"
echo
echo "InsQEMU SUCCEEDED!"
exit 0
