#!/bin/bash

###############################################################################
###############################################################################
##
##  Copyright (C) 2008 Fabio M. Di Nitto <fabbione@fabbione.net>
##  
##  This copyrighted material is made available to anyone wishing to use,
##  modify, copy, or redistribute it subject to the terms and conditions
##  of the GNU General Public License v.2.
##
###############################################################################
###############################################################################

set -e

definefile="$1"
define="$2"
release="$3"
srcdir="$4"
infile="$5"

definedata="$(cat $definefile | grep "^\#define $define" | sed -e 's/.*'$define' //')"

realinfile="$(ls $srcdir/$infile.*)"

interpreter="$(cat $realinfile | head -n 1 | awk -F "/" '{print $NF}')"
interpreter="$(echo $interpreter)"

case "$interpreter" in
	perl)
		start="\$"
		end=";"
	;;
	*)
		start=""
		end=""
	;;
esac

awk "{print}(\$1 ~ /#BEGIN_VERSION_GENERATION/){exit 0}" $realinfile
echo ${start}RELEASE_VERSION=\"${release}\"${end}
echo ${start}REDHAT_COPYRIGHT=${definedata}${end}
echo ${start}BUILD_DATE=\"\(built $(date)\)\"${end}
awk -v p=0 "(\$1 ~ /#END_VERSION_GENERATION/){p = 1} {if(p==1)print}" $realinfile

exit 0
