#!/bin/sh

# tests the libexample-clojure package to ensure it's properly structured and
# the maven-repo artifacts are correct, including pom dependency versions and
# debian.* properties

set -efux

TEMPDIR=$(mktemp -d)

dpkg-deb -c libexample-clojure_0.1.0_all.deb

# check Depends:
dpkg-deb --field libexample-clojure_0.1.0_all.deb Depends | grep -Fq libclojure-java
dpkg-deb --field libexample-clojure_0.1.0_all.deb Depends | grep -Fq libtruss-clojure

# extract package contents to examine maven-repo poms
dpkg-deb --extract libexample-clojure_0.1.0_all.deb "$TEMPDIR"

# check for the (non-)existence of jar and pom artifacts
test -L "${TEMPDIR}/usr/share/java/example.jar"
test -s "${TEMPDIR}/usr/share/java/example-0.1.0.jar"
test -L "${TEMPDIR}/usr/share/java/example-test.jar"
test -e "${TEMPDIR}/usr/share/java/example-debian.jar" && exit 1
test -s "${TEMPDIR}/usr/share/java/example-test-0.1.0.jar"
test -e "${TEMPDIR}/usr/share/java/example-test-debian.jar" && exit 1
test -L "${TEMPDIR}/usr/share/maven-repo/com/example/example/0.1.0/example-0.1.0.jar"
test -e "${TEMPDIR}/usr/share/maven-repo/com/example/example/0.1.0/example-0.1.0.jar"
test -s "${TEMPDIR}/usr/share/maven-repo/com/example/example/0.1.0/example-0.1.0.pom"
test -L "${TEMPDIR}/usr/share/maven-repo/com/example/example/0.1.0/example-0.1.0-test.jar"
test -e "${TEMPDIR}/usr/share/maven-repo/com/example/example/0.1.0/example-0.1.0-test.jar"
test -L "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.jar"
test -e "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.jar"
test -s "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom"
test -L "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian-test.jar"
test -e "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian-test.jar"

# ensure pom was cleaned
grep -Fq '<repositories>' "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" && exit 1

# verify debian.* properties
xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t \
    -m "/x:project/x:properties/x:debian.hasPackageVersion" -v . -n \
    "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" > /dev/null
xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t \
    -v "/x:project/x:properties/x:debian.packageName" \
    "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" > /dev/null
xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t \
    -v "/x:project/x:properties/x:debian.originalVersion" \
    "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" > /dev/null

# ensure dependency artifact versions are correct
xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t \
    -m "/x:project/x:dependencies/x:dependency[x:artifactId='clojure' and x:version='1.x']" -v "true()" -n \
    "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" > /dev/null
xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t \
    -m "/x:project/x:dependencies/x:dependency[x:artifactId='truss' and x:version='debian']" -v "true()" -n \
    "${TEMPDIR}/usr/share/maven-repo/com/example/example/debian/example-debian.pom" > /dev/null
