#!/bin/bash

test -e $BUILD_ROOT/skipped-install-cross && {
    echo 'skipping pre/post install checks, install was skipped'
    exit 0
}

echo "... testing for pre/postinstall scripts that are not idempotent"
TOPDIR=/usr/src/packages
test -d $BUILD_ROOT/.build.packages && TOPDIR=/.build.packages

SCRIPT_ARG=-e

RPM="chroot $BUILD_ROOT rpm --nodigest --nosignature"

# rpm needs /proc mounted - the build script handles that. just verify it did
test -d $BUILD_ROOT/proc/sys || { echo "proc is not mounted"; exit 1; }

export YAST_IS_RUNNING="instsys"
for i in $(find $BUILD_ROOT$TOPDIR/RPMS -type f -name "*.rpm" | LC_ALL=C sort) ; do
    set -- $($RPM --macros=/dev/null -qp --qf '%{PREINPROG} %{POSTINPROG} %{PREUNPROG} %{POSTUNPROG}' ${i#$BUILD_ROOT})
    pre_p="$1"
    post_p="$2"
    preun_p="$3"
    postun_p="$4"
    test "$pre_p" = "(none)" -a "$post_p" = "(none)" -a "$preun_p" = "(none)" -a "$postun_p" = "(none)" && continue

    $RPM -Vp --nodeps ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck1
    if test "${pre_p: -2}" = "sh" ; then
        $RPM --macros=/dev/null -qp --qf '%{PREIN}' ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck_scr
	if test "$(<$BUILD_ROOT/.build_patchrpmcheck_scr)" != "(none)" ; then
            if !  chroot $BUILD_ROOT ${pre_p} $SCRIPT_ARG /.build_patchrpmcheck_scr 2 ; then
                echo "preinstall script of ${i##*/} failed"
                touch $BUILD_ROOT/not-ready
                exit 1
            fi
        fi
    fi
    if test "${post_p: -2}" = "sh" ; then
        $RPM --macros=/dev/null -qp --qf '%{POSTIN}' ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck_scr
	if test "$(<$BUILD_ROOT/.build_patchrpmcheck_scr)" != "(none)" ; then
            if ! chroot $BUILD_ROOT ${post_p} $SCRIPT_ARG /.build_patchrpmcheck_scr 2 ; then
                echo "postinstall script of ${i##*/} failed"
                touch $BUILD_ROOT/not-ready
                exit 1
            fi
        fi
    fi
    if test "${preun_p: -2}" = "sh" ; then
        $RPM --macros=/dev/null -qp --qf '%{PREUN}' ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck_scr
	if test "$(<$BUILD_ROOT/.build_patchrpmcheck_scr)" != "(none)" ; then
            if ! chroot $BUILD_ROOT ${preun_p} $SCRIPT_ARG /.build_patchrpmcheck_scr 1 ; then
                echo "preuninstall script of ${i##*/} failed"
                touch $BUILD_ROOT/not-ready
                exit 1
            fi
        fi
    fi
    if test "${postun_p: -2}" = "sh" ; then
        $RPM --macros=/dev/null -qp --qf '%{POSTUN}' ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck_scr
	if test "$(<$BUILD_ROOT/.build_patchrpmcheck_scr)" != "(none)" ; then
            if ! chroot $BUILD_ROOT ${postun_p} $SCRIPT_ARG /.build_patchrpmcheck_scr 1 ; then
                echo "postuninstall script of ${i##*/} failed"
                touch $BUILD_ROOT/not-ready
                exit 1
            fi
        fi
    fi
    $RPM -Vp --nodeps ${i#$BUILD_ROOT} > $BUILD_ROOT/.build_patchrpmcheck2
    if ! cmp -s $BUILD_ROOT/.build_patchrpmcheck1 $BUILD_ROOT/.build_patchrpmcheck2; then
	echo "pre/postinstall/uninstall script of ${i##*/} modifies filelist!"
	echo "filelist diff:"
	diff -u0 $BUILD_ROOT/.build_patchrpmcheck1 $BUILD_ROOT/.build_patchrpmcheck2
	touch $BUILD_ROOT/not-ready
	exit 1
    fi
    rm -f $BUILD_ROOT/.build_patchrpmcheck1 $BUILD_ROOT/.build_patchrpmcheck2 $BUILD_ROOT/.build_patchrpmcheck_scr
done

exit 0
