#! /bin/sh
# $Id: install-ftp,v 1.22 1997/05/09 15:55:12 zeller Exp $
# Install DDD release on local FTP server

# Copyright (C) 1995-1997 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
# 
# This file is part of the DDD Library.
# 
# The DDD Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
# 
# The DDD Library 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 Library General Public License for more details.
# 
# You should have received a copy of the GNU Library General Public
# License along with the DDD Library -- see the file COPYING.LIB.
# If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# 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'.

# Name of the FTP server (use "" if local)
# ftp=ftp
ftp=

# Directory on FTP server
dir=/ftp/pub/local/softech/ddd

# rsh and rcp commands
if [ "$ftp" = "" ]; then
  rsh="sh -c"
  rcp="cp"
  ftpdir=$dir

  if [ ! -d $ftpdir ]; then
    echo $ftpdir: no such directory
    exit 1
  fi
else
  rsh="rsh $ftp"
  rcp="rcp"
  ftpdir="$ftp:$dir"
fi

# diff command
gdiff=/usr/local/bin/gdiff

# Introduction files
README="ANNOUNCE    \
        BUGS        \
        COPYING     \
        COPYING.LIB \
        CREDITS     \
	FAQ         \
	INSTALL     \
	NEWS        \
	PROBLEMS    \
	README      \
        README-BETA \
	TODO        \
"

# Documentation files
DOC="ddd.man.ps ddd.man-nopics.ps"

# Sample files
SAMPLES="ddd.man ddd.man.txt sample.dddinit Ddd"

