#!/bin/sh

# This Program is Free Software
# (c) Patrik Nilsson 2005 - 2008 (blambi@chebab.com)
# Licensed under the GNU GPL v3

# build-kernel.sh version 0.7

# Changes in v0.7
# * added support for sparc64
# * removed a unnessery echo if it was run on "linux" terminal
# * added -v and version command to only display copyright and version
#   information.
# Changes in v0.6
# * added support for SMP
#   arguments: smp jobs [noclean]
# * no longer tries to write to a "xterm" title if we are in the linux
#   console.
# Changes in v0.5
# * added support for distcc
#   arguments: distcc jobs [hosts] [noclean]
#   if hosts in not given we will check /etc/distcc/hosts
# * some cleanup of printing rutines etc
# Changes in v0.4
# * added support for powerpc
# * support for newer linux versions (>=2.6.18)
# Changes in v0.3
# * added a argument (noclean) that prevents the normal cleaning to be done.
#   Its not recommended to run this on other then new sources.
# Changes in v0.2
# * moved version detection to be before Install of the kernel
# * added ARCH autodetection (from the makefile of linux 2.6.16)

KERNELDIR="/usr/src/linux"

print_status()
{
    TEXT=$1
    echo -e "\033[01;31m-----------> $TEXT\033[00m"
    if [ $TERM != "linux" ]; then
	echo -en "\033]0;$TEXT\007"
    fi
}


# Display version and copyright Magic =)
head -n 7 build-kernel.sh | tail -n 1 | sed -e 's:\# ::'
echo
head -n 5 build-kernel.sh | tail -n 3 | sed -e 's:\# ::'
echo

if [ "$1" == "-v" ] || [ "$1" == "version" ]; then
    exit
fi

cd $KERNELDIR

if [ "$1" == "smp" ]; then
    print_status "Setting up SMP"
    USE_SMP=1
    JOBS=$2
    if [ "$3" == "noclean" ]; then
	NOCLEAN=1
    fi
fi

if [ "$1" == "distcc" ]; then
    print_status "Setting up distcc"
    USE_DISTCC=1
    JOBS=$2
    if [ "$3" ]; then
	export DISTCC_HOSTS="$3"
    else
	export DISTCC_HOSTS=$( cat /etc/distcc/hosts )
    fi

    if [ "$4" == "noclean" ]; then
        NOCLEAN=1
    fi
    echo "Will use:"
    printf "jobs: %d\n" $JOBS
    echo "hosts: $DISTCC_HOSTS"
fi    

if [ "$1" == "noclean" ]; then
    NOCLEAN=1
fi

if [ ! $NOCLEAN ]; then
    print_status "Cleaning"
    make clean
fi

print_status "Building bzImage"
if [ ! $USE_DISTCC ]; then
    if [ ! $USE_SMP ]; then
	make
    else
	make -j$JOBS
    fi
elif [ ! $USE_SMP ]; then
    make CC="distcc" -j$JOBS
fi

print_status "Building modules"
if [ ! $USE_DISTCC ]; then
    if [ ! $USE_SMP ]; then
	make modules
    else
	make modules -j$JOBS
    fi
elif [ ! $USE_SMP ]; then
    make modules CC="distcc" -j$JOBS
fi

print_status "Installing modules"
make modules_install

print_status "Checking kernel version and arch"
KERNELVERSION=$( cat $KERNELDIR/include/linux/version.h | grep UTS_RELEASE | sed 's/\#define UTS_RELEASE \"//' | sed 's/"//' )
if [ "$KERNELVERSION" == "" ]; then
    KERNELVERSION=$( make kernelrelease )
fi
echo $KERNELVERSION
ARCH=$( uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/s390x/s390/ -e s/parisc64/parisc/ -e s/ppc.*/powerpc/ )

print_status "Installing the kernel & config"
if [ ! "$ARCH" == "powerpc" ] && [ ! "$ARCH" == "sparc64" ]; then
    cp $KERNELDIR/arch/$ARCH/boot/bzImage /boot/kernel-$KERNELVERSION
else
    cp $KERNELDIR/vmlinux /boot/kernel-$KERNELVERSION
fi	
cp $KERNELDIR/.config /boot/kernel-$KERNELVERSION.config
echo "Installed to: /boot/kernel-$KERNELVERSION"
echo "ARCH: $ARCH"
print_status "Done"
if [ $TERM != "linux" ]; then
    echo -en "\033]0;$TERM\007"
fi
