#!/bin/bash
#
# Generate a header that defines the kernel we care about.
#
# Version 1.0
#
# Adapted from Red Hat's initscripts package
# Licensed under the GNU GPL

kernel_version=`uname -r`
output_file="/boot/kernel.h"
target_arch=""

# Parse command line arguments
action_flag=""
while [ $# -gt 0 ]; do
    case $1 in
	--targetarch*|-a)
	    if echo $1 | grep '=' >/dev/null ; then
		target_arch=`echo $1 | sed 's/^.*=//'`
            else
                target_arch="$2"
                shift
            fi
            ;;
	--kernelver*|-k)
	    if echo $1 | grep '=' >/dev/null ; then
		kernel_version=`echo $1 | sed 's/^.*=//'`
            else
                kernel_version="$2"
                shift
            fi
            ;;
	--output*|-o)
	    if echo $1 | grep '=' >/dev/null ; then
		output_file=`echo $1 | sed 's/^.*=//'`
            else
                output_file="$2"
                shift
            fi
            ;;
    esac
    shift
done


KERNEL_TYPE=`echo ${kernel_version} | sed 's_^.*\(BOOT\|smp\|enterprise\|bigmem\|hugemem\|debug\|vmnix\)$_-\1_;t;s_.*__;'`
KERNEL_RELEASE=`echo ${kernel_version} | sed 's|BOOT\|smp\|enterprise\|bigmem\|hugemem\|debug||g'`

if [ -n "${target_arch}" ]; then
    KERNEL_ARCH="${target_arch}"
else
    rpm -q kernel$KERNEL_TYPE-$KERNEL_RELEASE >/dev/null 2>&1 && KERNEL_ARCH=`rpm -q --qf '%{ARCH}' kernel$KERNEL_TYPE-$KERNEL_RELEASE 2>/dev/null` || KERNEL_ARCH=`uname -m`
fi

if [ -n "$KERNEL_ARCH" ]; then
  ENTERPRISE='0'
  SMP='0'
  UP='0'
  BIGMEM='0'
  HUGEMEM='0'
  BOOT='0'
  DEBUG='0'
  VMNIX='0'
  case "$KERNEL_TYPE" in
  -BOOT)
	  BOOT='1'
	  if [ "${KERNEL_ARCH}" = "i686" ] || [ "${KERNEL_ARCH}" = "i586" ] || [ "${KERNEL_ARCH}" = "athlon" ]; then
	      KERNEL_ARCH="i386"
	  fi
	  ;;
  -smp) SMP='1';;
  -enterprise) ENTERPRISE='1';;
  -bigmem) BIGMEM='1';;
  -hugemem) HUGEMEM='1';;
  -vmnix) VMNIX='1';;
  *) UP='1';;
  esac
  cat > ${output_file} << EOF
/* This file is automatically generated at boot time. */
#ifndef __BOOT_KERNEL_H_
#define __BOOT_KERNEL_H_

/* Kernel type $KERNEL_ARCH$KERNEL_TYPE */

#ifndef __MODULE_KERNEL_$KERNEL_ARCH
#define __MODULE_KERNEL_$KERNEL_ARCH 1
#endif

#ifndef __BOOT_KERNEL_ENTERPRISE
#define __BOOT_KERNEL_ENTERPRISE $ENTERPRISE
#endif

#ifndef __BOOT_KERNEL_BIGMEM
#define __BOOT_KERNEL_BIGMEM $BIGMEM
#endif

#ifndef __BOOT_KERNEL_HUGEMEM
#define __BOOT_KERNEL_HUGEMEM $HUGEMEM
#endif

#ifndef __BOOT_KERNEL_SMP
#define __BOOT_KERNEL_SMP $SMP
#endif

#ifndef __BOOT_KERNEL_UP
#define __BOOT_KERNEL_UP $UP
#endif

#ifndef __BOOT_KERNEL_BOOT
#define __BOOT_KERNEL_BOOT $BOOT
#endif

#ifndef __BOOT_KERNEL_DEBUG
#define __BOOT_KERNEL_DEBUG $DEBUG
#endif

#ifndef __BOOT_KERNEL_VMNIX
#define __BOOT_KERNEL_VMNIX $VMNIX
#endif
#endif
EOF
fi
