#! /bin/sh

set -e

fullbuilddir=`pwd`
if test "x$srcdir" = x ; then
  fullsrcdir=`pwd`
else
  fullsrcdir=`cd $srcdir && pwd`

  # XXX: we should maybe try to copy a file to really ensure that
  #      they are not the same directory.
  test "$fullsrcdir" = "$fullbuilddir" || {
    rm -rf "$fullbuilddir/examples/tiny" 
    # copy the ``tiny'' directory.
    cp -r "$fullsrcdir/examples/tiny" "$fullbuilddir/examples/tiny" || exit 1
  }
fi

cd examples
$fullsrcdir/examples/build-examples
debian_arch=`cat debian-arch`

#=====================================================================
# Test built packages.
#=====================================================================
tocdeb="example-output/toc-deb"
tocrpm="example-output/toc-rpm"
debscripts="example-output/debcontrolscripts"
rm -rf "$debscripts"

compute_tocs () {
  package="$1"
  version="$2"
  release=1

  debname="example-output/${package}_${version}-${release}_${debian_arch}.deb"
  rpmname="example-output/${package}-${version}-${release}.${debian_arch}.rpm"
  dpkg-deb -c "$debname" |
    sed -e 's/....................................................//' \
      > $tocdeb || exit 1
  rpm -qpl "$rpmname" > $tocrpm || exit 1
  dpkg-deb -e "$debname" "$debscripts" || exit 1
  failed=0
}

assert_deb_control_script () {
  test -x "$debscripts/$1" || {
    echo "debian control script $1 was missing from $debname." 1>&2
    failed=1
  }
}
assert_not_deb_control_script () {
  test -x "$debscripts/$1" && {
    echo "debian control script $1 was spuriously found in $debname." 1>&2
    failed=1
  }
}
assert_deb_file () {
  grep -E "^$1\$" $tocdeb > /dev/null || {
    echo "$1 was not found in $debname" 1>&2
    failed=1
  }
}
assert_rpm_file () {
  grep -E "^$1\$" $tocrpm > /dev/null || {
    echo "$1 was not found in $rpmname" 1>&2
    failed=1
  }
}
assert_file () {
  assert_deb_file "$1"
  assert_rpm_file "$1"
}
assert_deb_not_file () {
  grep -E "^$1\$" $tocdeb > /dev/null && {
    echo "$1 was found in $debname" 1>&2
    failed=1
  }
}
assert_rpm_not_file () {
  grep -E "^$1\$" $tocrpm > /dev/null && {
    echo "$1 was found in $rpmname" 1>&2
    failed=1
  }
}
assert_not_file () {
  assert_deb_not_file "$1"
  assert_rpm_not_file "$1"
}

clean_tocs () {
  rm -f $tocdeb
  rm -f $tocrpm
  rm -rf "$debscripts"
  test $failed = 1 && {
    echo "$0: test for \`$package' failed" 1>&2
    exit 1
  }
}

# --- test aa ---
set +e
compute_tocs aa 0.0
assert_file /usr/bin/dummy-binary
assert_not_file /usr/bin/unpackaged-binary
clean_tocs

compute_tocs bb-a 0.0
assert_file /usr/bin/bb-a
assert_not_file /usr/bin/bb-b
clean_tocs

compute_tocs bb-b 0.0
assert_file /usr/bin/bb-b
assert_not_file /usr/bin/bb-a
clean_tocs

compute_tocs cc-nondebug 0.0
assert_file /usr/bin/test-nondebug
assert_not_file /usr/bin/test-debug
clean_tocs

compute_tocs cc-debug 0.0
assert_file /usr/bin/test-debug
assert_not_file /usr/bin/test-nondebug
clean_tocs

compute_tocs dd 0.0
assert_deb_file /usr/share/doc/dd/source-doc-dir.tar.gz
assert_deb_file /usr/share/doc/dd/installed-docs.tar.gz
assert_deb_file /usr/share/doc/dd/source-misc-doc.gz
assert_deb_file /usr/share/doc/dd/source-subdir-doc.gz
assert_deb_file /usr/share/doc/dd/installed-misc-doc.gz
assert_rpm_file /usr/doc/dd-0.0-1/source-doc-dir/sa
assert_rpm_file /usr/doc/dd-0.0-1/source-doc-dir/sb
assert_rpm_file /usr/doc/dd-0.0-1/source-doc-dir/sc
assert_rpm_file /usr/doc/dd-0.0-1/installed-docs/ia
assert_rpm_file /usr/doc/dd-0.0-1/installed-docs/ib
assert_rpm_file /usr/doc/dd-0.0-1/installed-docs/ic
assert_rpm_file /usr/doc/dd-0.0-1/source-misc-doc
assert_rpm_file /usr/doc/dd-0.0-1/source-subdir-doc
assert_rpm_file /usr/doc/dd-0.0-1/installed-misc-doc
assert_deb_file /usr/share/doc/dd/changelog.Debian.gz
clean_tocs

compute_tocs ee 0.0
assert_file /usr/share/ee/archdepdata
assert_not_file /usr/share/ee/archindepdata
clean_tocs

compute_tocs ee-archindep 0.0
assert_file /usr/share/ee/archindepdata
assert_not_file /usr/share/ee/archdepdata
clean_tocs

# the library package (ll) -- for testing purposes it
# comes with a non-library package (ll-data).
compute_tocs ll 0.0
assert_deb_control_script ll.prerm
assert_deb_control_script ll.postinst
rpm -qp --scripts $rpmname | grep -q ldconfig || {
  echo "scripts for $rpmname did not include ldconfig" 1>&2
  failed=1
}
clean_tocs

compute_tocs ll-data 0.0
assert_not_deb_control_script ll-data.prerm
assert_not_deb_control_script ll-data.postinst
rpm -qp --scripts $rpmname | grep -q ldconfig && {
  echo "scripts for $rpmname included ldconfig" 1>&2
  failed=1
}
clean_tocs

# verify redhat changelog integrity, should probably
# do the same for debian changelog...
rpm -qp --changelog example-packages/dd-0.0-1.$arch.rpm > example-output/cl
if fgrep 'will never be released' example-output/cl > /dev/null &&
   fgrep 'Dave Benson' example-output/cl > /dev/null &&
   fgrep 'daveb@ffem.org' example-output/cl > /dev/null ; then
  echo "$0: Changelog in RPM didn't look right (for dd)" 1>&2
  rm -f example-output/cl
  exit 1
fi
rm -f example-output/cl

# --- make sure we can still package ourself ---
cd ..
make packages
