#!/bin/sh
# fake a .sgm file for a SINGLE corpus file
# usage:  oneeci corp comp [sub] [switches] -- e.g. one-eci dut02 a 03
#       or
#         oneeci file.eci [switches]        -- e.g. one-eci ../eci1/dut02b.eci
#       or
#         oneeci - {file.eci,corp comp [sub]} [switches] -- data from STDIN
#                                                      but code from ARGs
# remaining args are passed to eci and/or sgmls
if [ ! "$ECI_ROOT" ]
 then
  echo "Must set ECI_ROOT before using!" 1>&2
  exit 10
fi
std=0
if [ "$1" = "-" ]; then std=1; shift; fi
case $1 in
 *.*)
  line=`perl $ECI_ROOT/src/perl/unpackfn.pl $1`
  read dir corp comp sub <<EOL
$line
EOL
  shift ;;
 *)
  corp=$1
  shift
  comp=$1
  shift
  case $1 in
   [!-]*) sub=$1
          shift ;;
  esac
  dir="./" ;;
esac
if [ ! -f "$dir$corp$comp$sub.eci" ]
    then
     # try to provide a path
     nd=`echo $ECI_ROOT/data/eci?/$corp`
     echo "$dir$corp$comp$sub.eci not found, trying $nd/$corp$comp$sub.eci instead" 1>&2
     dir=$nd/
fi
case "$dir" in
 ./) ;;
 ?*)
  ddf="-dir $dir" ;;
esac
{ cat << EOS1
<!DOCTYPE eci
     PUBLIC  "-//ACL European Corpus Initiative//DTD eci//EN//"
[
<!ENTITY % eci.single "INCLUDE">
<!ENTITY % eci.simple "IGNORE">
<!ENTITY % corp.code "$corp">
<!ENTITY % comp.code "$comp">
]>
<eci id="&comp.id">
&comp.hdr;
&opentext;
<body>
<!-- Then include the text of the sub-corpus                             -->
EOS1
case $std in
   1) cat - ;;
   0) cat $dir$corp$comp$sub.eci ;;
esac
cat << EOS2
</body>
</text>
</eci>
EOS2
} | eci $ddf $@
