#!/bin/bash
#
# Regina - A Normal Surface Theory Calculator
# Source Distribution Verification
#
# Copyright (c) 2003-2007, Ben Burton
# For further details contact Ben Burton (bab@debian.org).
#
# Usage: distcheck <dist-tarball>
#
# Verifies that a tarball formed using "make dist" contains all the
# files it should.  This script outputs a list of files contained in
# the subversion source tree that are missing from the tarball.
#
# Files that are not necessary for inclusion in the tarball (such as
# auto-generated files or the Regina website) are not included in this
# output.
#
# This script must be run from either the top-level source directory within
# the subversion source tree, or from the admin/ directory beneath it.
#
# Requires: diff, find, grep, mktemp, sed, sort, tar
#
# This program 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.
#
# This program 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 this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
set -e

# Command-line sanity check.
if [ "$#" != 1 ]; then
  echo "Usage: distcheck <dist-tarball>"
  exit 1
fi

# Locate the top of the subversion source tree.
if [ ! -d .svn ]; then
  echo "This script must be run from within the subversion source tree."
  exit 1
fi

if [ -d admin -a -f HIGHLIGHTS.txt ]; then
  svntree=.
elif [ -d ../admin -a -f ../HIGHLIGHTS.txt ]; then
  svntree=..
else
  echo "This script must be run from either the top-level source directory"
  echo "within the subversion source tree, or from the admin/ directory"
  echo "beneath it."
  exit 1
fi

# Prepare the two temporary file lists.
svnlist="`mktemp -t svnlist.XXXXXXXXXX`" || svnlist=
distlist="`mktemp -t distlist.XXXXXXXXXX`" || distlist=
if [ -z "$svnlist" -o -z "$distlist" ]; then
  echo "Error creating temporary files."
  exit 1
fi

# Make a list of files in the distribution tarball.
echo "Analysing distribution tarball..."

if [ ! -f "$1" ]; then
  echo "The distribution tarball $1 does not exist or is not a regular file."
  exit 1
fi

case "$1" in
  *.tar.gz )
    taropts=-ztf
    ;;
  *.tgz )
    taropts=-ztf
    ;;
  *.tar.bz2 )
    taropts=-jtf
    ;;
  *.tar )
    taropts=-tf
    ;;
  * )
    echo "The distribution tarball $1 is not of the correct type."
    exit 1
    ;;
esac

if ! tar "$taropts" "$1" | sed -e 's%^[^/]*/%%' -e 's%/$%%' | \
    sort > "$distlist"; then
  echo "The contents of the distribution tarball $1 could not be listed."
  exit 1
fi

# Make a list of files in the subversion source tree.
echo "Analysing subversion source tree..."

if ! find "$svntree" | sed -e 's%^[^/]*/%%' | \
    grep -v '^\.$' | \
    grep -v '^\.\.$' | \
    grep -v '\(^\|/\)\.svn$' | \
    grep -v '\.svn/' | \
    grep -v 'Makefile$' | \
    grep -v '\.a$' | \
    grep -v '\.DS_Store$' | \
    grep -v '\.deps\(/\|$\)' | \
    grep -v '\.kidl$' | \
    grep -v '\.la$' | \
    grep -v '\.la\.closure$' | \
    grep -v '\.libs\(/\|$\)' | \
    grep -v '\.lo$' | \
    grep -v '\.moc$' | \
    grep -v '\.o$' | \
    grep -v '^autom4te.cache\(/\|$\)' | \
    grep -v '^config.log$' | \
    grep -v '^config.status$' | \
    grep -v '^docs/engine/.*\.css$' | \
    grep -v '^docs/engine/.*\.gif$' | \
    grep -v '^docs/engine/.*\.html$' | \
    grep -v '^docs/engine/.*\.png$' | \
    grep -v '^docs/engine/doxygen' | \
    grep -v '^docs/engine/graph_legend' | \
    grep -v '^docs/man/manpage\.' | \
    grep -v '^engine/regina-config\.h$' | \
    grep -v '^engine/regina-engine-config$' | \
    grep -v '^engine/snappea/kernel/unused\(/\|$\)' | \
    grep -v '^engine/stamp-h' | \
    grep -v '^hold\(/\|$\)' | \
    grep -v '^icons/src\(/\|$\)' | \
    grep -v '^kdeui/.*/index.cache.bz2$' | \
    grep -v '^kdeui/src/shell/regina-kde$' | \
    grep -v '^libtool$' | \
    grep -v '^metrics\(/\|$\)' | \
    grep -v '^packaging\(/\|$\)' | \
    grep -v '^python/regina-python$' | \
    grep -v '^redist\(/\|$\)' | \
    grep -v '^regina-[0-9.]\+\.tar\.bz2$' | \
    grep -v '^regina-[0-9.]\+\.tar\.gz$' | \
    grep -v '^regina-[0-9.]\+\.zip$' | \
    grep -v '^sfdist\(/\|$\)' | \
    grep -v '^stuff\(/\|$\)' | \
    grep -v '^test\(/\|$\)' | \
    grep -v '^testsuite/regtestsuite$' | \
    grep -v '^utils/reg[a-z]\+$' | \
    grep -v '^utils/[a-z]\+census[a-z]*$' | \
    grep -v '^utils/trisetcmp$' | \
    grep -v '^utils/local\(/\|$\)' | \
    grep -v '^utils/mpi/[a-z\-]\+-mpi$' | \
    grep -v '^utils/snappea\(/\|$\)' | \
    grep -v '^www\(/\|$\)' | \
    sort > "$svnlist" ; then
  echo "The contents of the subversion source tree could not be listed."
  exit 1
fi

# Output the differences.
diff -B "$svnlist" "$distlist" | grep -v '^[0-9]'

# Clean up.
rm "$svnlist" "$distlist"
