#!/bin/sh -e
#
# If valgrind can do it, I want memory leak checking, too.
#
case `uname -i` in
i?86)
	valgrind="valgrind -v --tool=memcheck --trace-children=yes --log-file=oddjob-valgrind --leak-check=yes --show-reachable=yes --leak-check=full --error-limit=no"
	;;
*)
	valgrind=
	;;
esac

# Check out.
cvs -d :pserver:anonymous@rhlinux.redhat.com:/usr/local/CVS login
cvs -d :pserver:anonymous@rhlinux.redhat.com:/usr/local/CVS co oddjob

# Build.
pushd oddjob
./autogen
./configure --enable-maintainer-mode
make
make dist
rpmbuild -tb oddjob-0.??-?.tar.gz
popd

# Install.
to_install=
rpm -qa 'oddjob*' | xargs --no-run-if-empty rpm -ev
for package in `rpm -q --specfile oddjob/oddjob.spec` ; do
	pkg=/usr/src/redhat/RPMS/`uname -i`/$package.`uname -i`.rpm
	if test -s $pkg ; then
		to_install="$to_install $pkg"
	fi
done
rpm -Uvh $to_install

# Force D-BUS to let us bind to the service names which we want.
set +e
daemonpid=`pidof dbus-daemon-1 2> /dev/null; pidof dbus-daemon 2> /dev/null`
set -e
kill -HUP $daemonpid

# Start.
$valgrind /usr/sbin/oddjobd -p /var/run/oddjobd.pid
touch /var/lock/subsys/oddjobd
set +e
oddjob_request -o /com/redhat -I > output1.xml
oddjob_request -o /com/redhat/sample -i com.redhat.sample sample > output1.log 2>&1
set -e
# Check.
trap 'rm -f /etc/oddjobd.conf.d/test.conf' EXIT
cat > /etc/oddjobd.conf.d/test.conf << EOF
<?xml version="1.0"?>
<oddjobconfig>
  <service name="com.redhat.oddjob">
    <object name="/com/redhat/sample">
      <allow user="root"/>
      <interface name="com.redhat.sample">
        <method name="sample">
          <helper exec="/bin/pwd"
		  arguments="0"
		  prepend_user_name="no"/>
        </method>
        <method name="ls">
          <helper exec="/bin/ls"
		  arguments="0"
		  prepend_user_name="no"/>
        </method>
      </interface>
      <interface name="org.freedesktop.DBus.Introspectable">
      </interface>
    </object>
  </service>
</oddjobconfig>
EOF
/sbin/service oddjobd reload

# Should succeed, and the node should show up in the introspection output.
set +e
oddjob_request -o /com/redhat -I > output2.xml
diff -u output1.xml output2.xml
oddjob_request -o /com/redhat/sample -i com.redhat.sample sample > output2.log 2>&1
diff -u output1.log output2.log
set -e

# Should be the same as case 1.
rm -f /etc/oddjobd.conf.d/test.conf
/sbin/service oddjobd reload
set +e
oddjob_request -o /com/redhat -I > output3.xml
diff -u output1.xml output3.xml
oddjob_request -o /com/redhat/sample -i com.redhat.sample sample > output3.log 2>&1
diff -u output1.log output3.log

/sbin/service oddjobd stop
rpm -qa 'oddjob*' | xargs --no-run-if-empty rpm -ev

