#!/bin/sh
# mkkpkg: make new configured kernel package from kernel-<>-<>.src.rpm
#                                  Time-stamp: "2000-02-29 21:18:10 vine"
#
# - fix a bug: exits when SRPM doesnt exist
#
# [Version 2.2.5] Mar 3 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - bug fix: set ARCH as i386 (i586 does not make pcmcia now)
# 
# [Version 2.2.4] Feb 29 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - fix a small bug: patch from Shoji Matsumoto
# 
# [Version 2.2.3] Feb 29 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - mkkpkg-2.2.3
# - fix a bug and typo by Lisa Sagami, thanks.
#
# [Version 2.2.2] Feb 25 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - fix a bug: wrong path for SRPMS
# 
# [Version 2.2.1] Feb 25 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - fix a bug: lost definition of RPMRC
# 
# [Version 2.2] Feb 24 2000
# Modified by SAGAMI <sagami@pc.highway.ne.jp>
# - fixed some bugs
#	- 2.1 exits -1 in anyways :-p
#	- BUILDMODE -> RPMBUILDMODE
#	- Some ENV value needs {,}?
#	- RPMDIR sometimes NULL
# - moved /usr/bin/mkkpkg to /usr/sbin/mkkpkg
# - tweaken, using --showrc --rmspec
# - dont include packager tag in new spec file, because rpm will include it
#   if defined in rpmmacros
# - awk ARGV problems in parser
#
# [Version 2.1] Jan 30 2000
# Modified by Jun Nishii <jun@vinelinux.org>
# - merge patch from SAGAMI <sagami@pc.highway.ne.jp>
#      + bug fix for processing .rpmmacros
#      + use --target
#      + added Packager, Vendor, Distribution in new spec file
#      + --clean, --rmsource
#      + use new revision number for new spec file name
#      + modify messages
# - clean up script
#
# [Version 2.0]
# Modified by Jun Nishii <jun@vinelinux.org> Jan 10 2000
# - use parsespec to remove smp, BOOT, BOOTsmp entry
#
# Modified by Yasuhide OOMORI <dasen@typhoon.co.jp>
# - Quick hack about rpmdir.
# - Modified for mistyping of new revision:
#     -- remove control character
# - Modified to quickly make one kernel:
#     -- no compile for smp or BOOT kernel.
#
# [Verion 1.0]
# Written by MATSUMOTO Shoji <vine@flatout.org>
# This script must be distributed by Project Vine <vine@vinelinux.org>.
# The responsibility of any trouble with this script is on you.
#
############################################################

CMDNAME=`basename $0`
TMPFILE="/tmp/$CMDNAME-$$"
DISTRIBUTION=Local
RETVAR=0

############################################################
### some functions
showUsage(){
	echo "mkkpkg: make new configured kernel package from kernel-<>-<>.src.rpm"
	echo "usage: $CMDNAME [-v] <sourcepackage> [config|menuconfig|xconfig]"
	return 0
}

checkDir(){
if [ ! -d $1 ] ; then
	echo "directory not found: $1"
	return -1
fi
return 0
}

checkFile(){
if [ ! -f $1 ] ; then
	echo "file not found: $1"
	return -1
fi
return 0
}

############################################################
### process arguments

if [ "$1" = "" ] ; then
	showUsage && exit -1
fi

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

SRPM=$1
CONFMETHOD=$2

checkFile $SRPM
RETVAR=$?
if [ $RETVAR -ne 0 ] ; then
	showUsage && exit -1 
fi

SRPMNAME=`basename $SRPM .src.rpm`
KVER=`echo $SRPMNAME | cut -d- -f2`
KREV=`echo $SRPMNAME | cut -d- -f3`

[ "$CONFMETHOD" = "" ] && CONFMETHOD=menuconfig
$MESG $CMDNAME will execute \"make $CONFMETHOD\"

############################################################
### get Architecture
MARCH=`uname -m`
case "$MARCH" in
	*86)	ARCH=i386;;
	  *)	ARCH=$MARCH;;
esac

#ARCH=`uname -m`

############################################################
## set RPMDIR and PACKAGER
RPMDIR="/usr/src/redhat"
RPMRC=${HOME}/.rpmmacros
PACKAGER="$(whoami) <$(whoami)@$(hostname)>"