# Files to be ignored in DIFF files
DIFF_IGNORE="
doc/ddd*.ps
ddd/vsl-lex.C
ddd/vsl-gramma.C
ddd/vsl-gramma.h
ddd/*.man
ddd/*.man.txt
ddd/*.huff.C
ddd/xlibdir.C
"

readme=false
dist=false
doc=false
diff=false
link=false

errors=false
if [ $# = 0 ]; then
  # Default: install all
  readme=true
  dist=true
  doc=true
  diff=true
  link=true
else
  while [ $# != 0 ]; do
    case "$1" in
    -readme) readme=true; shift;;
    -dist)   dist=true;   shift;;
    -doc)    doc=true;    shift;;
    -diff)   diff=true;   shift;;
    -link)   link=true;   shift;;
    *) echo "unknown option $1" >&2; errors=true; shift;;
    esac
  done
fi

if $errors; then
  exit 1
fi

if $readme; then
  for file in $README; do
    $rsh "cd $dir; cat $file" | $gdiff -c5 $file -
    if [ $? != 0 ]; then
      echo installing $file in $ftpdir
      $rsh "cd $dir; /bin/rm -f $README"
      $rcp -p $README $ftpdir
    fi
  done
fi

# Distribution files
DDD=ddd-*.tar.gz
DIST=`ls $DDD 2> /dev/null`

if $dist; then
  if [ "$DIST" = "" ]; then
     echo "cannot find dist file" >&2
     exit 1
  fi

  echo installing $DIST in $ftpdir/src...
  $rsh "cd $dir/src; /bin/rm -f $DIST"
  $rcp -p $DIST $ftpdir/src
fi

if $doc; then
  (
  cd ddd
  if [ ! -f Makefile ]; then
    echo configuring ddd...
    ./configure -v
  fi

  echo making $DOC...
  for doc in $DOC; do
    gmake ../doc/$doc
  done
  for sample in $SAMPLES; do
    gmake $sample
  done

  echo installing $DOC...
  $rsh "cd $dir/doc; /bin/rm -f $DOC"
  (cd ../doc; $rcp -p $DOC $ftpdir/doc)
  $rsh "cd $dir/doc; gzip -f -v -9 $DOC"

  echo installing $SAMPLES...
  $rsh "cd $dir/doc; /bin/rm -f $SAMPLES"
  $rcp -p $SAMPLES $ftpdir/doc

  (cd ../doc; rm -f $DOC)
  )
fi

if $diff; then
  if [ "$DIST" = "" ]; then
     echo "cannot find dist file" >&2
     exit 1
  fi
  echo installing diffs...
  for dist in $DIST; do
    case $dist in
      *-pics*) continue;;
    esac
    dist_version=`echo $dist | sed 's%.*-\(.*\)\.tar.*%\1%'`
    dist_release=ddd-$dist_version
    echo unpacking $dist...
    rm -fr $dist_release
    gtar zxf $dist
    for file in `$rsh "cd $dir/src; ls -t $DDD | grep -v pics | head -2"`; do
      if [ "$file" = "$dist" ]; then
	continue
      fi
      file_version=`echo $file | sed 's%.*-\(.*\)\.tar.*%\1%'`
      file_release=ddd-$file_version
      diff=$file_release-$dist_version.diff

      rm -f $diff
      rm -fr $file
      echo getting $file...
      $rcp -p $ftpdir/src/$file .
      rm -fr $file_release
      echo unpacking $file_release...
      gtar zxf $file

      echo writing $diff header...
      (
      echo "This patch will bring a $file_release release up to $dist_release."
      echo
      echo "To apply this patch, do the following:";
      echo
      echo "1. Change to the directory containing the $file_release"
      echo "   top-level directory, such that \`./$file_release/ANNOUNCE'"
      echo "   contains the $file_release announement."
      echo
      echo
      echo "2. Create a symbolic link from \`$file_release' to \`$dist_release'":
      echo
      echo "   % ln -s $file_release $dist_release"
      echo
      echo
      echo "3. Apply this patch on $file_release, using \`patch':"
      echo
      echo "   % patch < $diff"
      echo
      echo
      echo "4. Check for any rejected patches (in \`.rej' files):"
      echo
      echo "   % find $file_release -name '*.rej' -print"
      echo
      echo "   Rejected patches must be resolved manually; some files"
      echo "   can also be reconstructed using \`make'."
      echo
      echo
      echo "5. Consider removing unnecessary \`.orig' files, using \`find:'"
      echo
      echo "   % find $file_release -name '*.orig' -exec rm -f {} \;"
      echo
      echo
      echo "6. Consider removing files no more present in $dist_release."
      echo "   You can find these files by looking for the text"
      echo "   \`Only in $file_release:', below."
      echo
      echo
      echo "7. Rename the \`$file_release' directory to \`$dist_release':"
      echo
      echo "   % mv $file_release $dist_release"
      echo
      echo
      echo "That's all.  $dist_release should now be identical to the distributed version"
      echo "-- besides any changes you made, of course."
      echo
      echo "Note that changes in the following files are NOT included in this patch,"
      echo "since these files can easily be reconstructed using \`make':"
      echo "$DIFF_IGNORE" | sed 's/^/    &/'
      echo
      echo "Please report any bugs to ddd-bugs@ips.cs.tu-bs.de."
      echo
      # echo "Prereq: $file_release"
      # echo
      ) > $diff

      (cd $file_release; rm -f $DIFF_IGNORE)
      (cd $dist_release; tar cf - $DIFF_IGNORE) | 
      (cd $file_release; tar xfBp -)

      echo finding $diff...
      $gdiff -c2prP $file_release $dist_release | grep -v '^\\' >> $diff

      echo packing $diff...
      rm -f $diff.gz
      gzip -9 -c $diff > $diff.gz
      chmod -w $diff $diff.gz

      echo installing $diff...
      $rsh "cd $dir/diffs; /bin/rm -f $diff.gz"
      $rcp -p $diff.gz $ftpdir/diffs

      echo removing $file_release...
      rm -fr $file $file_release

    done
    echo removing $dist_release...
    rm -fr $dist_release
  done
fi

if $link; then
  if [ "$DIST" = "" ]; then
     echo "cannot find dist file" >&2
     exit 1
  fi
  for dist in $DIST; do
    case $dist in
      *-pics*) continue;;
    esac
    dist_base=`echo $dist | sed 's%.*-\(.*\)[a-z]*\.tar.*%\1%'`
    link=ddd-$dist_base.tar.gz
    echo installing link from $link to src/$dist...
    $rsh "cd $dir; /bin/rm -f $link; ln -s src/$dist $link"
  done
fi

# That's all, folks!
exit 0
