#!/bin/sh
# $Id: apply-license,v 1.1.1.1 1995/05/01 15:48:42 zeller Exp $
# add a license disclaimer to the given files if not already present
# if -f is given, disclaimer is added unconditionally

# Copyright (C) 1993 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller (zeller@ips.cs.tu-bs.de).
# 
# This file is part of NORA.
# 
# NORA 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.
# 
# NORA 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 NORA -- see the file COPYING.
# If not, write to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# NORA is an experimental inference-based software development
# environment. Contact nora@ips.cs.tu-bs.de for details.

# $Log: apply-license,v $
# Revision 1.1.1.1  1995/05/01  15:48:42  zeller
# DDD 0.9 distribution
#
# Revision 1.1.1.1  1995/02/09  09:14:41  zeller
# ICE 0.5 distribution
#
# Revision 1.17  1994/07/09  09:52:02  zeller
# New: support for lisp files
#
# Revision 1.16  1994/06/16  16:03:41  zeller
# New: extra texinfo support
#
# Revision 1.15  1994/06/02  14:29:56  zeller
# New: ICE support
#
# Revision 1.14  1993/10/19  13:23:55  zeller
# Neu: Manual-Support (halbherzig)
#
# Revision 1.13  1993/07/23  15:52:54  zeller
# Neu: Support fuer texinfo und configure
#
# Revision 1.12  1993/05/22  02:39:43  zeller
# %Q%-Feld hinzugefuegt
#
# Revision 1.11  1993/04/22  13:03:42  zeller
# Fix: Autorennamen unveraendert lassen
# 
# Revision 1.10  1993/04/22  10:43:48  zeller
# Fix: Abfrage auf library korrigiert
# 
# Revision 1.9  1993/04/22  10:19:49  zeller
# Lizenz verbessert
# 
# Revision 1.8  1993/04/22  10:16:52  zeller
# Neu: Backup aufgehoben
# 
# Revision 1.7  1993/04/22  10:08:07  zeller
# Neu: Alte Lizenz herausnehmen, neue einsetzen
# Neu: Verschiedene Lizenzen fuer Bibliothek und Agenten
# 
# Revision 1.6  1993/04/20  23:19:44  zeller
# Neu: logname per Option setzbar
# 
# Revision 1.5  1993/04/20  23:18:41  zeller
# Neu: Name und Mail-ID wird zur Laufzeit bestimmt
# 
# Revision 1.4  1993/04/20  09:04:47  zeller
# Fix: Tab entfernt
# 
# Revision 1.3  1993/04/16  11:22:36  zeller
# Neu: support for resource & postscript files
# 
# Revision 1.2  1993/04/16  11:14:19  zeller
# Neu: GNU Library General Public License eingefuegt
# 
# Revision 1.1  1993/04/16  11:06:53  zeller
# Initial revision
# 

this=`basename $0`
path=`dirname $0`

logname=`logname`
force_name=false
if [ "$1" = "-l" ]; then
  logname=$2
  force_name=true
  shift
  shift
fi

# prefer system V sed (does not strip initial space)
sed=sed
if [ -f /usr/5bin/sed ]; then
   sed=/usr/5bin/sed
fi

year=19`date +%y`

