#!/bin/bash

PARAMS=1

# Where we can find the RPM directory
# and where we want to store the source code from cvs
CVS_PATH=/home/$USER/cvs
RPM_PATH=/home/$USER/rpm
PRODUCT_PATH=%{_libdir}/zope/lib/python/Products/
FULL_RPM_BUILD_ROOT=\$RPM_BUILD_ROOT$PRODUCT_PATH

if [ $# -lt "$PARAMS" ] || [ $# -gt "$PARAMS" ]
then
  echo
  echo "Give me exactly $PARAMS command-line arguments!"
  echo "build-rpm PACKAGE_NAME"
  exit 0
fi  

if [ -n "$1" ]
then
  echo Starting Building $1
  NAME=$1
  VERSION=3.0rc2
  rm -rf $CVS_PATH/$NAME-$VERSION/BTreeFolder2
  rm -rf $CVS_PATH/$NAME-$VERSION/CMFCalendar
  rm -rf $CVS_PATH/$NAME-$VERSION/CMFTopic
  rm -rf $CVS_PATH/$NAME-$VERSION/CMFCore
  rm -rf $CVS_PATH/$NAME-$VERSION/CMFDefault
  rm -rf $CVS_PATH/$NAME-$VERSION/DCWorkflow
  rm -rf $CVS_PATH/$NAME-$VERSION/Localizer
  rm -rf $CVS_PATH/$NAME-$VERSION/TranslationService

  cd $CVS_PATH && tar jcvf $NAME-$VERSION.tar.bz2 $NAME-$VERSION && cd -
    cp $CVS_PATH/$NAME-$VERSION.tar.bz2 $RPM_PATH/SOURCES/

  # Remove theses text files in order to hack a small error
  mv -f $CVS_PATH/$NAME-$VERSION/Introduction.stx /tmp
  #mv -f $CVS_PATH/$NAME-$VERSION/COPYRIGHT.txt /tmp
  #mv -f $CVS_PATH/$NAME-$VERSION/INSTALL.txt /tmp
  #mv -f $CVS_PATH/$NAME-$VERSION/UPGRADE.txt /tmp

  # We will generate one part of the spec file, so we make sure
  # to not forget any file
  # Generate the folder tree
  rm -f /tmp/build-rpm-install.tmp
  rm -f /tmp/build-rpm-files.tmp
  rm -f /tmp/build-rpm-spec.tmp

  for directory in "" `cd $CVS_PATH/$NAME-$VERSION && find * -type d -not -name "CVS" && cd -`
    do echo install -d $FULL_RPM_BUILD_ROOT/$directory | sed -e "s/\/\//\//g" >> /tmp/build-rpm-install.tmp
      # then add files we want to include into the rpm
      for file_type in py dtml txt png pt stx form zsql gif jpg css html props
        do if (ls $CVS_PATH/$NAME-$VERSION/$directory/*.$file_type > /dev/null 2>&1)
          then echo install %{name}-%{version}/$directory/*.$file_type $FULL_RPM_BUILD_ROOT/$directory | sed -e "s/\/\//\//g" >> /tmp/build-rpm-install.tmp
        fi
      done

      done

  cp /tmp/Introduction.stx /$CVS_PATH/$NAME-$VERSION/
  #cp /tmp/COPYRIGHT.txt /$CVS_PATH/$NAME-$VERSION/
  #cp /tmp/INSTALL.txt /$CVS_PATH/$NAME-$VERSION/
  #cp /tmp/UPGRADE.txt /$CVS_PATH/$NAME-$VERSION/

  for directory in `cd $CVS_PATH/$NAME-$VERSION && find * -type d -maxdepth 0 && cd -`
    do echo $PRODUCT_PATH$directory/ >> /tmp/build-rpm-files.tmp
    done
    

  # now we will regenerate the spec file
  # The line where we have %install
  L_INSTALL=`grep -n "%install" $NAME.spec| sed -e "s/:/ /g" |awk '{print $1}'`
  # The line where we have %clean
  L_CLEAN=`grep -n "%clean" $NAME.spec| sed -e "s/:/ /g" |awk '{print $1}'`
  # The line where we have %doc
  L_DOC=`grep -n "%doc" $NAME.spec| sed -e "s/:/ /g" |awk '{print $1}'`
  # The line where we have %changelog
  L_CHANGELOG=`grep -n "%changelog" $NAME.spec| sed -e "s/:/ /g" |awk '{print $1}'`
  # The total number of lines 
  L_TOTAL=`wc -l $NAME.spec | awk '{print $1}'`
  # Take the head of the file
  head -n $L_INSTALL $NAME.spec > /tmp/build-rpm-spec.tmp
  cat /tmp/build-rpm-install.tmp >> /tmp/build-rpm-spec.tmp
  head -n $L_DOC $NAME.spec | tail -n `expr $L_DOC - $L_CLEAN + 1` >> /tmp/build-rpm-spec.tmp
  cat /tmp/build-rpm-files.tmp >> /tmp/build-rpm-spec.tmp
  echo "#----------------------------------------------------------------------" >> /tmp/build-rpm-spec.tmp
  tail -n `expr $L_TOTAL - $L_CHANGELOG + 1` $NAME.spec >> /tmp/build-rpm-spec.tmp

  # now we can replace the spec file
  cp -f /tmp/build-rpm-spec.tmp $RPM_PATH/SPECS/$NAME.spec


fi

exit 0
