#! /bin/csh -f 
# Last edited on 2002-01-15 15:49:29 by stolfi

set cmd = "$0"; set cmd = "${cmd:t}"
set usage = "${cmd} LANG/BOOK SIZETAG"

if ( $#argv != 2 ) then
  echo "usage: ${usage}"; exit 1
endif

set sample = "$1"; shift;
set sizetag = "$1"; shift;

set secs = ( `cat sample/${sample}/subsections-ok.tags` tot.t )
set secscm = "`echo ${secs} | tr ' ' ','`"
@ totsize = 0
@ allok = 1

# Decide if cleanup is necessary:
set sztfile = sample/${sample}/.current-sizetag
if ( -r ${sztfile} ) then
  set cursizetag = "`cat ${sztfile}`"
else
  set cursizetag = "*NONE*"
endif
if ( "/${sizetag}" != "/${cursizetag}" ) then
  echo "sizetag changed (was ${cursizetag}), cleanup..."
  /bin/rm -f sample/${sample}/{${secscm}}/{raw,gud,bad}.{tks,wfr}
endif

foreach sec ( ${secs} )
  if ( ${allok} ) then
    echo "=== making ${sample}/${sec} (${sizetag}) ==="
    set nfile = "sample/${sample}/${sec}/${sizetag}-raw.num"
    if ( ${sec} == "tot.t" ) then
      echo ${totsize} > ${nfile}
      set rawnum = ${totsize}
    else if ( -r ${nfile} ) then
      set rawnum = `cat ${nfile}`;
      @ totsize = ${totsize} + ${rawnum}
    else
      @ allok = 0
    endif
    if ( ${allok} ) then
      echo "raw sample size = ${rawnum} (${sizetag})"
      make -f lang-sample.make \
        SAMPLE=${sample} SUBSEC=${sec} \
        SIZETAG=${sizetag} \
        all
    else
      @ allok = 0
    endif
  endif
end

# Record which size tag was used
echo ${sizetag} > ${sztfile}

foreach kind ( raw gud bad )
  dicio-wc sample/${sample}/{${secscm}}/${kind}.tks
  dicio-wc sample/${sample}/{${secscm}}/${kind}.wfr
end
