#!/bin/sh

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

# Bourne shell script: mtt_update
# Generates Updates core representations
# Copyright (C) 2000 by Peter J. Gawthrop

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.12  2000/10/14 16:11:00  peterg
## Made all variables, states etc lower case
##
## Revision 1.11  2000/10/13 11:07:00  peterg
## Added initialisation for state and input
##
## Revision 1.10  2000/10/13 10:56:07  peterg
## Now uses the full name from the struc file - no need to recreate.
##
## Revision 1.9  2000/10/13 09:55:09  peterg
## Changed state/input name to $4_$3
##
## Revision 1.8  2000/10/13 09:28:16  peterg
## Changed regexp to /\(.*$old.*=\)/
##
## Revision 1.7  2000/10/13 08:56:51  peterg
## Output variables as column, not row.
##
## Revision 1.6  2000/10/13 08:07:24  peterg
## Added state and input - it checks against sympar as well so that
## defined variables may be used here.
##
## Revision 1.5  2000/10/12 19:24:55  peterg
## Corrected output message
##
## Revision 1.4  2000/10/12 17:57:34  peterg
## Fixed header typos
##
## Revision 1.3  2000/10/12 15:11:30  peterg
## Added the update switch
##
## Revision 1.2  2000/10/12 13:45:13  peterg
## Put in the no-file version
##
## Revision 1.1  2000/10/12 12:32:23  peterg
## Initial revision
##
##
###############################################################


sys=$1 # System name
rep=$2 # System representation
update=$3 # Update or not

if [ -z "$2" ]; then
  echo Usage mtt_update system representation [update]    
  exit
fi

rm -f mtt_empty
touch mtt_empty

case $rep in
    numpar)
	    ext=txt
	    textfile=$1_$2.$ext
	    infofile=$1_sympar.$ext
	    moreinfofile=$infofile
	    default='1.0'
	;;
    state|input)
	    ext=txt
	    textfile=$1_$2.$ext
	    infofile=mtt_list.$ext
	    moreinfofile=mtt_list_numpar.$ext
            awk '{if ($1==rep) printf("%s\n", $4)}' rep=$2 \
            <$1_struc.txt >$infofile
	    cat $infofile $1_sympar.$ext> $moreinfofile  
	    default='0.0'
	;;
    *)
	    echo Representation $rep not implemented
	    exit
esac

if [ -f "$textfile" ]; then
  # Inform User
  echo Checking $textfile
else
  case $rep in
    numpar)
           sympar2numpar_txt2txt $1
	;;
    state)
	   struc2state_txt2txt $1
	;;
    input)
	   struc2input_txt2txt $1
	;;
    *)
	
esac
  exit    
fi

# Remove tmp files
rm -f mtt_in_text mtt_in_info

# Strip files
strip_comments <$textfile | tr 'A-Z' 'a-z' >mtt_text
strip_comments <$infofile | tr 'A-Z' 'a-z' >mtt_info
strip_comments <$moreinfofile  | tr 'A-Z' 'a-z' >mtt_moreinfo

# Use awk to check file
awk '{
  if (FILENAME=="mtt_text") {
    gsub("[\t ]" ,""); # Remove whitespace
    split($0,a,"=");
    text[a[1]] = a[2];
  }
  if (FILENAME=="mtt_info"){
     info[$1] = default;
  }
  if (FILENAME=="mtt_moreinfo"){
     moreinfo[$1] = default;
  }
  }
END{
  # for (iname in info) print iname
  for (tname in text) {
    if (!(tname in moreinfo)) print tname >> "mtt_in_text"
  }
  for (iname in info) {
    if (!(iname in text)) print iname >> "mtt_in_info"
  }
  }'  default=$default mtt_text mtt_info mtt_moreinfo


if [ -z "$update" ]; then
  if [ -f "mtt_in_text" ]; then
    echo "The following variables are defined in $textfile, but do not exist:"
    sort mtt_in_text | awk '{printf("\t%s\n",$1)}'
    echo Use mtt $sys $rep $ext to update
  fi
  if [ -f "mtt_in_info" ]; then
    echo "The following variables exist, but are not defined in $textfile:"
    sort mtt_in_info | awk '{printf("\t%s\n",$1)}'
    echo Use mtt $sys $rep $ext to update
  fi
  exit    
fi
answered=''
if [ -f "mtt_in_text" ]; then
  in_text=`sort mtt_in_text`
  echo "The following variables are defined in $textfile, but do not exist:"
  sort mtt_in_text | awk '{printf("\t%s\n",$1)}'


  while [ -z "$answered" ]; do
    echo "Update $textfile (y/n)?"
    read answer < /dev/tty
    case $answer in
	y)
	    answered=yes
 	    for old in $in_text; do
              echo Commenting out $old
	      sed "s/\(.*$old.*=\)/## Removed by MTT on `date`: \1/"\
               <$textfile > mtt_tmp
              mv mtt_tmp $textfile
              changed=yes
	    done
	    ;;
	n)
	    answered=yes
	    ;;
	*)
    esac
  done      
fi

answered=''
if [ -f "mtt_in_info" ]; then
  in_info=`sort mtt_in_info`
  echo "The following variables exist, but are not defined in $textfile:"
  sort mtt_in_info | awk '{printf("\t%s\n",$1)}'

  while [ -z "$answered" ]; do
    echo "Update $textfile (y/n)?"
    read answer < /dev/tty
    case $answer in
	y)
	    answered=yes
 	    for new in $in_info; do
                echo Adding $new
		echo "$new = $default; # Added by MTT on `date`" >> $textfile
                changed=yes
	    done
	    ;;
	n)
	    answered=yes
	    ;;
	*)
    esac
  done      
fi
