#! /bin/sh
# mkkpkg: make new configured kernel package from kernel-<>-<>.src.rpm
#                                  Time-stamp: "99/02/25 13:35:43 shom"
# Verion 1.0
# Written by MATSUMOTO Shoji <vine@flatout.org>
# This script must be distributed by Project Vine <vine@flatout.org>.
# The responsibility of any trouble with this script is on you.

ARCH=i386

if [ "$1" = "-v" ] ; then
  MESG=echo
  shift
else
  MESG=true
fi

CMDNAME=`basename $0`
TMPFILE="/tmp/$CMDNAME-$$"

RPMRC=/usr/lib/rpm/rpmrc
if [ -f ~/.rpmrc ] ; then RPMRC=~/.rpmrc ; fi
RPMDIR=`grep -v '^#' $RPMRC | grep '^topdir:' | awk '{print $2}'`

$MESG use rpmdir: $RPMDIR

SRPM=$1
CONFMETHOD=$2

if [ -z "$SRPM" ] ; then
  echo "usage: `basename $0` [-v] <sourcepackage> [config|menuconfig|xconfig]"
  exit -1
fi

if [ ! -f "$SRPM" ] ; then
  echo "file not found: $SRPM"
  exit -1
fi

SRPMNAME=`basename $SRPM .src.rpm`
KVER=`echo $SRPMNAME | cut -d- -f2`
KREV=`echo $SRPMNAME | cut -d- -f3`
if [ "$CONFMETHOD" = "" ] ; then CONFMETHOD=menuconfig ; fi

$MESG $CMDNAME will execute \"make $CONFMETHOD\"

rpm -qlp $SRPM > $TMPFILE
SPEC="$RPMDIR/SPECS/`grep '.spec$' $TMPFILE`"
KCFG="`grep \"$ARCH.config\" $TMPFILE`"
rm $TMPFILE
if [ -z "$SPEC" ] ; then
  echo "can't find spec file"
  exit 1
fi
if [ -z "$KCFG" ] ; then
  echo "can't find default configuration file"
  exit 1
fi

$MESG use specfile: $SPEC
$MESG use kernel config file: $KCFG

echo executing \"rpm -i $SRPM\"
rpm -i $SRPM
echo executing \"rpm -bp $SPEC\"
echo Please wait to exec \"make $CONFMETHOD\"
rpm -bp $SPEC >& /dev/null
if [ ! -d $RPMDIR/BUILD/linux ] ; then
  echo $RPMDIR/BUILD/linux is not found
  exit -1;
fi
cd $RPMDIR/BUILD/linux
make $CONFMETHOD

$MESG mv $RPMDIR/SOURCES/$KCFG{,.org}
mv $RPMDIR/SOURCES/$KCFG{,.org}
$MESG cp .config $RPMDIR/SOURCES/$KCFG
cp .config $RPMDIR/SOURCES/$KCFG

while [ "$NREV" = "" ] ; do
  echo -n "Input Revision [current: $KREV] "
  read NREV
  if [ -f $RPMDIR/SRPMS/kernel-$KVER-$NREV.src.rpm ] ; then
    echo -n "$RPMDIR/SRPMS/kernel-$KVER-$NREV.src.rpm is found."
    echo -n "  Override? [y/n] "
    read yn
    if [ ! "$yn" = "y" -a ! "$yn" = "Y" ] ; then
      NREV=""
    fi
  fi
done

if sed -e "s/^Release: $KREV$/Release: $NREV/" $SPEC > $TMPFILE ; then
  mv $SPEC{,.org}
  cp $TMPFILE $SPEC
  rm $TMPFILE
else
  echo can\'t find \"Release: $KREV\" line
  rm $TMPFILE
  exit -1
fi

$MESG modified $SPEC
$MESG "  from \"Release: $KREV\" to \"Release: $NREV\""

echo "To edit $SPEC, Push \"Ctrl+Z\" to suspend."
echo "  (When finished to edit, type \"fg\" to continue.)"
echo -n "[Push enter to continue]"
read

echo executing rpm -ba $SPEC
echo "  "Log output to /tmp/kernelrebuild.{log,err}
echo "  You can read them with following sequence:"
echo "    1. Push \"Ctrl+Z\" to suspend this process."
echo "    2. Type \"bg\" to continue background."
echo "    3. Use \"less\" to print /tmp/kernel-rebuild.log,err."
echo "    3.1 Push \"Shift+F\" to \"Forward forever; like tail -f\"."
echo "    3.2 If you want to stop, push \"Ctrl+C\"."
rpm -ba $SPEC > /tmp/kernel-rebuild.log 2> /tmp/kernel-rebuild.err

if [ ! -f $RPMDIR/RPMS/$ARCH/kernel-$KVER-$NREV.$ARCH.rpm ]; then
  echo Some errors were occurred.
  echo kernel-$KVER-$NREV.$ARCH.rpm was not generated.
  echo Please check /tmp/kernel-rebuild.{log,err}
  exit -1
fi

echo Created:
ls $RPMDIR/RPMS/$ARCH/kernel-$KVER-$NREV.i386.rpm
ls $RPMDIR/RPMS/$ARCH/kernel-*-$KVER-$NREV.i386.rpm
ls $RPMDIR/SRPMS/kernel-$KVER-$NREV.src.rpm
