#!/bin/bash
set -e

CHANGELOG="debian/changelog"

# get first row from debian/changelog
FIRST_LINE=$(head -n 1 "$CHANGELOG")

# extract "1-180" from "actionswf (1-180) jammy; urgency=medium"
VERSION=$(echo "$FIRST_LINE" | sed -n 's/^[^(]*(\([^)]*\)).*/\1/p')

if [ -z "$VERSION" ]; then
    echo "Error: could not extract version from $CHANGELOG" >&2
    exit 1
fi

# metadata string, version substituted in
METADATA='\0\0\0\0\0\0<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"> <rdf:Description rdf:about=\"\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"> <dc:description>actionswf '"$VERSION"' flashixy.com</dc:description> </rdf:Description> </rdf:RDF>'

# write version.oh
printf 'char version="%s"\n' "$METADATA" > version.oh

# write version.h
printf 'static unsigned char version[]="%s";\n' "$METADATA" > version.h

echo "Version: $VERSION"
echo "Wrote version.oh and version.h"
