#!/bin/sh
checkcmd() {
  if test $? -ne 0
  then
    echo "failure: ${cmd}"
    exit 1
  fi
}

if test $# -ne 1
then
  echo "$0 <texfile>
  exit 1
fi
inputfile=$1
echo "inputfile=${inputfile}"
if test -f ${inputfile}.special
then
  specialfile=${inputfile}.special
  echo "specialfile=${specialfile}"
else
  specialfile=""
fi

C2LIT="c2lit.x -refnamefile Refnames"
exportmode="-export"
cmd="Extractfilenames.sh ${inputfile}"
${cmd} > filelist
checkcmd
allfiles=`cat filelist | tr '\n' ' '`

for filename in ${allfiles}
do
  if test "${specialfile}" != ""
  then
    filepath=`grep ${filename} ${specialfile}`
    if test $? -ne 0
    then
      cmd="vpath.sh ${filename}"
      filepath=`${cmd}`
      checkcmd
    fi
  else
    cmd="vpath.sh ${filename}"
    filepath=`${cmd}`
    checkcmd
  fi
  ending=`echo ${filename} | tail -c 3`
  if test "${ending}" = ".c"
  then
    exportmode="-export"
    ## echo "############### ${filename} is a C-file"
  else
    exportmode=""
    ## echo "############### ${filename} is a something else"
  fi
  if test ! -f ${filepath}
  then
    echo "$0: file ${filepath} does not exist"
    exit 1
  fi
  if test ! -f ${filename}.inc
  then
    echo "generate \"${filename}.inc\" from \"${filepath}\""
    cmd="${C2LIT} ${exportmode} ${filepath}"
    ${cmd} > tmp
    checkcmd
    cmd="lit2alt.x -C tmp"
    ${cmd} > ${filename}.inc
    checkcmd
  else
    if test ${filepath} -nt ${filename}.inc
    then
      echo "update \"${filename}.inc\" from \"${filepath}\""
      cmd="${C2LIT} ${exportmode} ${filepath}"
      ${cmd} > tmp
      checkcmd
      cmd="lit2alt.x -C tmp"
      ${cmd} > ${filename}.inc
      checkcmd
    fi
  fi
done
rm -f tmp filelist
