#! /bin/sh

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

# Bourne shell script: cmp2cmp_m
# Revises the component file (cmp) to account for aliases.

# Copyright (c) P.J.Gawthrop 1998

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
###############################################################

# Inform user
echo Revising $1_cmp.m

# Copy the alias and cmp files.
cat $1_alias.txt $1_cmp.m > junk

#Create the cmp file complete with headers.
#echo "# Component file ($1_cmp.m)" #> $1_cmp.m
#echo "# Revised by MTT at `date`" #>> $1_cmp.m

#Write out the variables 
awk '
BEGIN{
alias_file=1
Parameter_Regexp = "^_Parameter_"
Line_Regexp = "cr|arg"
quote = "\047"
}
{
  if (alias_file) {
    if (match($1,Parameter_Regexp)>0){
      alias[$2] = $1;
      sub(Parameter_Regexp,"$",alias[$2]); # Replace by $ format
    }
  }
  else {
    if (match($1,Line_Regexp)){
      cr_or_args = substr($3,2,length($3)-3); # It it a cr or arg line
      N=split(cr_or_args,a,";"); # Split into parts
      for (i=1;i<=N;i++){
        if (a[i] in alias)       # Replace by alias
          a[i] = alias[a[i]];
      }
      cr_or_args = a[1];         # Recombine
      for (i=2;i<=N;i++)
        cr_or_args = sprintf("%s;%s", cr_or_args, a[i]);
      print   "\t" $1 "\t" $2 "\t" quote cr_or_args quote
    }
    else
      print $0
  }
  if ($1=="###_END_OF_ALIAS_###")
    alias_file=0;
}' <junk > $1_cmp.m

rm -f junk








