#!/bin/sh 
#
#  COPYRIGHT 2010, KRUPCZAK.ORG, LLC.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA
#
#  For more information, visit:
#  http://www.krupczak.org/

# Cartographer startup script
# chkconfig: 2345 55 55
# description: Cartographer daemon for network/system/application management
# 

versionStr="KorgVersion 1.05";
SYSTEMCTL_SKIP_REDIRECT=1

# no need to source function library as its not available on all linux
# distros and we're not using it anyway
if [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions 
else
  echo "Init functions not present";
  echo_success() { /bin/echo "ok"; }
  echo_failure() { /bin/echo "failed"; }
fi

CARTOGRAPHERDIR=/opt/cartographer
PATH=$PATH:$CARTOGRAPHERDIR
export LD_LIBRARY_PATH=/opt/cartographer:$LD_LIBRARY_PATH

RETVAL=0

case "$1" in
   start)
       cd $CARTOGRAPHERDIR
       if [ -x xmpd-linux ]; then
          echo -n $"Starting Cartographer: "
          xmpd-linux -b -g
          RETVAL=$?
	  if [ $RETVAL -eq 0 ]; then
              echo_success
	  else
	      echo_failure
	  fi
	  echo
       fi
       ;;

   stop)
       echo -n $"Stopping Cartographer:  "
       /usr/bin/pkill -u root xmpd-linux
       RETVAL=$?
       # sleep while we wait for shutdown to occur gracefully
       sleep 30
       echo_success
       echo
       ;;

   status)
       /usr/bin/pgrep -u root xmpd-linux > /dev/null 2>&1
       RETVAL=$?
       if [ $RETVAL -eq 0 ]; then
	   echo "cartographer agent is running";
       else
	   echo "cartographer agent is not running";
       fi
       ;;

   restart)
       $0 stop
       $0 start
       RETVAL=$?
       ;;
 
   *)
       echo "Usage: cartographer {start|stop|restart}"
       exit 1
       ;;
esac

exit $RETVAL
