#!/bin/sh
#
# Copyright (c) 2011 Ken McDonell.  All Rights Reserved.
# 
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# 
# Legacy wrapper for the Performance Co-Pilot Daemon(s) Start / Stop scripts
#
# This script is NOT part of the "rc" framework ... the "pmcd" and "pmlogger"
# scripts ARE part of the "rc" framework.  This script is provided as a
# legacy bridge for any scripts or procedures that used $PCP_RC_DIR/pcp
# from the days before PCP 3.6.
#
# The following is for chkconfig on RedHat based systems
# chkconfig:
# description: Legacy init script wrapper for the Performance Co-Pilot (PCP) daemons
#
# The following is for insserv(1) based systems,
# e.g. SuSE, where chkconfig is a perl script.
### BEGIN INIT INFO
# Provides:          pcp
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: Legacy control for PCP daemons
# Description:       Legacy init script wrapper for the Performance Co-Pilot (PCP) daemons
### END INIT INFO

. $PCP_DIR/etc/pcp.env

# for chasing arguments we're passed from init/systemd/...
#
#debug# echo "$*: `date`" >>$PCP_LOG_DIR/rc_pcp.log
#debug# env >>$PCP_LOG_DIR/rc_pcp.log

tmp=`mktemp -d "$PCP_TMPFILE_DIR/pcp_rc.XXXXXXXXX"` || exit 1
status=0
trap "rm -rf $tmp; exit \$status" 0 1 2 3 15
prog=$PCP_RC_DIR/pcp

_usage()
{
    echo "Usage: $pmprog [-v] {start|restart|condrestart|stop|status|reload|force-reload}"
}

# wrapper for PCP daemon init scripts ... use systemctl if available,
# else run the PCP scripts directly
#
# Usage: _service <service> [on|off|restart|...]
#
_service()
{
    if [ $# -ne 2 ]
    then
	echo "_service: expect 2 arguments, not $# ($*)"
	exit
    fi
    if [ -n "$PCP_SYSTEMDUNIT_DIR" -a -f $PCP_SYSTEMDUNIT_DIR/$1.service -a "$PCPQA_SYSTEMD" != no ]
    then
	# smells like systemctl is the go ...
	#
	systemctl $2 $1.service
    elif [ -f $PCP_RC_DIR/$1 ]
    then
	$PCP_RC_DIR/$1 $2
    else
	echo "_service: $1 is not a systemctl service nor a $PCP_RC_DIR script!"
	exit
    fi
}

case "$1" in

  'start'|'restart'|'condrestart'|'reload'|'force-reload')
	_service pmcd $*
	_service pmlogger $*
        ;;

  'stop')
	_service pmlogger $*
	_service pmcd $*
        ;;

  'status')
	_service pmcd $* || status=$?
	_service pmlogger $* || status=$?
	;;

  *)
	_usage
        ;;
esac