for file in $*; do

  written="`grep 'Written by ' $file`"
  if [ "$written" = "" -o $force_name = true ]; then
    fullname=`ypmatch $logname passwd | cut -d: -f5 | cut -d, -f1`
    mailname=$logname@`domainname | sed 's/[^\.]*\.//'`
  else
    fullname=`echo "$written" | sed 's/.*Written by *\([^(]*\) *(.*/\1/
s/ $//g'`
    mailname=`echo "$written" | sed 's/.*(\([^)]*\)).*/\1/
s/ $//g'`
  fi

  # If we have a 'lib' in the path, take the library license.
  # Use the general license otherwise.
  fullfile=`pwd`/`basename $file`  
  case $fullfile in
    */lib/*) library=true;;
    */lib)   library=true;;
    *)       library=false;;
  esac

  case $fullfile in
    */ddd*)   project=DDD;;
    */ice/*)  project=ICE;;
    */ice)    project=ICE;;
    */nora/*) project=NORA;;
    */nora)   project=NORA;;
    *)        project=FOOBAR;;
  esac

  case $project in
    NORA)     project_detail='NORA is an experimental inference-based software development\
environment. Contact nora@ips.cs.tu-bs.de for details.';;
    ICE)      project_detail='ICE is the incremental configuration engine.\
Contact ice@ips.cs.tu-bs.de for details.';;
    DDD)      project_detail='DDD is the GDB-based data display debugger.\
Contact ddd@ips.cs.tu-bs.de for details.';;
    *)        project_detail='FOOBAR is fouled up beyond all recognition.\
Contact foobar@kremvax.su.org for details.';;
  esac

  if $library; then
    What='The '$project' Library'
    what='the '$project' Library'
    license='Library '
    license_file='COPYING.LIB'
  else
    What=$project
    what=$project
    license=''
    license_file='COPYING'
  fi

  text='Copyright (C) '$year' Technische Universitaet Braunschweig, Germany.\
Written by '"$fullname ($mailname)"'.\
\
This file is part of '$what'.\
\
'$What' is free software; you can redistribute it and/or\
modify it under the terms of the GNU '$license'General Public\
License as published by the Free Software Foundation; either\
version 2 of the License, or (at your option) any later version.\
\
'$What' 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 '$license'General Public License for more details.\
\
You should have received a copy of the GNU '$license'General Public\
License along with '$what' -- see the file '$license_file'.\
If not, write to the Free Software Foundation, Inc.,\
675 Mass Ave, Cambridge, MA 02139, USA.\
\
'$project_detail'\
'
  
  c_text='/*\
'`echo "$text" | sed 's,^..,    &,'`'
*/\
'
  cpp_text=`echo "$text" | sed 's,^.,// &,'`
  sh_text=`echo "$text"  | sed 's,^.,# &,'`
  ad_text=`echo "$text"  | sed 's,^.,! &,'`
  ps_text=`echo "$text"  | sed 's,^.,% &,'`
  texi_text=`echo "$text"  | sed 's,^.,@c &,'`
  m4_text=`echo "$text"  | sed 's,^.,dnl &,'`
  man_text=`echo "$text"  | sed 's,^.,\.\\\\\\\\" &,'`
  lisp_text=`echo "$text" | sed 's,^.,;; &,'`

  bakfile=$file~
  tmpfile=LIC$$
  echo $file: >&2
  t=""
  header="`head -1 $file`"
  if [ $? = 0 ]; then
    e='^[ 	]*$'
    c='c\
'
    case "$header" in
      //*)  t="$cpp_text";;
      /\**) t="$c_text"; e='^*/$'; c=d;;
      \#*)  t="$sh_text";;
      !*)   t="$ad_text";;
      %*)   t="$ps_text";;
      @c*)  t="$texi_text";;
      *input\ texinfo*) t="$texi_text";;
      dnl*) t="$m4_text";;
      .\\\"*) t="$man_text";;
      \;*)  t="$lisp_text";;
      *)    echo "unknown file type" >&2;;
    esac
    if [ "$t" != "" ]; then

      # Remove old license, if present
      rm -f $bakfile
      ln $file $bakfile
      
      grep -s 'General Public License' $file
      if [ $? = 0 ]; then
	lineno=`grep -n '^$' $file | head -1 | cut -d: -f1`
	if [ "$lineno" = "" ]; then
	  command=2,24"$c"
	else
	  command="${lineno}",'\%'"${e}"'%'"$c"
	fi
	$sed "$command" $file > $tmpfile
	if [ $? = 0 ]; then
	  mv $tmpfile $file
	fi
      fi

      # Apply new license
      lineno=`grep -n '^$' $file | head -1 | cut -d: -f1`
      if [ "$lineno" = "" ]; then
	echo "warning: cannot find first paragraph" >&2
	lineno=3
      fi
      
      command=${lineno}'a\
'"${t}"'
'
      $sed "$command" $file > $tmpfile
      if [ $? = 0 ]; then
        mv $tmpfile $file
      fi

      # Show differences
      diff $bakfile $file
    fi
  fi
done
