#! /bin/sh
#
# make-patch -- Copyright (c) 2002 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx 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, or (at your option)
# any later version.
#
# epocemx 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 epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

die()
{
  echo "*** $@"
  exit 2
}

usage()
{
  echo 'Usage: maint/make-patch [-install [-yes]] [-nodiff] MODULE...'
  exit 1
}

do_install()
{
  if [ -n "$install" ]
  then
    cp -p $1 $2 || die "cp $1 $2: failed"
  fi
}

test -n "$PAGER" || PAGER=more

test -x build || die "Must run in epocemx directory"
test -f maint/exclude-bu || die "Must run in epocemx directory"

install=
nodiff=
yes=
while [ $# != 0 ]
do
  case "$1" in
    -install) install=x;;
    -nodiff)  nodiff=x;;
    -yes)     yes=x;;
    -*)       usage;;
    *)        break;;
  esac
  shift
done

test -n "$yes" && test -z "$install" && usage
test $# -gt 0 || usage

test -d orig || mkdir orig || die "mkdir failed"

./build -modules "$@" >/dev/null || exit 2
./build -modules "$@" | while read mod bn
do
  echo "Processing module $mod ($bn)"
  old=patches/$bn.patch
  new=maint/$mod.patch
  if [ -n "$nodiff" ]
  then
    test $new || die "$new: not found"
  else
    test -f maint/exclude-$mod || die "maint/exclude-$mod: not found"
    test -d local/$bn || die "local/$bn: does not exist"
    if [ ! -d orig/$bn ]
    then
      if [ -f SOURCE/$bn.tar.gz ]
      then
        echo "Unpacking $bn.tar.gz"
        gunzip <SOURCE/$bn.tar.gz | (cd orig;tar xf -) || die "tar failed"
      elif [ -f SOURCE/$bn.tar.bz2 ]
      then
        echo "Unpacking $bn.tar.bz2"
        bunzip2 <SOURCE/$bn.tar.bz2 | (cd orig;tar xf -) || die "tar failed"
      else
        die "SOURCE/$bn.tar.gz: not found"
      fi
    fi
    diff -rcN --exclude-from=maint/exclude-$mod orig/$bn local/$bn >$new
    test $? -gt 1 && die "diff failed"
  fi

  test -f $new || die "$new: not found"
  rm -f $new.diff
  if [ -f $old ]
  then
    if cmp -s $old $new
    then
      true
    else
      echo "$old: changed"
      diff $old $new >$new.diff
      if [ -n "$install" ]
      then
	if [ -n "$yes" ]
	then
	  do_install $new $old
	else
          (echo "----------------------------------------------------------------------------"
	  echo "Output of \"diff $old $new\":"
           echo "----------------------------------------------------------------------------"
           cat $new.diff) | $PAGER
	  while true
	  do
	    printf "Install %s as %s [y/n]? " $new $old
	    read r </dev/tty
	    if [ "$r" = "n" ]
	    then
	      break
	    elif [ "$r" = "y" ]
	    then
	      do_install $new $old
	      break;
	    fi
	  done
	fi
      fi
    fi
  else
    do_install $new $old
  fi
done
