#!/bin/bash
#
# Create debian files from *.in files
#
# $1 = path to debian files
#
if [ $# != 1 ]; then
  echo 'Usage : fixdeb path'
  exit 1
fi

PACKAGEVERSION=`head -n 1 $1/changelog | awk '{ print $2 }' | tr -d '[()]'`
FPCVERSION=`echo $PACKAGEVERSION | awk -F '-' '{ print $1 }'`
DEBVERSION=`echo $PACKAGEVERSION | awk -F '-' '{ print $2 }'`

# Snapshot ?
if [ "$DEBVERSION" == "0" ]; then
  OLDPACKAGEVERSION=$PACKAGEVERSION
  DEBVERSION=`/bin/date --utc +%Y%m%d`
  PACKAGEVERSION=$FPCVERSION-$DEBVERSION
  sed s+$OLDPACKAGEVERSION+$PACKAGEVERSION+ $1/changelog > $1/changelog.tmp
  mv $1/changelog.tmp $1/changelog
fi

echo 'PackageVersion: ' $PACKAGEVERSION
echo 'FPCVersion    : ' $FPCVERSION
echo 'DebVersion    : ' $DEBVERSION

for i in $1/*.in
do
  j=${i/.in/}
  sed -e 's/%{fpcversion}/'$FPCVERSION'/g;s/%{packageversion}/'$PACKAGEVERSION'/g' $i > $j
done
