#!/bin/sh
# avoid silly sorting
export LANG=C
arg=$1
target=~/public_html/source/mdadm
if [ " $arg" = " test" ]
then
  target=/tmp/mdadm-test
  rm -rf $target
  mkdir -p $target
fi
if [ -d $target ]
then :
else echo $target is not a directory
     exit 2
fi
set `grep '^#define VERSION' ReadMe.c `
version=`echo $3 | sed -e 's/"//g'`
grep "^.TH MDADM 8 .. v$version" mdadm.8.in > /dev/null 2>&1 ||
 {
   echo mdadm.8.in does not mention version $version.
   exit 1
 }
grep "^.TH MDMON 8 .. v$version" mdmon.8 > /dev/null 2>&1 ||
 {
   echo mdmon.8 does not mention version $version.
   exit 1
 }
rpmv=`echo $version | tr - _`
grep "^Version: *$rpmv$" mdadm.spec > /dev/null 2>&1 ||
 {
   echo mdadm.spec does not mention version $version.
   exit 1
 }
if [ -f ANNOUNCE-$version ]
then :
else
   echo ANNOUNCE-$version does not exist
   exit 1
fi
if grep "^ANNOUNCE-$version\$" inventory
then :
else { cat inventory ; echo ANNOUNCE-$version ; } | sort -o inventory
fi

echo version = $version
base=mdadm-$rpmv.tar.gz
if [ " $arg" != " diff" ]
then
  if [ -f $target/$base ]
  then
    echo $target/$base exists.
    exit 1
  fi
  trap "rm $target/$base; exit" 1 2 3
  git archive --prefix=mdadm-$rpmv/ HEAD | gzip --best > $target/$base
  chmod a+r $target/$base
  ls -l $target/$base
  if tar tzf $target/$base | sed 's,[^/]*/,,' | sort | diff -u inventory -
  then : correct files found
  else echo "Extra files, or inventory is out-of-date"
       rm $target/$base
       exit 1
  fi
  rpmbuild -ta $target/$base || exit 1
  find ~/rpmbuild/RPMS -name "*mdadm-$version-*" \
     -exec cp {} $target/RPM \;
  cp ANNOUNCE-$version $target/ANNOUNCE
  cp ChangeLog $target/ChangeLog
  if [ " $arg" != " test" ]
  then
    echo -n "Confirm signing this release? "
    read a
    if [ " $a" != " y" ]; then echo OK - bye. ; exit 1; fi
    if zcat $target/$base | gpg -ba > $target/$base.sign && gpg -ba $target/ANNOUNCE
    then
      kup put $target/$base  $target/$base.sign \
	  /pub/linux/utils/raid/mdadm/mdadm-$version.tar.gz
      kup put $target/ANNOUNCE $target/ANNOUNCE.asc /pub/linux/utils/raid/mdadm/ANNOUNCE
    else
      echo signing failed
      exit 1
    fi
  fi
else
  if [ ! -f $target/$base ]
  then
    echo $target/$base does not exist.
    exit 1
  fi
  ( cd .. ; ln -s mdadm.v2 mdadm-$version ; tar chf - --exclude=.git --exclude="TAGS" --exclude='*,v' --exclude='*~' --exclude='*.o' --exclude mdadm --exclude=mdadm'.[^ch0-9]' --exclude=RCS mdadm-$version ; rm mdadm-$version ) | gzip --best > /var/tmp/mdadm-new.tgz
  mkdir /var/tmp/mdadm-old ; zcat $target/$base | ( cd /var/tmp/mdadm-old ; tar xf - )
  mkdir /var/tmp/mdadm-new ; zcat /var/tmp/mdadm-new.tgz | ( cd /var/tmp/mdadm-new ; tar xf - )
  diff -ru /var/tmp/mdadm-old /var/tmp/mdadm-new
  rm -rf /var/tmp/mdadm-old /var/tmp/mdadm-new /var/tmp/mdadm-new.tgz
fi
