#!/bin/sh

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

# Bourne shell script: mtt_make_sympar
# Makes the sympar.txt file
# Copyright (C) 2000 by Peter J. Gawthrop

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.11  2002/04/28 18:41:27  geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.10  2001/07/28 21:10:18  geraint
## Generate warning instead of error if reserved word used.
##
## Revision 1.9  2001/07/23 05:16:39  geraint
## Simple filter for Reduce reserved words in sympar.
##
## Revision 1.8  2001/07/04 06:00:12  gawthrop
## Fixed a funny with tr - changed SEPS and REPS - something to do with ^
##
## Revision 1.7  2001/04/13 07:14:12  geraint
## Implemented lower level lbl.txt recognition of #NOT[V|P]AR
##
## Revision 1.6  2001/04/10 12:54:50  gawthrop
## Minor fixes for sensitivity versions
##
## Revision 1.5  2000/11/03 14:57:06  peterg
## Corrected regexp notvar
##
## Revision 1.4  2000/11/02 18:53:46  peterg
## Added NOPAR words to unwanted list.
##
## Revision 1.3  2000/10/16 09:41:29  peterg
## Fixed bug with printing a blank line.
##
## Revision 1.2  2000/10/16 09:10:08  peterg
## Now strips out "unwanted" bits frome the _aliased.txt file
##
## Revision 1.1  2000/10/12 19:24:28  peterg
## Initial revision
##
##
###############################################################


sys=$1 # System name
filename=$1_sympar.txt

use_reduce=$2

# Inform user
echo Creating $filename

# Create list of all sympars including those that are aliased
sh $1_type.sh 'echo ' ' ' ' ' |\
gawk '{printf("abg2sympar_m2txt %s %s\n ", $1, $2)}' |\
sh | sort -u > mtt_all_sympar.txt

# Sort the aliased list
mtt_strip_args < $1_aliased.txt | sort -u  > mtt_aliased_sort.txt

# Create list of unwanted stuff
cat  mtt_aliased_sort.txt> mtt_unwanted.txt

notvar="[%|#]NOT[V|P]AR";
sh $1_type.sh 'strip_notvars.sh ' ' ' ' mtt_unwanted.txt'

sort -u mtt_unwanted.txt > mtt_unwanted_sort.txt

# Compare it with the aliased list and show the differences
diff mtt_all_sympar.txt mtt_unwanted_sort.txt |\
gawk '{if ($1=="<") printf("%s\t%s\n", $2,$3)}' > mtt_sympar.txt

# Assume same name in different system is the same
gawk '{
    if ($1==name){
	sysname[i++]=$2
    }
    
    if ($1!=name){
        if (length(name)>0){
          printf("%s\t",name)
          comma="";
          for (j=1;j<=i;j++){
            printf("%s%s", comma, sysname[j]);
            comma=",";
          }
          printf("\n")
        }
        i=1;
        sysname[1]=$2;
    }
    name = $1
}
END{
        printf("%s\t",name)
        comma="";
        for (j=1;j<=i;j++){
          printf("%s%s", comma, sysname[j]);
          comma=",";
        }
        printf("\n")

}' < mtt_sympar.txt >$filename

if [ ! "${use_reduce}" == "no" ]; then
    reserved_words=`\
    cat ${MTT_LIB}/reduce/reserved_words.txt	|\
    gawk '{printf "%s ", $0}'			|\
    tr [a-z] [A-Z]`
    sympar_words=`\
    cat ${filename}				|\
    cut -f1					|\
    gawk '{printf "%s ", $0}'			|\
    tr [a-z] [A-Z]`
    flag=0
    for reserved_word in ${reserved_words}; do
	for sympar_word in ${sympar_words}; do
	    if [ ${sympar_word} = ${reserved_word} ]; then
		echo ""
		echo "*** MTT Warning:"
		echo "    ${sympar_word} is reserved (Reduce)"
		echo ""
	    fi
	done
    done
fi
