#!/bin/sh

# Copyright (C) 2013, Rafael Laboissiere <rafael@laboissiere.net>
#
# 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 3 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.
#
# On Debian systems, the complete text of the GNU General Public License
# version 3 can be found in the /usr/share/common-licenses/GPL-3 file.

if test "$1" = --installed; then
    COMMAND="uscan --no-conf"
    shift
else
    top_srcdir=$(readlink -f "${0%/*}/..")
    COMMAND="perl -I $top_srcdir $top_srcdir/scripts/uscan.pl --no-conf"
fi

cleanup(){
    kill -9 $(cat $TMPDIR/repo/pid)
    rm -rf $TMPDIR

}

trap cleanup 1 2 3 13 15

containsName(){
  expr "$1" : ".*/$2" > /dev/null
  echo $?
}

. "${0%/*}/shunit2-helper-functions.sh"

# The following function tests the Files-Excluded feature of uscan, which
# allows the selective exclusion of files from the upstream tarball before
# repacking it.
#
# This function does the following: (1) create a minimal Debian package
# directory, containing minimal files debian/{changelog,watch,copyright},
# (2) create a minimal repository, containing a tarball (built on the fly),
# (3) start an HTTP server that works offline, using the SimpleHTTPServer
# module of Python, and (4) run uscan inside that minimal universe.

testFileExclusion() {

    PKG=foo
    PORT=8000
    TMPDIR=$(mktemp -d)

    mkdir -p $TMPDIR/$PKG/debian

    cat <<END > $TMPDIR/$PKG/debian/watch
version=3
http://localhost:$PORT/$PKG-(\d).tar.gz
END

    cat <<END > $TMPDIR/$PKG/debian/changelog
$PKG (0-1) unstable; urgency=low

  * Initial release

 -- Joe Developer <jd@debian.org>  Mon, 02 Nov 2013 22:21:31 -0100
END

    cat <<END > $TMPDIR/$PKG/debian/copyright
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Files-Excluded: exclude-this
 exclude-dir
END

    # Test matching with escaped characters
    printf ' ;\ echo\ baz;\ #\n' >> $TMPDIR/$PKG/debian/copyright

    mkdir -p $TMPDIR/repo
    touch $TMPDIR/repo/include-this
    touch $TMPDIR/repo/exclude-this
    mkdir -p "$TMPDIR/repo/; echo baz; #/"
    mkdir -p $TMPDIR/repo/exclude-dir
    touch $TMPDIR/repo/exclude-dir/file

    ( cd $TMPDIR/repo ;
      tar cfz $PKG-1.tar.gz * ;
      python -m SimpleHTTPServer $PORT &
      echo $! > pid )

    OUTPUT=$( (cd $TMPDIR/$PKG ; $COMMAND) | grep baz)

    TARBALL=${PKG}_1+dfsg.orig.tar.gz
    assertTrue 'pristine tarball is not created' "[ -f $TMPDIR/$TARBALL ]"
    CONTENTS="$(tar atf $TMPDIR/$TARBALL)"
    assertFalse 'file that must be excluded is present in the tarball'	\
                $(containsName "$CONTENTS" exclude-this)
    assertFalse "dir that must be excluded is present in the tarball"	\
                $(containsName "$CONTENTS" exclude-dir)
    assertFalse "path with whitespace that must be excluded is present"	\
                $(containsName "$CONTENTS" "; echo baz; #/")
    # 731849
    assertNull 'dirty root directory allowed command execution' "$OUTPUT"

    cleanup

}

. shunit2
