#!/bin/sh

# testinst_pdeb  version 0.15
# a hook script to test package installation/upgrade/downgrade/removal/...
# (from a pdebuild environment)
#
# Copyright (c) 2009-2019 Francesco Poli
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


echo " ===== starting ${0} ====="

COMMON="$( dirname ${0} )/pdeb_common"
. $COMMON

set -e

init_aptitude_log APTITUDE_TLOG
$APTITUDE_TLOG install awk apt-utils

echo " => setting up local temporary repository"
create_local_repo LOC_REPO LOC_SOURCES LOC_PREFS

PKGS=$( awk '/^Package: / { print $2 ; }' ${HOME}/*/debian/control ) || true
APTCACHE="/var/cache/apt/archives"

for PKG in $PKGS
do
    # skip bootloaders, since they cannot be easily installed inside a
    # pdebuild chroot environment
    if echo $PKG | grep '^grub.*$\|^lilo.*$' > /dev/null
    then
        echo " => skipping tests for bootloader package $PKG"
        continue
    fi

    # special cases, treated as if they were essential packages
    ESS=no
    if echo $PKG | grep '^apt$\|^libapt-pkg.*$\|^aptitude$' > /dev/null
    then
        ESS=yes
    fi

    echo " => installing official version of $PKG"
    $APTITUDE_TLOG -t $DIST install $PKG

    echo " => upgrading to locally rebuilt version of $PKG"
    init_aptitude_log APTITUDE_TLOG_UPGR
    $APTITUDE_TLOG_UPGR -t unreleased install $PKG

    echo " => downgrading to official version of $PKG"
    downgrade_upgraded_pkgs APTITUDE_TLOG_UPGR "$APTITUDE_TLOG"

    if test $ESS = yes || dpkg -s $PKG | grep 'Essential: yes' > /dev/null
    then
        echo " => package $PKG is essential: skipping purge test"
    else
        echo " => purging package $PKG"
        $APTITUDE_TLOG --purge-unused purge $PKG
    fi

    echo " => installing locally rebuilt version of $PKG"
    $APTITUDE_TLOG -t unreleased install $PKG

    if test $ESS = yes || dpkg -s $PKG | grep 'Essential: yes' > /dev/null
    then
        echo " => package $PKG is essential: skipping remove test"
    else
        echo " => removing package $PKG"
        $APTITUDE_TLOG remove $PKG
    fi

    echo " => installing locally rebuilt version of $PKG"
    $APTITUDE_TLOG -t unreleased install $PKG

    if test $ESS = yes || dpkg -s $PKG | grep 'Essential: yes' > /dev/null
    then
        echo " => package $PKG is essential: skipping purge test"
        echo " => downgrading to official version instead"
        downgrade_pkgs_again APTITUDE_TLOG
    else
        echo " => purging package $PKG"
        $APTITUDE_TLOG --purge-unused purge $PKG
    fi

    N_IN_CACHE=$( ls -1      $APTCACHE/${PKG}_*.deb 2> /dev/null | wc -l )
    N_IN_LREPO=$( ls -1 $LOC_REPO/pool/${PKG}_*.deb 2> /dev/null | wc -l )
    if test $N_IN_CACHE -eq 1 -a $N_IN_LREPO -eq 1
    then
        $APTITUDE_TLOG install devscripts wdiff
        echo " => comparing official and locally rebuilt versions of $PKG"
        debdiff --auto-ver-sort $APTCACHE/${PKG}_*.deb \
                           $LOC_REPO/pool/${PKG}_*.deb || true
    fi
done

echo " => cleaning up"
purge_installed_pkgs APTITUDE_TLOG /var/log/aptitude
destroy_local_repo LOC_REPO LOC_SOURCES LOC_PREFS

echo " ===== finished ${0} ====="