if [ -f $RPMRC ] ; then
	RPMDIR=`rpm --showrc | grep '_topdir' | tail -1 | \
		sed "s/^.*_topdir//g" | cut -f2`
	if [ ! -d $RPMDIR ] ; then
		RPMDIR="/usr/src/redhat"
	fi
	PACKAGER=`grep -v '^#' $RPMRC | grep '^%packager' | tail -1 | \
	    awk '{print $2,$3,$4,$5,$6}'`
	if [ "$PACKAGER" = "" ] ; then 
	    PACKAGER="$(whoami) <$(whoami)@$(hostname)>"
	fi
fi

checkDir $RPMDIR || exit -1
$MESG Using rpmdir: $RPMDIR

############################################################
## extract file names from SRPM

rpm -qlp $SRPM > $TMPFILE
SPEC="$RPMDIR/SPECS/`grep '.spec$' $TMPFILE`"
KCFG="`grep \"$ARCH.config\" $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 for $ARCH"
	exit -1
fi
rm -f $TMPFILE

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

############################################################
## expand SRPM

echo executing \"rpm -ivh $SRPM\"
rpm -ivh $SRPM >& /dev/null

############################################################
## get revision number

while [ "$NREV" = "" ] ; do
	echo -n "Input your Kernel Revision [current: $KREV] "
	read NREV

	# remove control character in string
	NREV=`echo $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
	else
	# Confirmation of kernel revision
		echo -n "  Your kernel Revision is [revision: $NREV]. OK? [y/n] "
		read yn
		if [ ! "$yn" = "y" -a ! "$yn" = "Y" ] ; then
			NREV=""
		fi
	fi
done

############################################################
## make spec file

NSPEC=${RPMDIR}/SPECS/kernel-${KVER}-${NREV}.spec
/usr/lib/mkkpkg/parsespec "$NREV" "$SRPMNAME" "$DISTRIBUTION" "$PACKAGER" "$SPEC" > $NSPEC

$MESG "created new spec file: " $NSPEC
$MESG "  from \"Release: $KREV\" to \"Release: $NREV\""

echo ""
echo "To edit $NSPEC, Push \"Ctrl+Z\" to suspend."
echo "  (When finished to edit, type \"fg\" to continue.)"
read

############################################################
##

echo
echo -n "Do you want to re-expand sources/patches with new SPEC file? (y/n)"
read yn
if [ "$yn" = "y" -o "$yn" = "Y" ] ; then
  echo executing \"rpm -bp $NSPEC\"
  if rpm -bp $NSPEC > /dev/null ; then
    echo Success.
  else
    echo Failed.
    exit -1
  fi
fi
   
checkDir $RPMDIR/BUILD/linux || exit -1

cd $RPMDIR/BUILD/linux

if [ -f configs/$KCFG ] ; then
  $MESG "copying .config for $ARCH"
  cp configs/$KCFG .config
fi 

############################################################
##

echo ""
echo -n "Do you want to build SRPM ? (If no, only RPM will be built) (Y/n)"
read yn
if [ ! "$yn" = "n" -a ! "$yn" = "N" ] ; then
	RPMBUILDMODE="-ba"
	$MESG "Ok, SRPM and RPM will be built."
else
	RPMBUILDMODE="-bb"
	$MESG "Ok, only RPM will be built."
fi

############################################################
## now make config

echo Ready for make \"$CONFMETHOD\"
echo -n "[Push enter to continue]"
read

make $CONFMETHOD

checkFile .config || exit -1

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

############################################################
## now build rpm package

echo ""
echo ""
echo "Now we are prepared for executing"
echo "  rpm $RPMBUILDMODE $NSPEC --target $ARCH ."
echo ""
echo "  Log output goes to /tmp/kernel-rebuild.{log,err}."
echo "  You can read them with following sequence:"
echo "    1. Push \"Ctrl+Z\" to suspend the next long 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\"."
echo "    X. If you are using X, you can read log thru another term window."
echo ""
echo "Ready for Action?"
echo -n "[Push enter to continue]"
read
echo -n "Building..."

rpm $RPMBUILDMODE $NSPEC --target $ARCH \
	> /tmp/kernel-rebuild.log 2> /tmp/kernel-rebuild.err

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

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}.${ARCH}.rpm
ls ${RPMDIR}/RPMS/${ARCH}/kernel-*-${KVER}-${NREV}.${ARCH}.rpm
ls ${RPMDIR}/SRPMS/kernel-${KVER}-${NREV}.src.rpm

############################################################
## clean up

echo -n "Do you want to clean up BUILD tree and sources? (y/n)"
read yn
if [ ! "$yn" = "y" -a ! "$yn" = "Y" ] ; then
	true
else
	rpm --rmsource --rmspec --clean $NSPEC &> /dev/null
fi
exit 0

############################################################
