#!/bin/bash

# Script to compile a resource file for a DLL if there is a .rc file
# for it. The resource source file is supposed to contain a version
# info section, that uses the string BUILDNUMBER as the least
# significant part of the version numbers. This script replaces that
# string with a "build number" before compiling the binary resource
# file. The build number is kept between builds in a "stamp" file, and
# incremented each time. (If there is no stamp file, build number 0 is
# used.) The intention is that only the "official" maintainer of a DLL
# keeps such a stamp file, and thus the DLLs he releases have
# increasing version number resources, which can be used by an
# installer program to decide whether to replace an existing DLL with
# the same name.

# This is just my (tml@iki.fi) idea, if somebody comes up with a
# better way to generate version number resources, don't hesitate to
# suggest.

# The command line arguments are:
# $1: the name of the .rc file to check
# $2: the name of the resource object file to produce, if the rc file exists

# Check if we have a resource file for this DLL.
rcfile=$1
resfile=$2
if [ -f $rcfile ]; then
    # Check if we have a build number stamp file.
    basename=`basename $rcfile .rc`
    if [ -f $basename-build.stamp ]; then
	read number <$basename-build.stamp
	buildnumber=$[number+1]
	echo Build number $buildnumber
	echo $buildnumber >$basename-build.stamp
    else
	echo Using zero as build number
        buildnumber=0
    fi

    m4 -DBUILDNUMBER=$buildnumber <$rcfile >$$.rc &&
	windres $$.rc $resfile &&
	rm $$.rc
else
    # Return failure
    exit 1
fi
