# -*- mode: sh; coding: utf-8 -*-
# Copyright © 2003 Jeff Bailey <jbailey@debian.org>
#
# 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, 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., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.

if test "$(basename $(pwd))" != 'test'; then
  echo "error: test must be run in test subdir"
  exit 1
fi

export NO_PKG_MANGLE=1

_cdbs_test_basedir=`pwd`/workdir
WORKDIR=$_cdbs_test_basedir/cdbs-testsuite-0.1
SRCDIR=$(realpath $(dirname $0)/../)

eval `dpkg-architecture -s`

setup_workdir () {
  clean_workdir
  mkdir -p $WORKDIR/debian
  cp -R debian/* $WORKDIR/debian
}

clean_workdir () {
  if [ ! $_cdbs_noclean ]; then
    rm -rf $_cdbs_test_basedir
  fi
}

return_fail () {
  if [ ! $_cdbs_quiet ]; then
    echo "This test has failed."
  fi
  exit 1
}

return_ignore () {
  if [ ! $_cdbs_quiet ]; then
    echo "This test has been skipped."
  fi
  exit 77
}

return_pass () {
  if [ ! $_cdbs_quiet ]; then
    echo "This test has passed."
  fi
  exit 0
}

build_package () {
  pushd $WORKDIR >/dev/null
  build_package_1
  popd >/dev/null
}

clean_package () {
  pushd $WORKDIR >/dev/null
  clean_package_1
  popd >/dev/null
}

get_package_contents () {
  tempd=$(mktemp -d)
  dpkg -X "$1" "$tempd"
  rm -rf "$tempd"
}

build_package_1 () {
  eval "dpkg-buildpackage -rfakeroot -us -uc $_cdbs_silent"
  _cdbs_retval=$?
  if [ $_cdbs_retval != 0 ]; then
    return_fail
  fi
}  

clean_package_1 () {
  eval "fakeroot ./debian/rules clean $_cdbs_silent"
  _cdbs_retval=$?
  if [ $_cdbs_retval != 0 ]; then
    return_fail
  fi
}  

test_tarballs () {
  eval "make tarballs $_cdbs_silent"
}

options () {
  progname="`basename \"$0\"`"

while [ $# != 0 ]; do
        case "$1" in
	--noclean)	_cdbs_noclean=yes;;
        --quiet)        _cdbs_quiet=yes;;
        --help)         usage; exit 0;;
        *) echo >&2 "$progname: unknown option or argument $1"; usage; exit 2;;
        esac
        shift
done

# Handle the TESTS_SILENT case
if [ $TESTS_SILENT ]; then
  _cdbs_quiet="yes"
fi

if [ $_cdbs_quiet ]; then
  _cdbs_silent=">/dev/null 2>&1"
fi

}

usage () {
    echo "usage:"
    echo " --noclean            Do not delete temporary files"
    echo " --quiet              Silence all output"
    echo " --help               Display this message"
}
                                                                                
