#!/bin/sh
# $Id: apply-license,v 1.7 1997/05/09 15:54:48 zeller Exp $
# add a license disclaimer to the given files if not already present
# if -l NAME is given, author is NAME 

# Copyright (C) 1996 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
# 
# This file is part of ICE.
# 
# ICE 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.
# 
# ICE 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 ICE -- see the file COPYING.
# If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ICE is the incremental configuration environment.
# For details, see the ICE World-Wide-Web page, 
# `http://www.cs.tu-bs.de/softech/ice/',
# or send a mail to the ICE developers at `ice@ips.cs.tu-bs.de'.


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 | head -1`"
  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/etc*) project=DDD; library=false;;
    */ice/etc*) project=ICE; library=false;;
    */ddd*)   project=DDD; library=true;;
    */ice*)   project=ICE; library=true;;
    */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 environment.\
For details, see the ICE World-Wide-Web page, \
`http://www.cs.tu-bs.de/softech/ice/'"'"',\
or send a mail to the ICE developers at `ice@ips.cs.tu-bs.de'"'"'.';;
    DDD)      project_detail='DDD is the data display debugger.\
For details, see the DDD World-Wide-Web page, \
`http://www.cs.tu-bs.de/softech/ddd/'"'"',\
or send a mail to the DDD developers at `ddd@ips.cs.tu-bs.de'"'"'.';;
    *)        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.,\
59 Temple Place - Suite 330, Boston, MA 02111-1307, 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

    # echo "$t"; exit 0
    
    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
