#!/bin/bash
#
#!F:coke
#
#!P:/usr/local/bin
#
#!D:Compile Kernel Utility 
#
#!C:Copyright 1997-1998 by Peter Bieringer <pb@bieringer.de>
#
#!V:Version 1.05 23.04.1998

# Changes since
#  1.04: with option "-ndc", output is appended to existing files
#  1.03: info display
#  1.02: new option "-ndc", autobackup old error files
#  1.01: stupid typing bug displaying modules warnings
#  1.00: new features

STDERR_KERNEL=err.txt
STDERR_MODULES=errm.txt

TARGET_KERNEL=bzImage
TARGET_MODULES=modules

OPTION_ONLYINFO=`echo $* | grep -e -i >/dev/null && echo 1`
OPTION_RESULTS=`echo $* | grep -e -r >/dev/null && echo 1`
OPTION_NODEPEND=`echo $* | grep -e -nd >/dev/null && echo 1`
OPTION_HELP=`echo $* | grep -e -[h?] >/dev/null && echo 1`

info() {
  grep "^#!D" $0 | awk -F: '{ print $2}'
  grep "^#!C" $0 | awk -F: '{ print " " $2}'
  grep "^#!V" $0 | awk -F: '{ print " " $2}'
  echo
}
    
help() {
    echo -e " Usage: $0 [-i|-?] [-ndc]\n"
    echo -e "        -ndc : make only target, no dep and no clean\n"
    echo -e "        -?   : do nothing"
    echo -e "        -i   : shows only information"
    echo -e "        -r   : shows results"
}

#set -x

info

if [ "$OPTION_HELP" = "1" ]; then
    help;
    exit 1;
fi

if [ "$OPTION_RESULTS" = "1" ]; then
    more $STDERR_KERNEL $STDERR_MODULES
    exit 0
fi

if ! [ "$OPTION_ONLYINFO" = "1" ]; then

    if ! [ -f Makefile ]; then
	echo -e "\a No 'Makefile' is in the actual directory - Stop!"
	exit 1
    fi

    if ! [ "$OPTION_NODEPEND" = "1" ]; then
        echo -e "\a Start: 'make dep'"
        make dep

        echo -e "\a Start: 'make clean'"
        make clean

        # Backup old error files
	TIME=`date '+%H%M%S'`
        if [ -f $STDERR_KERNEL ]; then
	    mv $STDERR_KERNEL $STDERR_KERNEL.$TIME
        fi
        if [ -f $STDERR_MODULES ]; then
	    mv $STDERR_MODULES $STDERR_MODULES.$TIME
        fi

	CMDKERNEL="make $TARGET_KERNEL 2>$STDERR_KERNEL"
        CMDMODULES="make $TARGET_MODULES 2>$STDERR_MODULES"
    else
	CMDKERNEL="make $TARGET_KERNEL 2>>$STDERR_KERNEL"
        CMDMODULES="make $TARGET_MODULES 2>>$STDERR_MODULES"
    fi
    
    ## Compile kernel
    # Because I don't know, how to execute a command stored in a variable, 
    # there are two "if" necessary.
    
    echo -e "\a Start: '$CMDKERNEL'"
    
    if ! [ "$OPTION_NODEPEND" = "1" ]; then
	make $TARGET_KERNEL 2>$STDERR_KERNEL
    else
	make $TARGET_KERNEL 2>>$STDERR_KERNEL
    fi
        
    ## Compile modules
    echo -e "\a Start: '$CMDMODULES'"
    
    if ! [ "$OPTION_NODEPEND" = "1" ]; then
        make $TARGET_MODULES 2>$STDERR_MODULES
    else
        make $TARGET_MODULES 2>>$STDERR_MODULES
    fi

    echo
    echo -e "\aDone"
    sleep 1
    echo -e "\a"
    echo "Results..."
else
    echo "Show only information..."
fi

# Kernel: test for errors and warnings
echo -n " Kernel:  stderr output: "
NUMERRORS_KERNEL=`grep -i -c error $STDERR_KERNEL`
NUMWARNINGS_KERNEL=`grep -i -c warning $STDERR_KERNEL`
echo " errors: $NUMERRORS_KERNEL lines  warnings: $NUMWARNINGS_KERNEL lines"

# Modules: test for errors and warnings
echo -n " Modules: stderr output: "
NUMERRORS_MODULES=`grep -i -c error $STDERR_MODULES`
NUMWARNINGS_MODULES=`grep -i -c warning $STDERR_MODULES`
echo " errors: $NUMERRORS_MODULES lines  warnings: $NUMWARNINGS_MODULES lines"


