#!/bin/sh -e

help () {
    cat <<EOF
$0 [OPTION]: run piuparts against landscape-client packages built from a branch

The script checks if packaging changes (i.e. modifications to debian/ files) have
been introduced in the latest commit of the branch, and if so builds fresh
packages and run piuparts against them.

The first time it's run, this script automatically creates a pbuilder chroot
for the same Ubuntu release the system is running. The chroot will be saved
under /var/cache/pbuilder and re-used in subsequent runs.

The piuparts output will be available under build/piuparts/output.log.

Available options:

  -m, --mirror <mirror>   The Ubuntu mirror to use to build the pbuilder used
                          by piuparts chroot.
  -f, --force             Force running the tests, even if the debian/ directory
                          hasn't been changed in the last bzr commit.
EOF
}

#
# Check if X is running, if not abort because we would make X crash (D-Bus!)
#
if pidof X > /dev/null; then
    cat <<EOF
It appears you're running an X session. This script would kill your session
due to landscape-client reloading dbus in its postinst script. Quitting now.
EOF
    exit 1
fi

#
# Check we have piuparts >= 0.36
#
PIUPARTS_VERSION=$(sudo piuparts --version 2>/dev/null| cut -f 2 -d " ")
if ! dpkg --compare-versions $PIUPARTS_VERSION ge 0.36; then
    cat <<EOF
You need piuparts 0.36 or higher
EOF
    exit 1
fi

#
# Parse command line arguments
#
OPTS=$(getopt -o hm:f --long help,mirror:,force -- "$@")
if [ $? != 0 ]; then
    exit 1
fi
eval set -- "$OPTS"

MIRROR=http://archive.ubuntu.com/ubuntu
FORCE=no

while true ; do
    case "$1" in
	-h|--help) help; exit 1; shift ;;
	-m|--mirror) MIRROR=$2; shift 2 ;;
	-f|--force) FORCE=yes; shift 1 ;;
	--) shift ; break ;;
	*) echo "Internal error!" ; exit 1 ;;
    esac
done

#
# Check bzr changes under debian/
#
if [ "${FORCE}" = "no" ]; then
    if ! bzr diff -r revno:-1|diffstat -l|grep -q "^debian/.*"; then
        echo "No packaging changes, skipping piuparts tests."
        exit 0
    fi
fi

#
# Export the sources
#
TOPDIR=$(pwd)/build/piuparts
RELEASE=$(lsb_release -cs)
SOURCE=$(dpkg-parsechangelog |grep "^Source:" | cut -f 2 -d " ")
VERSION=$(dpkg-parsechangelog |grep "^Version:" | cut -f 2 -d " ")
ARCH=$(dpkg --print-architecture)
rm -rf $TOPDIR
mkdir -p $TOPDIR
bzr export $TOPDIR/$SOURCE-$VERSION

#
# Build the package
#
cd $TOPDIR/$SOURCE-$VERSION
dpkg-buildpackage -rfakeroot -b -uc -us
cd $TOPDIR

BASETGZ=/var/cache/pbuilder/${SOURCE}-${RELEASE}-${ARCH}.tgz
if ! [ -e "$BASETGZ" ]; then
    # Create the pbuilder chroot
    COMPONENTS="main universe multiverse restricted"
    OTHERMIRROR="deb ${MIRROR} ${RELEASE}-updates ${COMPONENTS}"

    sudo pbuilder \
        --create \
        --distribution ${RELEASE} \
        --mirror ${MIRROR} \
        --components "${COMPONENTS}" \
        --othermirror "${OTHERMIRROR}" \
        --basetgz ${BASETGZ}
else
    # Update the pbuilder chroot
    sudo pbuilder \
        --update \
        --basetgz ${BASETGZ}
fi

#
# Run piuparts
#

# Workaround a bug in policykit not purging correctly
mkdir -p ${TOPDIR}/scripts/
cat > ${TOPDIR}/scripts/post_purge_policy_kit <<EOF
#!/bin/sh
rm -rf /var/lib/PolicyKit/user-haldaemon.auths
EOF
chmod 755 ${TOPDIR}/scripts/post_purge_policy_kit

sudo piuparts \
    --scriptsdir=${TOPDIR}/scripts/ \
    --keep-sources-list \
    --skip-minimize \
    --no-symlinks \
    -i /var/lib/PolicyKit \
    -i /etc/ssl \
    -I "/etc/udev.*" \
    -I "/lib/udev/.*" \
    -I "/var/lib/.*" \
    -I "/usr/lib/python2.6/dist-packages/twisted.*" \
    -i /etc/X11 \
    -i /etc/dbus-1 \
    -i /etc/dbus-1/system.d \
    -I "/usr/lib/python2.4/site-packages/apt.*" \
    -I "/usr/lib/python2.4/site-packages/dbus.*" \
    -I "/usr/lib/python2.5/site-packages/twisted.*" \
    -b ${BASETGZ} \
    ${SOURCE}_${VERSION}_${ARCH}.changes
