#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: rep_txt2tex
# Converts the text file dercribing a report to a report.
# P.J.Gawthrop August 1996
# Copyright (c) P.J.Gawthrop, 1996.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.4  2002/05/09 16:07:54  gawthrop
## Now handles multiple options whis .tex files as well
##
## Revision 1.3  2002/05/01 14:07:05  gawthrop
## Added -dr foo (reads files from foo) dir.
##
## Revision 1.2  2002/04/28 18:55:03  geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.1  2000/12/27 15:57:31  peterg
## Initial revision
##
## Revision 1.15  2000/04/06 10:57:12  peterg
## Removed debug line
##
## Revision 1.14  2000/04/06 10:51:14  peterg
## replaces $PWD by `pwd` for sh compatibility
##
## Revision 1.13  1999/10/19 23:47:27  peterg
## Rewrote for the extra representations - but with .ps at the end
## Need to retrofit old desc files using multiple figures
##
## Revision 1.12  1999/10/19 03:33:01  peterg
## Temporaraly removed argument stripping
##
## Revision 1.11  1999/02/17 02:00:29  peterg
## Absolute pathname for files.
## Indirect include via \input
##
## Revision 1.10  1998/08/25 12:45:27  peterg
## Added icad to languages
##
## Revision 1.9  1998/03/09 13:26:22  peterg
## Removed `system' from part and section headers.
##
## Revision 1.8  1998/02/09 08:59:35  peterg
## Minor debugging lines removed.
##
## Revision 1.7  1998/01/29 19:32:33  peterg
## Removed a spurious X from the file -- tried to run  the X server !!!
##
## Revision 1.6  1997/12/04 22:08:52  peterg
## Handles multiple odes.ps files
##
## Revision 1.5  1997/12/04 17:05:57  peterg
## Now handles multiple odeso.ps files
##
## Revision 1.4  1997/08/25 09:15:48  peterg
## Don't do sections for unrecognised languages.
##
# Revision 1.3  1997/06/27  13:28:25  peterg
# New version using explicit command line arguments in the text file
#
## Revision 1.2  1997/05/19 16:11:36  peterg
## Modified section headers.
##
# Revision 1.1  1996/08/18  20:00:38  peter
# Initial revision
#
###############################################################

documenttype=$2; 
mtt_switches=$4

## Remove -u and -q
mtt_switches=`echo ${mtt_switches} | sed 's/-[qu]//g'`

if [  "$documenttype" = "book" ]; then # do book version
  # Inform user
  echo "Creating $1_rep.tex (directory version)"
  strip_comments < $1_rep.txt|\
  gawk '
BEGIN{
oldpart = ""
}
{
  # Find directory name.
  for (i=1;i<NF;i++){
    if ($i=="-d") 
       directory=$(i+1);
  }
  # Find next to last name
  N=split(directory,a,"/");
  if (N>1) 
    part = a[N-1];
  if (length(part)>0)
    if (part!=oldpart)
      printf("\\part{\\textbf{%s}}\n", part);
  oldpart=part;
  printf("\\chapter{\\textbf{%s}}\n", $(NF-2));
  printf("\\label{chap:%s(%s)}\n", $(NF-2), directory);
  printf("\\input{%s/%s_rep}\n\n", directory, $(NF-2));
  }' > $1_rep.tex


else # do article and section version

if [ -z "$3" ]; then
  directory="";
else
  directory=$3/;
fi

# Inform user
echo "Creating $1_rep.tex"

