#!/bin/sh
# $Id: rrbld,v 1.1 2000/09/04 15:45:48 dmetz Exp $
# $crtd:  by  Derald Metzger  on  000419 $
# $cmnt:  Rebuild rpm from source (after code change). $

#  Exit on err
#set -e

topdir=/usr/src/redhat

loc_mir_root=/cm/arc/mirror        # local (nfs mount) mirror dirtree
site_mir_root=moses:/cm/arc/mirror # site mirror dirtree (may be remote) 
pub_ftp_root=yoda:/lhome/ftp/pub   # pub ftp server dirtree

m1="missing \`SPECS/<pkg>.spec -> ../SOURCES/<pkg>' symlink"
m2="missing \`SOURCES/<pkg> -> <pkg-ver>' symlink to cvs working dirtree"
m3="missing \`SOURCES/<pkg>/doc/<pkg>.spec' ie the spec file"

usage="
DESCRIPTION:
  rrbld - rpm rebuild
  This cmd uses the working space under internally defined \$topdir.
  It looks for the spec file in SPECS/<pkg>.spec. It expects to find
  a symlink at SOURCES/<pkg> to a cvs working dir for <pkg>-<ver>.
  At each step it will present info and prompt. After user 
  confirmation it will:
  1. rename the cvs working dir to match info in the spec file
  2. test the cvs working dir for a match to the repository
  3. make a tarball from the cvs working dir
  4. build binary and source rpms from the tarball
  5. post the binary rpm to the local mirror
  6. post the binary rpm to the remote site mirror
  7. post the binary rpm to the ftp server
  8. post the src.rpm to the ftp server
  9. post the tarball to the ftp server
usage:
  `basename $0` <pkg> [mir_sdir [rpms_sdir [srpms_sdir]]]
     mir_sdir   - mirror subdir, for both local and remote mirrors
                  default=$mir_sdir
     rpms_sdir  - ftp pub subdir for rpms. default=$rpms_sdir
     srpms_sdir - ftp pub subdir for srpms. default=$srpms_sdir
Notes:
  Default action is to skip the step and proceed to the next prompt.
  Use the \`rpm -b? <pkg>' cmds during debug.
  Use rrbld when ready to update and post a new rpm package.
"

pkg=$1
mir_sdir=contrib
rpms_sdir=$1
srpms_sdir=$1
tarball_sdir=$1

[ -n "$pkg" ] || { echo "### No pkg arg! $usage"; exit }

[ -z "$2" ] || mir_sdir=$2
[ -z "$3" ] || rpms_sdir=$3
[ -z "$4" ] || srpms_sdir=$4

#  Get version and release from the spec file. Make tag
specf=$topdir/SPECS/$pkg.spec
[ -e $specf ] || { 
	[ -L $specf ] || echo $m1
    [ -L $topdir/SOURCES/$pkg ] || echo $m2
    [ -f $topdir/SOURCES/$pkg/doc/$pkg.spec ] || echo $m3
    echo Exiting
    exit
}
v=`grep Version: $specf|cut -d' ' -f2`
r=`grep Release: $specf|cut -d' ' -f2`
tag=`echo "v$v" | tr '.' '_'`

#  Validate cvs working dir
cd $topdir/SOURCES
cvswd=`expr "\`ls -l $pkg\`" : '[^>]*> \(.*\)'`
[ "$pkg-$v" = "$cvswd" ] || {
	echo "
### The cvs working dirname does NOT match the spec file rev to be built.
    cvswd:  $cvswd
    pkg-ver: $pkg-$v"
	echo -n "    Rename cvswd [y|n](n)? "
	read
    if [ "$REPLY" = y ]; then \
		mv $cvswd $pkg-$v
        ln -sf $pkg-$v $pkg
	else
		echo "### Wrong working dirtree for spec file Version: entry. Exit"
		exit
	fi
}

#  Do cvs repository checks for this version
echo "
# ls -ldF `(exec pwd)`/pkg*"
ls -ldF $pkg*
echo "cd $pkg"
cd $pkg
echo "
# cvs -nq update -d -r$tag"
cvs -nq update -d

/bin/echo -n "
## You should have NO cvs discrepancies above before proceeding.
   tar and compress the cvs working dirtree $pkg-$v/ [y|n](n)? "
read
if [ "$REPLY" = y ]; then \
	cd ..
	tar zcvf $pkg-$v.tgz $pkg-$v
	echo "## tar & compress complete"
else
	echo "## Skipped tar & compress. Will use existing .tgz file."
fi

echo -en "\n## Do a full build and rpm pkg y|n](n)? "
read
if [ "$REPLY" = y ]; then { cd $topdir/SPECS; rpm -ba $pkg.spec; echo };
else echo "## Skipped build. Will use existing rpm/srpm"; fi
cd $topdir/RPMS/i386
echo -e "\n# ls -ldF `(exec pwd)`/$pkg*";
ls -ldF $pkg*;

/bin/echo -n "## cp rpm to $loc_mir_root/$mir_sdir [y|n](n)? "
read
[ "$REPLY" = y ] && cp $pkg-$v-$r.i386.rpm $loc_mir_root/$mir_sdir/
cd $loc_mir_root/$mir_sdir
echo -e "\n# ls -ldF `(exec pwd)`/$pkg*";
ls -ldF $pkg*;

echo -n "## scp rpm to $site_mir_root/$mir_sdir [y|n](n)? "
read
[ "$REPLY" = y ] && scp $pkg-$v-$r.i386.rpm $site_mir_root/$mir_sdir

echo -n "## scp rpm to $pub_ftp_root/$rpms_sdir [y|n](n)? "
read
[ "$REPLY" = y ] && scp $pkg-$v-$r.i386.rpm $pub_ftp_root/$rpms_sdir

cd $topdir/SRPMS
echo -e "\n# ls -aCF `(exec pwd)`/$pkg*";
ls -ldF $pkg*;
echo -n "## scp src.rpm to $pub_ftp_root/$srpms_sdir [y|n](n)? "
read
[ "$REPLY" = y ] && scp $pkg-$v-$r.src.rpm $pub_ftp_root/$srpms_sdir

cd $topdir/SOURCES
echo -e "\n# ls -ldF `(exec pwd)`/$pkg*";
ls -ldF $pkg*;
echo -n "## scp tarball to $pub_ftp_root/$tarball_sdir [y|n](n)? "
read
[ "$REPLY" = y ] && scp $pkg-$v.tgz $pub_ftp_root/$tarball_sdir
