#! /bin/sh
# $Id: bcc-version.in 5550 2009-02-19 18:14:43Z cher $
# Copyright (c) 2008 Alexander Chernov <cher@ejudge.ru>

libexecdir="/opt/ejudge/libexec"
contests_home_dir="/home/judges"
local_dir="/var/lib/ejudge"
LANG_CONFIG_DIR="/home/judges/compile/conf/lang.d"

common_config() {
  echo 'long_name="Borland C"'
  echo 'src_sfx=".c"'
  echo 'exe_sfx=".exe"'
  echo 'arch="dos"'
}

failure() {
  rm -f conftest*
  echo 'version='
  echo 'arg="'"${arg}"'"'
  common_config
  echo 'BCCRUN=false'
  [ "${verbose}" = 1 ] && echo "no" >&2
  exit 1
}

if [ x"$1" = x-v ]
then
  verbose=1
  shift
fi

# recheck the language
if [ x"$1" = x-r ]
then
  arg="$2"
  [ x"$2" != x ] && BCCRUN="$2"
  [ "${BCCRUN}" = "" ] && BCCRUN="${libexecdir}/ejudge/lang/bccemu"

  [ "${verbose}" = 1 ] && echo -n "checking whether Borland C 3.1 is available..." >&2

  [ -x "${BCCRUN}" ] || failure
  dosdir="${local_dir}"
  [ "${dosdir}" != "" ] || dosdir="${contests_home_dir}"
  [ -d "${dosdir}" ] || failure
  dosdir="${dosdir}/dosemu"
  [ -d "${dosdir}" ] || failure
  [ -x "${dosdir}/bin/dos" ] || failure
  [ -f "${dosdir}/boot/BC31/BIN/BCC.EXE" ] || failure
  version="3.1"

  rm -f ./conftest*
  echo 'version="'"${version}"'"'
  echo 'arg="'"${arg}"'"'
  common_config
  echo 'BCCRUN="'"${BCCRUN}"'"'
  [ "${verbose}" = 1 ] && echo "yes, ${BCCRUN}, ${version}" >&2
  exit 0
fi

if [ x"$1" = x-l ]
then
  echo "Borland C 3.1 (DOS mode)"
  exit 0
fi

[ "${EJUDGE_LANG_CONFIG}" = "" ] && EJUDGE_LANG_CONFIG="${LANG_CONFIG_DIR}/bcc.cfg"

if [ -f "${EJUDGE_LANG_CONFIG}" ]
then
  . "${EJUDGE_LANG_CONFIG}"
fi

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

if [ x"$1" = x-p ]
then
    echo "${BCCRUN}"
    exit 0
fi

[ x"$1" = x-f ] && echo -n "Borland C "
echo "3.1"