#!/bin/sh
files_exist()
{
    [ -e "$1" ]
}
if files_exist $REPOCOP_PKG_ROOT/etc/init.d/*; then
	STATUS=warn
	MESSAGE="warning: found files in /etc/init.d/; better to use %_initdir=/etc/rc.d/init.d"
else
	STATUS=ok
	MESSAGE=
	files_exist $REPOCOP_PKG_ROOT/etc/rc.d/init.d/* || exec repocop-test-skip
fi

for i in $REPOCOP_PKG_ROOT/etc/rc.d/init.d/* $REPOCOP_PKG_ROOT/etc/init.d/*; do
    filename=${i##$REPOCOP_PKG_ROOT}
    if [ -x $i ]; then
        HAS_CONDRESTART=`grep 'condrestart' $i`
        if [ -z "$HAS_CONDRESTART" ]; then
	    if rpmquery --scripts -p $REPOCOP_PKG | grep post_service >/dev/null; then
	        STATUS=fail
            	MESSAGE="$MESSAGE$filename: missing condrestart target. ERROR: alt-specific script %_sbindir/post_service (used in your %post_service macro) depends on condrestart. Please, fix."
	    else
	    	[ "$STATUS" != "fail" ] && STATUS=warn
		MESSAGE="$MESSAGE$filename: missing condrestart target. Note: alt-specific script %_sbindir/post_service (used in %post_service macro) depends on condrestart. It is wise to add condrestart anyway."
	    fi
        fi
        HAS_CONDSTOP=`grep 'condstop' $i`
        if [ -z "$HAS_CONDSTOP" ]; then
	    if rpmquery --scripts -p $REPOCOP_PKG | grep preun_service >/dev/null; then
	        STATUS=fail
            	MESSAGE="$MESSAGE$filename: missing condstop target. ERROR: alt-specific script %_sbindir/preun_service (used in your %preun_service macro) depends on condstop. Please, fix."
	    else
	    	[ "$STATUS" != "fail" ] && STATUS=warn
		MESSAGE="$MESSAGE$filename: missing condstop target. Note: alt-specific script %_sbindir/preun_service (used in %preun_service macro) depends on condstop. It is wise to add condstop anyway."
	    fi
        fi
    fi
done
repocop-test-$STATUS $MESSAGE
