#!/bin/sh

KIDL_FILE="$1"
SKEL_FILE="$2"
STUB_FILE="$3"
STUB_H_FILE="$4"
DCOP_OBJECT="$5"

dcop_cleanup()
{
	rm -f "${KIDL_FILE}" "${SKEL_FILE}" "${STUB_FILE}" "${STUB_H_FILE}"
	return $?
}

if [ "$6" = "--conditional" ]; then
	if [ -f "${KIDL_FILE}" ] && \
	   [ -f "${SKEL_FILE}" ] && \
	   [ -f "${STUB_FILE}" ] && \
	   [ -f "${STUB_H_FILE}" ]; then
		echo "don't need to generate the DCOP stub object."
		exit 0
	fi
elif [ "$6" = "--clean" ]; then
	echo "removing DCOP files..."
	dcop_cleanup
	if [ $? -eq 0 ]; then
		echo "done."
		exit 0
	else
		echo "FAILED!"
		exit 1
	fi
fi

echo "generating DCOP stub object..."
dcop_cleanup
if [ $? -ne 0 ]; then
	echo "cleanup FAILED!"
	exit 1
fi
dcopidl "${DCOP_OBJECT}" > "${KIDL_FILE}"
if [ $? -ne 0 ]; then
	echo
	echo "\"dcopidl\" FAILED!"
	echo "  Please make sure that the"
	echo "  program \"dcopidl\" is installed and in your \$PATH."
	echo "  \"dcopidl\" is most likely part of the"
	echo "  kde-devel packages for your distribution."
	echo
	exit 2
fi
dcopidl2cpp "${KIDL_FILE}"
if [ $? -ne 0 ]; then
	echo
	echo "\"dcopidl2cpp\" FAILED!"
	echo "  Please make sure that the"
	echo "  program \"dcopidl2cpp\" is installed and in your \$PATH."
	echo "  \"dcopidl2cpp\" is most likely part of the"
	echo "  kde-devel packages for your distribution."
	echo
	exit 3
fi
echo "done."
exit 0
