#!/bin/sh

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

# Bourne shell script: sub_sh2tex
# Converts _sub_sh files to a latex itemized list.
# P.J.Gawthrop May 1997
# Copyright (c) P.J.Gawthrop, 1997.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.7  1998/02/04 10:59:37  peterg
## Tidied up.
##
# Revision 1.6  1997/09/08  19:42:37  peterg
# Replaced copy by compcopy
#
## Revision 1.5  1997/08/15 11:20:13  peterg
## Explicitly copies compound components (if not already there)
##
# Revision 1.4  1997/06/24  08:28:26  peterg
# Now takes %SUMMARY description from the label file
# Appends count of number of subsystems at that level
#
## Revision 1.3  1997/05/21 10:07:36  peterg
## Fixed \ probs for GNU sh/echo
##
## Revision 1.2  1997/05/19 16:46:17  peterg
## *** empty log message ***
##
# Revision 1.1  1997/05/19  11:29:44  peterg
# Initial revision
#
###############################################################


#Useful strings
quote="'";
listtype="itemize"

# Maximum list depth - maybe I should learn sh arithmetic!!
# Just go to two levels
maxlevel='0+1+1'; 

#Look for a command line argument
labels=''; arg='';
while [ -n "`echo $1 | grep '-'`" ]; do
  case $1 in
	-l )
		labels='yes'; arg='-l' ;;
	*)
		echo "$1 is an invalid argument - ignoring" ;;
  esac
  shift
done


system=$1
filename=$2 


if [ -z "$3" ]; then
    level=0;
else
    level=$4;
fi

#Top-level commands
if [ -z "$2" ]; then
  filename="$1_sub.tex"

  # Inform user
  echo "Creating $filename"

  #Write some file headers
  echo "% Subsystem file for system $system ($filename)"   > $filename
  echo "% Generated by MTT at `date`"  >> $filename
  echo  >> $filename
fi

# Write list item if not at the top level.
if [ -n "$2" ]; then
  #Set up some names
  filename=$2
  figfile=$3_abg.fig
  lblfile=$1_lbl.txt
  summary='%SUMMARY'

  #Get the system description, copying where necesary
  mtt -q -u compcopy $system $MTTPATH/lib/comp/compound
  # mtt -q -u $system lbl txt
  desc=`grep $summary $lblfile | sed "s/$summary//"`

  #Count the number of subsystems
  subs=`grep -c $system $figfile`

  if [ "$labels" = "yes" ]; then
    echo '  \item '$desc ' ('$subs') Section \Ref{sec:'$system'}' >> $filename
  else
    echo '  \item' $desc ' ('$subs')'   >> $filename
  fi
fi

# Create the subsystem sh files (if not too deep)
if [ "$level" != "$maxlevel" ]; then
    mtt -q -u  -l 1 $system sub sh

    # Write the list of subsystems (if they exist).
    n_subsystems=`grep -c '$1' $1_sub.sh`

    if [ "$n_subsystems" != "0" ]; then
	cat <<EOF >> $filename
	    \begin{$listtype}
EOF
	# Recursively generate the subsystems
        level1="$level+1"
	sh $system\_sub.sh "sub_sh2tex $arg " " $filename $system $level1" 
	cat <<EOF >> $filename
	    \end{$listtype}
EOF
else
    cat  <<EOF >> $filename
         No subsystems.
EOF
fi
fi








