#!/bin/sh
sqlite3 "$REPOCOP_DISTROTEST_DBDIR/rpm.db" <<EOSQL
.mode tabs
.output $REPOCOP_TEST_TMPDIR/systemd_services
SELECT filename FROM rpm_files WHERE FILENAME glob '/lib/systemd/system/*.service';
EOSQL
sed -i -e 's,^/lib/systemd/system/,,;s,\.service$,,' $REPOCOP_TEST_TMPDIR/systemd_services


for repocop_pkg_key in `ls $REPOCOP_STATEDIR/init-script`; do
    keydir="$REPOCOP_STATEDIR/init-script/$repocop_pkg_key"
    STATUS=ok
    MESSAGE=
    for i in "$keydir"/*; do
        filename=${i##$keydir/}
	if [ -x $i ]; then
	    HAS_INIT=`grep '# chkconfig:' $i`
	    HAS_LSB_INIT=`grep '### BEGIN INIT INFO' $i`
            if [ -z "$HAS_LSB_INIT" ]; then
                if [ -n "$HAS_INIT" ]; then
		    if grep -q '^'$filename'$' $REPOCOP_TEST_TMPDIR/systemd_services; then
			STATUS=warn
			MESSAGE="$MESSAGE/etc/rc.d/init.d/$filename: lsb init header missing. "
                    else
			STATUS=fail
			MESSAGE="$MESSAGE/etc/rc.d/init.d/$filename: not systemd compatible: lsb init header missing and ${filename}.service is not present. "
	            fi
                else
		    STATUS=warn
                    MESSAGE="$MESSAGE/etc/rc.d/init.d/$filename: strange executable: neither lsb header nor chkconfig header aren't found. "
                fi
            fi
        fi
    done
    repocop-test-$STATUS -k $repocop_pkg_key "$MESSAGE See http://www.altlinux.org/Services_Policy for details."
done
rm -f $REPOCOP_TEST_TMPDIR/systemd_services
