#!/bin/sh
#-----------------------------------------------------------------------------
# mkinst: Generate template instantiation files from a list
#-----------------------------------------------------------------------------
#
#   Copyright (C) 1996,1997,2000,2002
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: casa-feedback@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: mkinst [-q] <file>
#-----------------------------------------------------------------------------
# mkinst generates a template instantiation file for each entry in the
# specified file, ("templates" by default).  It leaves the resulting .cc files
# in the current directory.
#
# The format of the entries in the templates file is
#
#   <ident> <.cc file> <.h file> [<extra .h files>]
#
# The system accepts any name consisting of lowercase letters also as 
# include files (like the standard vector e.g.)
#
# The 
#   <ident> <.cc file> <.h file> [<extra .h files>] 
# part has to be on one line (the first line), the first <template declaration>
# can be on the same line. More declarations (on separate lines) are
# possible.
# Each declaration line can have either of 7 forms:
#	= ....                     (include file)
#       #namespace [<name>]        (optional namespace; casa if no such line))
#	template .......           (template class or function instantiation)
#	template <> class ....	   (template-less forward declaration)
#	#if .....                  (or #ifdef, #ifndef)
#       #else                      (optional)
#	#endif                     (required for any #if)
#       AIPS_*_AUX_TEMPLATES(...)  (auxilliary definition of templates, e.g.
#                                   for the system map)
#       typedef ... ...            (to bypass problems with ',' in macro)
#
# An example of a definition could be (shown in format as output by reident):
# 1020 aips/Arrays/Array.cc aips/Arrays/Vector.h trial/Arrays/SquareMatrix.h
#      = trial/Arrays/SquareMatrix.h
#      template <> class ostream 
#      template class Array<Vector<SquareMatrix<Float, 2> > > 
#      #if !defined(AIPS_STDLIB) 
#      template class Array<Vector<SquareMatrix<Float, 4> > > 
#      #endif 
# or
#
#   <ident> <.h file> [<extra .h files>] <template declaration>
#
# The latter form is for instantiation of purely inline functions.  The .cc
# and .h files are expressed as relative pathnames with respect to
# $AIPSROOT/code/include.  For example:
#
#  1050 aips/Set/Set.cc template class Set<Int>
#  1040 aips/Set/Set.cc aips/Cube/Cube.h template class Set<Cube>
#  1060 aips/Set/Set.cc 
#       template Set<Int> operator+(Set<Int> const &, Set<Int> const &)
#
# The ident contains a 4-digit number which is unique amongst all templates
# based on a particular class implementation .cc or .h file.  The name of the
# template instantiation .cc file is composed of the name of the class .cc or
# .h file and the ident, for example, the instantiation file for the first
# entry above would be Set_1050.cc.
#
# A namespace line indicates the begin of a namespace and ends the previous
# one (if any). A namespace line is valid until the end of the ident group
# or until the next namespace line.
# The namespace's name must be given after the namespace keyword.
# No name means that no namespace should be used.
# No namespace line at all means that the default casa namespace is used.
#
# Comments in the templates file are signalled by a "#" in the first field.
# Blank lines are also allowed.
#
# Any template instantiation .cc file in the current directory which does not
# correspond to an entry in the templates file will be deleted.  These files
# are recognized by having names of the form "*_[0-9][0-9][0-9][0-9].cc".
#
# In all cases it has been assumed that the format has been checked and
# made canonical with the reident program.
#
# Options:
#   -q     Run quietly.
#
# Status returns:
#    0:  success
#    1:  the templates file contains one or more bad entries
#
# Notes:
#    1) The templates file is normally sorted on the name of the .cc or .h
#       file so that it can easily be checked that the idents are unique.
#
#    2) If a templates file entry is changed then its ident should also be
#       changed to force its instantiation file to be regenerated.
#
# Original: 1996/04/17 by Mark Calabretta, ATNF after a perl script by
#           Shelby Yang, NRAO.
# $Id$
#-----------------------------------------------------------------------------

  echo "mkinst: Processing template instantiation files..."

  [ -f templates ] && exec 3<templates

