#!/bin/bash

for dir in *
do
  echo
  echo $dir
  cd $dir
  if [ ! -f dead.package ]; then

    S=$(find . -maxdepth 1 ! -empty -name sources)
    if [ "$S" = "./sources" ]; then

      NP=$(cvs status | grep Needs\ Patch | wc -l)
      if [ "$NP" != 0 ]; then
         # clean up from the last run.

         SC=$(cvs status sources | grep Needs\ Patch | wc -l)
         if [ "$SC" = 0 ]; then
           # Sources didn't change, keep tarballs.
           SPEC=$(find . -maxdepth 1 -name "*.spec")
           rpmbuild --clean --nodeps --define "_sourcedir ."  --define "_builddir ."  $SPEC
         else
           # Sources changed. throw out old tarball
           make clean
         fi
         # gfs-* is messy and leaves this crap around even after a clean
         rm -rf __MACOSX
         rm -rf ._GFS_ARTEMISIA ._GFS_BODONI_OT ._GFS_GAZIS

         cvs up
         make prep
      else
         echo no change since last prep.
      fi
    else
      echo "Empty or no sources."
    fi
  else
   echo "Dead package."
  fi
  cd ..
done
