#!/usr/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_^.*\(desktop586\|desktop\|netbook\|server\|xen-pvops\|iop32x\|kirkwood\|versatile\)$_-\1_;t;s_.*__;'`
KERNEL_RELEASE=`echo ${kernel_version} | sed 's|desktop586\|desktop\|netbook\|server\|xen-pvops\|iop32x\|kirkwood\|versatile\|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
  DESKTOP586='0'
  DESKTOP='0'
  NETBOOK='0'
  SERVER='0'
  XEN_PVOPS='0'
  IOP32X='0'
  KIRKWOOD='0'
  VERSATILE='0'
  case "$KERNEL_TYPE" in
  -desktop586) DESKTOP586='1';;
  -desktop) DESKTOP='1';;
  -netbook) NETBOOK='1';;
  -server) SERVER='1';;
  -xen-pvops) XEN_PVOPS='1';;
  -iop32x) IOP32X='1';;
  -kirkwood) KIRKWOOD='1';;
  -versatile) VERSATILE='1';;
  esac

  if [ -e /etc/mageia-release ]; then
    cat > ${output_file} << EOF
/* This file is automatically generated at boot time. */
#ifndef __BOOT_KERNEL_H_
#define __BOOT_KERNEL_H_

/* Kernel type $KERNEL_TYPE */

#ifndef __BOOT_KERNEL_DESKTOP586
#define __BOOT_KERNEL_DESKTOP586 $DESKTOP586
#endif

#ifndef __BOOT_KERNEL_DESKTOP
#define __BOOT_KERNEL_DESKTOP $DESKTOP
#endif

#ifndef __BOOT_KERNEL_NETBOOK
#define __BOOT_KERNEL_NETBOOK $NETBOOK
#endif

#ifndef __BOOT_KERNEL_SERVER
#define __BOOT_KERNEL_SERVER $SERVER
#endif

#ifndef __BOOT_KERNEL_XEN_PVOPS
#define __BOOT_KERNEL_XEN_PVOPS $XEN_PVOPS
#endif

#ifndef __BOOT_IOP32X
#define __BOOT_IOP32X $IOP32X
#endif

#ifndef __BOOT_KERNEL_KIRKWOOD
#define __BOOT_KERNEL_KIRKWOOD $KIRKWOOD
#endif

#ifndef __BOOT_KERNEL_VERSATILE
#define __BOOT_KERNEL_VERSATILE $VERSATILE
#endif

#endif
EOF
  elif [ -e /etc/redhat-release ] || [ -e /etc/fedora-release ]; then
    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
fi
