#!/bin/sh
# $Id: apply-license 6884 2008-03-09 20:42:06Z apg $
# Add a license disclaimer to the given files unless already present
# If -l NAME is given, set author to NAME.
# If -f is given, add license to files that did not have one.

# Copyright (C) 1997 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller <zeller@gnu.org>.
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.
# 
# 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 <ice@ips.cs.tu-bs.de>.

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

incremental=true
logname=$LOGNAME
force_name=false

# Process options
while getopts fil: c
do
  case $c in
    f) incremental=false;;
    i) incremental=true;;
    l) logname=$OPTARG; force_name=true;;
    *) echo "$this: usage: $this [-f] [-l NAME] FILES..."
       exit 2;;
  esac
done
shift `expr $OPTIND - 1`

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

year=`date +%Y 2> /dev/null || gdate +%Y`
fullname=`ypmatch $logname passwd | cut -d: -f5 | cut -d, -f1`

for file in "$@"; do

  if [ ! -f $file ]; then
    echo "$this: $file: no such file or directory" >&2
    continue
  fi

  written_line="`grep -n 'Written by ' $file | head -1 | cut -d: -f1`"

  and_line=
  written_by=
  and_by=
  if [ "$written_line" != "" ]; then
    and_line=`expr $written_line + 1`
    written_by="`tail +$written_line $file | head -1`"
    and_by="`tail +$and_line $file | head -1 | grep ' and '`"
  fi

  fullname1=
  mailname1=
  fullname2=
  mailname2=
  if [ "$written_by" = "" -o $force_name = true ]; then
    fullname1=$fullname
    mailname1=zeller@gnu.org
  else
    fullname1=`echo "$written_by" | sed 's/.*Written by *\([^<]*\) *<.*/\1/
s/ $//g'`
    mailname1=`echo "$written_by" | sed 's/.*<\([^>]*\)>.*/\1/
s/ $//g'`
    if [ "$and_by" != "" ]; then
      fullname2=`echo "$and_by" | sed 's/.*and *\([^<]*\) *<.*/\1/
s/ $//g'`
      mailname2=`echo "$and_by" | sed 's/.*<\([^>]*\)>.*/\1/
s/ $//g'`
    fi
  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=false;;
    */ice*)   project=ICE; library=false;;
    */nora*)  project=NORA;;
    *)        project=FOOBAR;;
  esac

  # Take the project from the old header, if applicable
  part=`grep 'This file is part of' $file`
  case $part in
    *DDD*Library*) project=DDD; library=true;;
    *DDD*)         project=DDD; library=false;;
    *ICE*Library*) project=ICE; library=true;;
    *ICE*)         project=ICE; library=false;;
  esac

  copyright=`grep -i 'copyright' $file | head -1 | sed 's/^[^0-9]*\([1-9][-, 0-9]*[0-9]\)[^0-9]*$/\1/'`
  case $copyright in
    19*) ;;
    20*) ;;
    *) copyright=$year;;
  esac

  owner=`grep -i 'copyright' $file | head -1`
  if [ "$owner" != "" ]; then
    tubs=`echo "$owner" | grep Braunschweig`
    if [ "$tubs" = "" ]; then
      # Other owner
      echo "$this: $file: other owner" >&2
      continue
    fi
  else
    if $incremental; then
      # No copyright yet
      echo "$this: $file: no copyright entry" >&2
      continue
    fi
  fi

  case $project in
    NORA)     project_detail='NORA is an experimental inference-based software development\
environment. Contact nora@'"$domain"' 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 <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.gnu.org/software/ddd/'"'"',\
or send a mail to the DDD developers <ddd@gnu.org>.';;
    *)        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) '$copyright' Universitaet Passau, Germany.'
  if [ "$fullname2" = "" ]; then
    text="$text"'\
Written by '"$fullname1 <$mailname1>"'.'
  else
    text="$text"'\
Written by '"$fullname1 <$mailname1>"'\
and '"$fullname2 <$mailname2>"'.'
  fi
  text="$text"'\
\
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 3 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, see <http://www.gnu.org/licenses/>.\
\
'$project_detail'\
'

  c_text='/*\
'`echo "$text" | sed 's,^..,    &,'`'
*/\
'
  cpp_text=`echo "$text" | sed 's,^.,// &,'`
  am_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~
  origfile=$file.orig
  tmpfile=${TMPDIR-.}/.lic$$
  t=""
  header="`grep -i copyright $file`"
  e="^[ 	]*\$"
  if [ $? = 0 ]; then
    case "$header" in
      //*)  t="$cpp_text";;
      \#\#*) t="$am_text";;
      \#*)  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"; e="^\.\\\"";;
      \;*)  t="$lisp_text";;
    esac
  fi

  if [ "$t" = "" ]; then
    header="`head -1 $file`"
    case "$header" in
      //*)  t="$cpp_text";;
      /\**) t="$c_text"; e="^*/\$";;
      \#\#*) t="$am_text";;
      \#*)  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"; e="^\.\\\"";;
      \;*)  t="$lisp_text";;
      *)    echo "$this: $file: unknown file type" >&2;;
    esac
  fi

  # echo "$t"; exit 0
  
  if [ "$t" = "" ]; then
    continue
  fi

  # Remove old license, if present
  rm -f $bakfile
  ln $file $bakfile

  copyright_lineno="`grep -n 'Copyright' $file | head -1 | cut -d: -f1`"
      
  grep 'General Public License' $file > /dev/null
  if [ $? = 0 ]; then
    if [ "$copyright_lineno" != "" ]; then
      lineno=$copyright_lineno
    else
      lineno="`grep -n '^$' $file | head -1 | cut -d: -f1`"
    fi
    if [ "$lineno" = "" ]; then
      command=2,24d
    else
      command="${lineno}",'\%'"${e}"'%'d
    fi
    $sed "$command" $file > $tmpfile && mv $tmpfile $file
  fi

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

  # Show differences - if none, remove backup file
  if diff -c5 $bakfile $file > $tmpfile; then
    # no differences
    mv $bakfile $file
    rm $tmpfile
  else
    # report diffs to stdout
    echo $file:
    cat $tmpfile
    rm $tmpfile
    mv $bakfile $origfile
  fi
done
