#!/bin/sh
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: if this program is able to find the arch, it will print. Otherwise, nothing will be shown.

KERNEL=$1
Usage() {
  echo "Usage:"
  echo "$(basename $0) KERNEL_VER"
  echo "Ex: $0 2.4.20-31.9smp"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -*)     echo "${0}: ${1}: invalid option" >&2
            Usage >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

[ -z "$KERNEL" ] && exit 1

CONFIG_FILE=/boot/config-$KERNEL
[ ! -f "$CONFIG_FILE" ] && exit 1

# Actully, maybe we just need 386/586/686/X86_64 ?
CPULIST="CONFIG_M386 CONFIG_M486 CONFIG_M586 CONFIG_M686 CONFIG_MPENTIUMII CONFIG_MPENTIUMIII CONFIG_MPENTIUMM CONFIG_MPENTIUM4 CONFIG_MK6 CONFIG_MK7 CONFIG_MK8 CONFIG_X86_64"
for icpu in $CPULIST; do
  if grep -q "$icpu=y" $CONFIG_FILE; then
    arch="$(echo $icpu | sed -e "s/CONFIG_M//g" -e "s/CONFIG_//g")"
    case "$arch" in
     [3456]86):
       arch=i${arch}
       ;;
    esac
    echo "$arch"
  fi
done
