#!/bin/bash

set -e

P=$1; [ -z "$P" ] && P="."
P=$(readlink -e $P)

cd $(dirname $P)
P=$(basename $P)

###########################################################################
V=$(grep -i "^Version: " $P/$P.spec | awk '{print $2}')
R=$(grep -i "^Release: " $P/$P.spec | awk '{print $2}')
D=$(date +%Y.%m.%d)
###########################################################################
if [ "$V" = "$D" ]
then
  R=$((R+1))
else
  V=$D
  R=1
fi
###########################################################################
#V="0.0.0"
#R="0.dev"
###########################################################################
sed -i "s/^Version:.*/Version: $V/g;s/^Release:.*/Release: $R/g" $P/$P.spec
###########################################################################

tar czf $P.tar.gz $P
rpmbuild -bb $P/$P.spec

rm -rf \
   ~/rpm/BUILD/* \
   ~/rpm/SOURCES/*.tar.gz