# Create the LaTeX file
strip_comments <$1_rep.txt |\
gawk '
{
  ## Add in the external switches
  sub("mtt", sprintf("mtt %s", mtt_switches));
  
  ## Count the switches (noting that -i -dr -dw and -de have an argument)
  ## and make up a string (switches) containing them
  Switches=0; switches = "";
  for (i=2;i<=NF;i++) {
    if (match($i,"-")==1) {
      Switches++;
      switches=sprintf("%s%s", switches, $i);
    }
    if (match($i,"-i")==1) {
      Switches++;
      j=i+1;
      switches=sprintf("%s%s", switches, $j);
    }
    if (match($i,"-dr")==1){
      Switches++;
      j=i+1;
      switches=sprintf("%s%s", switches, $j);
    }
    if (match($i,"-dw")==1){
      Switches++;
      j=i+1;
      switches=sprintf("%s%s", switches, $j);
    }
    if (match($i,"-de")==1){
      Switches++;
      j=i+1;
      switches=sprintf("%s%s", switches, $j);
    }
  }
  print "%%%" switches
  System = $(Switches+2)
  Representation = $(Switches+3);
  Multiple = "odes odeso sms smso daes daeso";
  Language = $(Switches+4);
  Languages = "tex txt r m c h ps icad";
  args = $(Switches+5);
  quote = "\047";
  args = gensub(quote, "", "g",  args);
  args = gensub(/"/, "", "g",  args);
#  args = substr(Args,2,length(Args)-2);
#  if ( (length(Args)>0)&&(match(Multiple,Representation)>0)) {
  if ( (length(args)>0)&&(match(Language,"ps")>0)) {
    _Args = sprintf("-%s", args); 
    __Args = sprintf("-%s", gensub(/_/, "_", "g", args) );
    ___Args = sprintf("-%s", gensub(/,/, "-", "g", args) );
  }
  else
  {
  _Args = "";
  __Args = "";
  ___Args = "";
  }

  ## Add in the mtt args
  __Args = sprintf("%s%s", switches, __Args);

  ## Treat no arguments as -noargs
  if (length(__Args)==0){
    __Args = "-noargs";
    header_args = "";
  }
  else {
    header_args = __Args;
    gsub("-", " -", header_args);
    header_args = sprintf("(%s)", header_args); 
  }

print "%%" __Args "%%"
#  print "DEBUG", Language, args, _Args, __Args
  RepLang = sprintf("%s%s.%s", Representation, __Args, Language);
  SysRepLang = sprintf("%s_%s", System, RepLang);
  SysRepLangArgs = sprintf("%s_%s", System, RepLang, __Args);
  SysRep = sprintf("%s_%s%s", System, Representation, __Args);
  filename = gensub(/,/, "-", "g", SysRep);
  FileName = sprintf("%s/%s", PWD, filename);
  section_head = sprintf("\\textbf{%s\\_%s.%s} \\emph{%s}",
   System,Representation,Language, header_args);
   
# section headings
    if (match(Languages,Language)>0) {
    print "\n\n\\section{" section_head "}";
    print "\\label{sec:" SysRepLangArgs "}";
    print "\\index{\\textbf{" System "} -- " Representation "}";
    
    print "\n\nMTT command:";
    print "\\begin{verbatim}";
      for (i=1;i<=NF;i++) printf("%s ", $i);
    print "\n\\end{verbatim}";
    }

# tex files
    if( match("tex",Language)>0) {
      printf("  \\input{%s}\n", FileName);
#      command = sprintf("sed s!fig{!fig{%s! < %s_%s.%s", Directory, System, Representation, Language);
#      system(command);    
}

# text files
    if( match("txt r m c h icad",Language)>0) {
      print "  \\begin{verbatim}";
      command = sprintf("cat %s.txt", filename);
      system(command);
      print "  \\end{verbatim}";  
    }
# ps files
    if( match("ps",Language)>0) {
      printf("This representation is given as Figure \\Ref{fig:%s}.\n", SysRepLangArgs);
      Label = sprintf("%s", SysRepLangArgs);
      Caption = sprintf("System \\textbf{%s}, representation %s (%s)", System, Representation, __Args);
      printf("\\fig{%s}\n    {%s}\n    {0.9}\n    {%s}\n",FileName,Label,Caption);
  }
}
END {

}' mtt_switches="${mtt_switches}" system=$1 Directory=$directory PWD=`pwd` > $1_rep.tex

fi