# Parse options.
  VERBOSE=1
  while [ "$#" -gt 0 ]
  do
     case $1 in
     -q)
        VERBOSE=
        ;;
     -)
        exec 3<&0
        break
        ;;
     *)
        exec 3<$1
        break
        ;;
     esac

     [ "$2" = "" ] && break

     shift
  done

  rm -f before after tmp.$$ tmp1.$$
  trap 'echo "Cleaning up..." ; \
        rm -f before after tmp.$$ tmp1.$$ ; \
        trap - 0 ; \
        exit' 0 1 2 3 15

  ls -1 *_[0-9][0-9][0-9][0-9].cc > before 2>/dev/null
  touch after

  STATUS=0

  OLD=0
  REPL=0
  NEW=0
  BAD=0

# indicate start
  TFILE=""
  NAMESPACEDONE=0
  NAMESPACE="casa"
  while read ENTRY 0<&3
  do
    [ "$ENTRY" = "" ] && continue

#    Disable filename generation.
    set -f

    set $ENTRY

    case $1 in
    \#namespace)
      if [ $NAMESPACEDONE = 1  -a  "$NAMESPACE" != "" ]
      then
        echo "} //# NAMESPACE - END" >> tmp1.$$
      fi
      NAMESPACEDONE=0
      NAMESPACE=`echo $* | sed -e 's/.*namespace//' -e 's/ //g'`
      if [ "$NAMESPACE" = "" ]
      then
        NAMESPACEDONE=1
      fi
      continue
      ;;
    \#if*)
      if [ "$TFILE" = "" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi

      if [ "$NAMESPACEDONE" = 0 ]
      then
        NAMESPACEDONE=1
        echo "namespace $NAMESPACE { //# NAMESPACE - BEGIN" >> tmp1.$$
      fi
      echo "$*" >> tmp1.$$
      continue
      ;;
    \#endif)
      if [ "$TFILE" = "" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi
      echo "$*" >> tmp1.$$
      continue
      ;;
    AIPS_*_AUX_TEMPLATES*)
      if [ "$TFILE" = "" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi
      if [ "$NAMESPACEDONE" = 0 ]
      then
        NAMESPACEDONE=1
        echo "namespace $NAMESPACE { //# NAMESPACE - BEGIN" >> tmp1.$$
      fi
      echo "$*" >> tmp1.$$
      continue
      ;;
    typedef)
      if [ "$TFILE" = "" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi
      if [ "$NAMESPACEDONE" = 0 ]
      then
        NAMESPACEDONE=1
        echo "namespace $NAMESPACE { //# NAMESPACE - BEGIN" >> tmp1.$$
      fi
      echo "$*;" >> tmp1.$$
      continue
      ;;
    =)
      if [ "$TFILE" = "" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi
      shift
      for TOKEN
      do
	case $TOKEN in
        *.cc | *.h | *[a-z][a-z][a-z] | [a-z]*646)
	  echo "#include <$TOKEN>" >> tmp1.$$
	  ;;
        *)
          echo "   Bad entry for ${TFILE}:" 1>&2
          echo "        $ENTRY" 1>&2

          rm -f tmp1.$$
	  TFILE=""

          NEW=`expr $NEW - 1`
          BAD=`expr $BAD + 1`
          STATUS=1
          break
          ;;
        esac
	shift
      done
      continue
      ;;
    template)
      if [ "$TFILE" = "" ]
      then
	echo "   Bad entry skipped:" 1>&2
	echo "        $ENTRY" 1>&2
	BAD=`expr $BAD + 1`
	STATUS=1
	continue
      fi
      if [ "$2" = "<>" ]
      then
	shift
	shift
      fi
      if [ "$NAMESPACEDONE" = 0 ]
      then
        NAMESPACEDONE=1
        echo "namespace $NAMESPACE { //# NAMESPACE - BEGIN" >> tmp1.$$
      fi


      echo "$*;" >> tmp1.$$

      continue
      ;;
    \#*)
      continue
      ;;
    [0-9][0-9][0-9][0-9])
      if [ $NAMESPACEDONE = 1  -a  "$NAMESPACE" != "" ]
      then
        echo "} //# NAMESPACE - END" >> tmp1.$$
      fi
      NAMESPACEDONE=0
      NAMESPACE="casa"
      if [ "$TFILE" != "" ]
      then
	if [ -f "$TFILE" ]
	then
	  if tail +2 $TFILE | diff - tmp1.$$ >/dev/null 2>&1
          then
            [ "$VERBOSE" ] && echo "   $TFILE unchanged"
            OLD=`expr $OLD + 1`
            rm -f tmp1.$$
	    TFILE=""
          else
            echo "   Replacing $TFILE"
            REPL=`expr $REPL + 1`
	  fi
	fi
        if [ "$TFILE" != "" ]
        then
          rm -f $TFILE
          echo "// $TFILE -- `date` -- `whoami`" > $TFILE
          cat tmp1.$$ >> $TFILE
          NEW=`expr $NEW + 1`
          rm -f tmp1.$$
        fi
      fi
      TFILE=""
      BASENAME=`echo $2 | sed -e '{s#.*/##;s#\.cc$##;s#\.h$##;}'`
      if [ "$BASENAME" = "" -o "$BASENAME" = "$2" ]
      then
        echo "   Bad entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
        STATUS=1
        continue
      fi
      TFILE="${BASENAME}_$1.cc"
      NAMESPACEDONE=0
      NAMESPACE="casa"
