#!/bin/sh
out="$1"       ; shift
version="$1"   ; shift
buildtype="$1" ; shift
empty="0000000000000000000000000000000000000000"

sha()
{
    grep="$1"; shift
    printf "%s\n" "$@" | grep $srcdir/$grep | while read f; do
        cat "$f" | $sha1sum | cut -d' ' -f1
    done | sort | $sha1sum | cut -d' ' -f1
}

src=$(sha divine "$@")
runtime=$(sha dios "$@")
relsha=$(cat $srcdir/releng/checksum)

if test $out = /dev/null; then
    echo $src $runtime
    exit 0
fi

{
    echo "const char *DIVINE_VERSION = \"$version\";"
    echo "const char *DIVINE_SOURCE_SHA = \"$src\";"
    echo "const char *DIVINE_RUNTIME_SHA = \"$runtime\";"
    echo "const char *DIVINE_BUILD_TYPE = \"$buildtype\";"
    echo "const char *DIVINE_RELEASE_SHA = \"$relsha\";"
} > $out.new

if diff -u $out $out.new; then
    rm -f $out.new
    echo unchanged 1>&3
else
    mv $out.new $out
fi
