#!/bin/sh

ROOT_DIR=/home/hughesjr/bin
function do_usage {
  echo 'rpmmod <import rpm|srpm spec dist|prep spec|build srpm >'
}

function cmd_rpm {
  rpm --define "_topdir `pwd`" --nomd5 --define "dist .el6.centos" $*
}

function cmd_rpmbuild {
    rpmbuild --define "_topdir `pwd`" --define "dist .el6.centos" --define "rhel 6" $*
}

function do_import {
  if [ ! -e SPECS   ]; then mkdir -p SPECS   ; fi
  if [ ! -e SOURCES ]; then mkdir -p SOURCES ; fi
  cmd_rpm -ivh $1
  if [ ! -e .git ] ; then git init ; fi
  for f in `find SOURCES/ -maxdepth 1 -type f -exec file {} \; | egrep '.*\t.*text.*' | cut -d ":" -f1`; do 
    git add $f
  done
  git add SPECS/*.spec
  git status
}

function do_prep {
  if [ ! -e BUILD ]; then mkdir -p BUILD; fi
  cmd_rpmbuild -bp --nodeps $1
}

function do_srpm {
  if [ ! -e SRPMS ] ; then mkdir SRPMS ; fi
  #cmd_rpmbuild -bs --nodeps $*
  if [ "$2" == ".el5.centos" ]; then 
   rpmbuild --define "_topdir `pwd`" --define "redhat 5" --define "rhel 5" --define "dist .el5.centos" -bs --nodeps $1
  elif [ "$2" == "c5" ]; then
   rpmbuild --define "_topdir `pwd`" --define "redhat 5" --define "rhel 5" --define "dist .el5.centos" -bs --nodeps $1
  else
   rpmbuild --define "_topdir `pwd`" --define "redhat 6" --define "rhel 6" --define "dist .el6.centos" -bs --nodeps $1
  fi
}

datestamp=$(date +%Y%m%d%H%M%S)
sleep 1

case $1 in
  'help'	) do_usage      ;;
  'import'	) do_import $2  ;;
  'srpm'	) do_srpm $2 $3  ;;
  'prep'	) do_prep $2    ;;
esac
 
