#!/bin/sh

CONFIGFILE=/6100/etc/coredata-dbus.conf

dbus_pid_file=/dev/shm/coredata-dbus.pid
coredatad_pid_file=/dev/shm/coredatad.pid
persistd_pid_file=/dev/shm/persistd.pid

start()
{
    echo -n "Starting core dbus daemon... "
    /usr/bin/dbus-daemon --config-file=${CONFIGFILE} --print-pid 3 3> ${dbus_pid_file}
    echo "done"

    echo -n "Starting coredatad... "
    /6100/bin/coredatad --pidfile $coredatad_pid_file
    echo "done"

    echo -n "Starting persistd... "
    /6100/bin/persistd --pidfile $persistd_pid_file
    echo "done"
}

stop()
{
    if [ -f $persistd_pid_file ]
    then
        echo -n "Stopping persistd..."
        kill `cat $persistd_pid_file` 2>&1 >/dev/null
        rm $persistd_pid_file
        echo "done"
    fi

    if [ -f $coredatad_pid_file ]
    then
        echo -n "Stopping coredatad..."
        kill `cat $coredatad_pid_file` 2>&1 >/dev/null
        rm $coredatad_pid_file
        echo "done"
    fi

    if [ -f ${dbus_pid_file} ]
    then
        echo -n "Stopping coredata dbus daemon... "
        kill `cat ${dbus_pid_file}` 2>&1 >/dev/null && rm -f ${dbus_pid_file}
        echo  done
    fi
}

case "$1" in
    'start'|'restart')
        stop
        start
        ;;
    'stop')
        stop
        ;;
    *)
        echo "usage: coredata start|restart|stop"
        echo "   Start or stop everything needed for coredata."
        ;;
esac
