#!/bin/sh

USE_CASTXML=1

if [ -z "`which castxml 2> /dev/null`" ]; then
	USE_CASTXML=0

	if [ -z "`which gccxml 2> /dev/null`" ]; then
		echo "You need castxml or gccxml to use this script.";
		exit;
	fi
fi;

if [ -z "`which xsltproc 2> /dev/null`" ]; then
	echo "You need xsltproc to use this script.";
	echo "You can find it in the libxslt package.";
	exit;
fi;

if [ $# -lt 2 ]; then
	echo "Usage: $0 input.cpp output.osd [struct1 struct2 ...]";
	exit;
fi;

XSL_CONVERTER="/usr/share/okteta/structures/gccxml-to-osd.xsl"

INPUT_FILE="$1";
OSD_FILE="$2";
GCCXML_FILE="${OSD_FILE%.*}.gcc.xml";

shift 2;
STRUCTS="$@"

if [ ${USE_CASTXML} -eq 1 ] ; then
	castxml --castxml-gccxml "$INPUT_FILE" -o "$GCCXML_FILE" || { echo "castxml failed"; exit; };
else
	gccxml "$INPUT_FILE" -fxml="$GCCXML_FILE" || { echo "gccxml failed"; exit; };
fi

xsltproc --stringparam "structs" "$STRUCTS" "$XSL_CONVERTER" "$GCCXML_FILE" > "$OSD_FILE" || echo "xsltproc failed";

rm "$GCCXML_FILE";
