#!/bin/sh
# $Id: kumir.in 6047 2010-11-18 12:24:38Z cher $
# Copyright (c) 2010 Alexander Chernov <cher@ejudge.ru>

# This is a `compilation' script for the Kumir language.
# Usage: kumir <input> <output>

# using EJUDGE_FLAGS we may pass additional flags

LANG_CONFIG_DIR="/home/judges/compile/conf/lang.d"
[ "${EJUDGE_LANG_CONFIG}" = "" ] && EJUDGE_LANG_CONFIG="${LANG_CONFIG_DIR}/kumir.cfg"

if [ -f "${EJUDGE_LANG_CONFIG}" ]
then
  . "${EJUDGE_LANG_CONFIG}"
else
  version="unknown"
  KUMIRPATH="/usr/local/bin/ckumir"
fi

if [ x"${version}" = x ]
then
  echo "This language is not supported." >&2
  exit 1
fi

if grep __END__ "$1"
then
  echo "Program must not contain __END__" >&2
  exit 1
fi

"${KUMIRPATH}" --check --program-encoding=UTF-8 --messages-encoding=UTF-8 "$1"
retval=$?
if [ "$retval" != 0 ]
then
  exit $retval
fi

echo '#! /bin/sh' > "$2"
echo 'cat <<__END__ > "${0}__"' >> "$2"
cat "$1" >> "$2"
echo '__END__' >> "$2"
echo 'exec "'${KUMIRPATH}'" --run --program-encoding=UTF-8 --messages-encoding=UTF-8 "${0}__"' >> "$2"
chmod +x $2
exit 0