#!/bin/bash
# build an UML kernel based on a vanilla kernel and UML patch
#
# Copyright (C) 2004  Eric Marchionni, Patrik Rayo
# Zuercher Hochschule Winterthur
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# RCSID $Id: build-umlkernel,v 1.2 2005/01/09 21:54:25 as Exp $

DIR=`dirname $0`

source $DIR/function.sh

[ -f $DIR/../testing.conf ] || die "configuration file 'testing.conf' not found"

source $DIR/../testing.conf

cecho-n " * Looking for kernel at '$KERNEL'.."
if [ -f "${KERNEL}" ]
then
    cecho "found it"
    KERNELVERSION=`basename $KERNEL .tar.bz2 | sed -e 's/linux-//'`
    cecho " * Kernel version is $KERNELVERSION"
else
    cecho "none"
    exit
fi

if [ ${UMLPATCH} ]
then
    cecho-n " * Looking for uml patch at '$UMLPATCH'.."
    if [ -f "${UMLPATCH}" ]
    then
	cecho "found it"
    else
	cecho "none"
	exit
    fi
fi

cecho-n " * Looking for kernel config at '$KERNELCONFIG'.."
if [ -f "${KERNEL}" ]
then
    cecho "found it"
else
    cecho "none"
    exit
fi

#######################################################
# unpack kernel and create symlink
#

if [ ! -d $BUILDDIR ]
then
    cecho " * Creating directory '$BUILDDIR'"
    mkdir $BUILDDIR
fi

cecho " * Changing to directory '$BUILDDIR'"
cd $BUILDDIR

LOGFILE=${BUILDDIR}/testing.log

if [ ! -f $LOGFILE ]
then
    cecho-n " * Logfile '$LOGFILE' does not exist..creating.."
    touch $LOGFILE
    cecho "done"
fi

cecho-n " * Unpacking kernel.."
tar xjf $KERNEL >> $LOGFILE 2>&1
cecho "done"

KERNELDIR=${BUILDDIR}/linux-${KERNELVERSION}

if [ -d $KERNELDIR ]
then
    cecho " * Kernel directory is '$KERNELDIR'"
    cecho " * Creating symlink 'linux'"
    if [ -d linux ]
    then
	rm linux
    fi
    ln -s linux-${KERNELVERSION} linux
else
    cecho "!! Kernel directory '$KERNELDIR' can not be found"
    exit
fi

#######################################################
# patch kernel
#

cecho " * Changing to directory '$KERNELDIR'"
cd $KERNELDIR

if [ $UMLPATCH ]
then
    cecho-n " * Applying uml patch.."
    bzcat $UMLPATCH | patch -p1 >> $LOGFILE 2>&1
    cecho  "done"
fi

#######################################################
# copy our default .config to linux and build kernel
#

cp $KERNELCONFIG .config

cecho "!!"
cecho "!! Making .config for kernel. You might be prompted for new parameters!"
cecho "!!"
make oldconfig ARCH=um >> $LOGFILE 2>&1

cecho-n " * Now compiling uml kernel.."
make linux ARCH=um  >> $LOGFILE 2>&1
cecho "done"

cecho-n " * Copying uml kernel to '${BUILDDIR}/linux-uml-${KERNELVERSION}'.."
mv linux ${BUILDDIR}/linux-uml-${KERNELVERSION}
cecho "done"