#    Check for duplicates.
      if grep "^$TFILE" after >/dev/null 2>&1
      then
	echo "   Duplicate entry skipped:" 1>&2
        echo "        $ENTRY" 1>&2
        BAD=`expr $BAD + 1`
	TFILE=""
        STATUS=1
        continue
      fi
      echo "$TFILE" >> after

      touch tmp1.$$
      shift

      for TOKEN
      do
	case $TOKEN in
        template)
	  echo "$*;" >> tmp1.$$
          shift
          [ "$VERBOSE" ] && echo "$*"
          break
	  ;;
        *.cc | *.h | *[a-z][a-z][a-z] | [a-z]*646)
	  echo "#include <$TOKEN>" >> tmp1.$$
	  ;;
        *)
          echo "   Bad entry for ${TFILE}:" 1>&2
          echo "        $ENTRY" 1>&2

          rm -f tmp1.$$
	  TFILE=""

          NEW=`expr $NEW - 1`
          BAD=`expr $BAD + 1`
          STATUS=1
          break
          ;;
        esac
	shift
      done

      continue
      ;;
    esac

  done
  if [ $NAMESPACEDONE = 1  -a  "$NAMESPACE" != "" ]
  then
    echo "} //# NAMESPACE - END" >> tmp1.$$
  fi
  NAMESPACEDONE=0
  NAMESPACE=casa

# Write last
  if [ "$TFILE" != "" ]
  then
    if [ -f "$TFILE" ]
    then
      if tail +2 $TFILE | diff - tmp1.$$ >/dev/null 2>&1
      then
        [ "$VERBOSE" ] && echo "   $TFILE unchanged"
        OLD=`expr $OLD + 1`
        rm -f tmp1.$$
        TFILE=""
      else
        echo "   Replacing $TFILE"
        REPL=`expr $REPL + 1`
      fi
    fi
    if [ "$TFILE" != "" ]
    then
      rm -f $TFILE
      echo "// $TFILE -- `date` -- `whoami`" > $TFILE
      cat tmp1.$$ >> $TFILE
      NEW=`expr $NEW + 1`
      rm -f tmp1.$$
    fi
  fi
  TFILE=""

# Delete defunct template instantiation files.
  ZAP=0
  sort after | diff - before | sed -n -e 's/^> //p' |
  {
     while read ENTRY
     do
        rm -f $ENTRY
        echo "   Removed $ENTRY"
        ZAP=`expr $ZAP + 1`
     done

     echo "   Old: $OLD unchanged"
     case "$REPL" in
     1)
        echo "   New: $NEW ($REPL replacement)"
        ;;
     *)
        echo "   New: $NEW ($REPL replacements)"
        ;;
     esac
     echo "   Bad: $BAD syntax errors"
     echo "   Zap: $ZAP removed"
  }

  rm -f before after tmp.$$ tmp1.$$
  trap - 0
  exit $STATUS
