#! /bin/sh

# $Id: inst-templ,v 1.5 2000/07/25 00:09:37 carlo Exp $
#
# Template instantiation script
#
# Copyright (C) 1999-2000, by
#
#   Carlo Wood, Run on IRC <carlo@alinoe.com>
#   RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
#   Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
#
# All rights reserved.
#
# See the file LICENSE for copyright information.
#
# This file may be distributed under the terms of the Q Public License 1.0
# as defined by Troll Tech AS of Norway and appearing in the file LICENSE.QPL
# included in the packaging of this file.
#
#-----------------------------------------------------------------------------
#
# Edit $PROTODIR/Makedefs.h to get it working for your system
#

function find_file()
{
  echo "Looking for: \"$@\""
  TMP=`eval grep -F "'$@
.o'" nm_out`
  export TARGET="$@"
  FILENAME=`echo "$TMP" | awk '
  BEGIN { max_count=0; filename=""; upat=" U " ENVIRON["TARGET"]; tpat=" T " ENVIRON["TARGET"]; }
  {
    if ($0~/^[^ ]*\.o$/)
    {
      current=$0;
      count=0;
    }
    else if (index($0, upat) || index($0, tpat))
    {
      count++;
      if (count > max_count)
      {
        max_count=count;
	filename=current;
      }
    }
  }
  END { print filename; }
  ' | awk '{ sub("\.o$",".cc",$0); print }'`
  if [ ! -z "$FILENAME" ]; then
    echo "File with most occurences: $FILENAME"
  fi
}

UNDEFINED_REFERENCE=`expr match "$1" '[^ ]*: undefined reference to \`\(.*\)'`
echo "-----------------------------------------------------------------------------"
echo "Analysing: \"$UNDEFINED_REFERENCE\""
TMP=`echo "$UNDEFINED_REFERENCE" | egrep '^[^(<:]*(operator<|operator<<)?(<.*>)?\([^)]'`
if [ -z "$TMP" ]; then
  TYPE=`expr match "$UNDEFINED_REFERENCE" "\([^(]*>\)::operator"`
  if [ -z "$TYPE" ]; then
    TYPE=`expr match "$UNDEFINED_REFERENCE" "\([^(]*>\)::[^<>()]* virtual table"`
    if [ -z "$TYPE" ]; then
      TYPE=`expr match "$UNDEFINED_REFERENCE" "\([^(]*>\) virtual table"`
      if [ -z "$TYPE" ]; then
	TYPE=`expr match "$UNDEFINED_REFERENCE" "\([^(]*>\)::[^<>()]*("`
      fi
    fi
  fi
  if [ -z "$TYPE" ]; then
    echo "Error in TYPE"
    find_file "$UNDEFINED_REFERENCE"
    exit -1
  fi
  echo "Type: template class";
  DATA="template class $TYPE;"
else
  TYPE="$UNDEFINED_REFERENCE"
  echo "Type: template function"
  DATA="template $TYPE;"
fi
find_file "$TYPE"
if [ -z "$FILENAME" ]; then
  echo "Can't find file with undefined reference \"$TYPE\"."
  exit -1
fi
if [ ! -e "$FILENAME" ]; then
  echo "Can't find file with undefined reference \"$TYPE\"."
  exit -1
fi
TIGFILE=`echo "$FILENAME" | sed 's/\.cc$/.tig/'`
if [ "$TIGFILE" = "$FILENAME" ]; then
  echo "Oops, almost overwriting your source files!"
  exit -1
fi
echo "Appending: \"$DATA\"" to "\"$TIGFILE\""
if [ ! -s "$TIGFILE" ]; then
  echo "// Template instantiations; Don't touch: *automatically* generated!" > "$TIGFILE"
fi
echo "$DATA" >> "$TIGFILE"
touch $FILENAME
TIGNAME=`echo "$TIGFILE" | sed 's%^.*/%%g'`
TMP=`grep '^#include ".*\.tig"$' "$FILENAME"`
if [ -z "$TMP" ]; then
  echo "#include \"$TIGNAME\"" >> "$FILENAME";
fi
