#!/bin/sh
# Copyright (c) 2010, Heinz Ulrich Krause
#
# Author: Heinz Ulrich Krause, <eknori@eknori.de>, 02/2010
#
# /etc/init.d/sametime
#
# System startup script for the Lotus Sametime Server Components
#
### BEGIN INIT INFO
# Provides:       sametime
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    System startup script for the Lotus Sametime Server Components
### END INIT INFO

. /etc/profile
. /etc/rc.status

ST_BASEDIR="/opt/IBM/WebSphere/AppServer/profiles"
DMGR_PROFILE="STSCDMgrProfile"
STSC_APP_PROFILE="STSCAppProfile"
MEETING_SERVER_MGR="meetingMeetingDMProfile1"
MEETING_SERVER_SRV="meetingMeetingPNProfile1"
MEDIA_SERVER_MGR="mediaMediaDMProfile1"
MEDIA_SERVER_SRV="mediaMediaPNProfile1"
PROXY_SERVER_MGR="proxyProxyDMProfile1"
PROXY_SERVER_SRV="proxyProxyPNProfile1"
CMD_START_MANAGER="bin/startManager.sh"
CMD_START_NODE="bin/startNode.sh"
CMD_START_SERVER="bin/startServer.sh"
CMD_STOP_MANAGER="bin/stopManager.sh"
CMD_STOP_NODE="bin/stopNode.sh"
CMD_STOP_SERVER="bin/stopServer.sh"
CMD_SERVER_STATUS="bin/serverStatus.sh -all"

WAS_USER="YourWasadminHere"
WAS_USER_PWD="YourPasswordHere"
CREDENTIALS="-username ${WAS_USER} -password ${WAS_USER_PWD}"

. /etc/profile
. /etc/rc.status

rc_reset

# =======================================================
# DISPLAY_USAGE
# =======================================================
display_usage(){
echo -e ""
echo "IBM Lotus Notes Sametime start/stop script"
echo -e "(c) 2010, Ulrich Krause - eknori.de\n"
echo -e "Usage: $0 start|stop|reload|restart|status [server]\n"
echo -e "SAMPLE: $0 restart meeting\n"
echo -e "will first stop all meeting server processes, then start again\n"
}

# =======================================================
# START
# =======================================================
start_dmgr() {
echo "Starting IBM Lotus Sametime Deployment Manager"
${ST_BASEDIR}/${DMGR_PROFILE}/$CMD_START_MANAGER
rc_status -v
}

start_console(){
echo "Starting IBM Lotus Sametime System Console NodeAgent"
${ST_BASEDIR}/${STSC_APP_PROFILE}/$CMD_START_NODE
rc_status -v

echo "Starting IBM Lotus Sametime System Console Server"
${ST_BASEDIR}/${STSC_APP_PROFILE}/$CMD_START_SERVER STConsoleServer
rc_status -v
}

start_meeting(){
echo "Starting IBM Lotus Sametime Meeting Server Manager"
${ST_BASEDIR}/${MEETING_SERVER_MGR}/$CMD_START_MANAGER
rc_status -v

echo "Starting IBM Lotus Sametime Meeting Server NodeAgent"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_START_NODE
rc_status -v

echo -e "Starting IBM Lotus Sametime Meeting Server\n"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_START_SERVER STMeetingServer
rc_status -v

echo -e "Starting IBM Lotus Sametime Meeting Server Http Proxy\n"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_START_SERVER STMeetingHttpProxy
rc_status -v
}

start_media(){
echo -e "Starting IBM Lotus Sametime Media Server Manager\n"
${ST_BASEDIR}/${MEDIA_SERVER_MGR}/$CMD_START_MANAGER
rc_status -v

echo -e "Starting IBM Lotus Sametime Media Server NodeAgent\n"
${ST_BASEDIR}/${MEDIA_SERVER_SRV}/$CMD_START_NODE
rc_status -v

echo -e "Starting IBM Lotus Sametime Media Server\n"
${ST_BASEDIR}/${MEDIA_SERVER_SRV}/$CMD_START_SERVER STMediaServer
rc_status -v
}

start_proxy(){
echo -e "Starting IBM Lotus Sametime Proxy Server Manager\n"
${ST_BASEDIR}/${PROXY_SERVER_MGR}/$CMD_START_MANAGER
rc_status -v

echo -e "Starting IBM Lotus Sametime Proxy Server NodeAgent\n"
${ST_BASEDIR}/${PROXY_SERVER_SRV}/$CMD_START_NODE
rc_status -v

echo -e "Starting IBM Lotus Sametime Proxy Server\n"
${ST_BASEDIR}/${PROXY_SERVER_SRV}/$CMD_START_SERVER STProxyServer
rc_status -v
}

stop_meeting(){
echo "Stopping IBM Lotus Sametime Meeting Server Http Proxy"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_STOP_SERVER STMeetingHttpProxy ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Http Server stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Meeting Server"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_STOP_SERVER STMeetingServer ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Meeting Server NodeAgent"
${ST_BASEDIR}/${MEETING_SERVER_SRV}/$CMD_STOP_NODE ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server NodeAgent stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Meeting Server Manager"
${ST_BASEDIR}/${MEETING_SERVER_MGR}/$CMD_STOP_MANAGER ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server Manager stopped"
rc_status -v
}

stop_console(){
echo "Stopping IBM Lotus Sametime System Console Server"
${ST_BASEDIR}/${STSC_APP_PROFILE}/$CMD_STOP_SERVER STConsoleServer ${CREDENTIALS}
echo "IBM Lotus Sametime System Console Server stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime System Console NodeAgent"
${ST_BASEDIR}/${STSC_APP_PROFILE}/$CMD_STOP_NODE ${CREDENTIALS}
echo "IBM Lotus Sametime System Console Node stopped"
rc_status -v
}

stop_dmgr(){
echo "Stopping IBM Lotus Sametime Deployment Manager"
${ST_BASEDIR}/${DMGR_PROFILE}/$CMD_STOP_MANAGER ${CREDENTIALS}
echo "IBM Lotus Sametime Deployment Manager stopped"
rc_status -v
}

stop_media(){
echo "Stopping IBM Lotus Sametime Media Server"
${ST_BASEDIR}/${MEDIA_SERVER_SRV}/$CMD_STOP_SERVER STMediaServer ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Media Server NodeAgent"
${ST_BASEDIR}/${MEDIA_SERVER_SRV}/$CMD_STOP_NODE ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server NodeAgent stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Media Server Manager"
${ST_BASEDIR}/${MEDIA_SERVER_MGR}/$CMD_STOP_MANAGER ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server Manager stopped"
rc_status -v
}

stop_proxy(){
echo "Stopping IBM Lotus Sametime Proxy Server"
${ST_BASEDIR}/${PROXY_SERVER_SRV}/$CMD_STOP_SERVER STProxyServer ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Proxy Server NodeAgent"
${ST_BASEDIR}/${PROXY_SERVER_SRV}/$CMD_STOP_NODE ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server NodeAgent stopped"
rc_status -v

echo "Stopping IBM Lotus Sametime Proxy Server Manager"
${ST_BASEDIR}/${PROXY_SERVER_MGR}/$CMD_STOP_MANAGER ${CREDENTIALS}
echo "IBM Lotus Sametime Meeting Server Manager stopped"
rc_status -v
}

status_dmgr(){
$ST_BASEDIR/$DMGR_PROFILE/$CMD_SERVER_STATUS ${CREDENTIALS}
}

status_console(){
$ST_BASEDIR/$STSC_APP_PROFILE/$CMD_SERVER_STATUS ${CREDENTIALS}
}

status_meeting(){
$ST_BASEDIR/$MEETING_SERVER_MGR/$CMD_SERVER_STATUS ${CREDENTIALS}
$ST_BASEDIR/$MEETING_SERVER_SRV/$CMD_SERVER_STATUS ${CREDENTIALS}
}

status_media(){
$ST_BASEDIR/$MEDIA_SERVER_MGR/$CMD_SERVER_STATUS ${CREDENTIALS}
$ST_BASEDIR/$MEDIA_SERVER_SRV/$CMD_SERVER_STATUS ${CREDENTIALS}
}

status_proxy(){
$ST_BASEDIR/$PROXY_SERVER_MGR/$CMD_SERVER_STATUS ${CREDENTIALS}
$ST_BASEDIR/$PROXY_SERVER_SRV/$CMD_SERVER_STATUS ${CREDENTIALS}
}

start_all(){
start_dmgr
start_console
start_meeting
#start_media
#start_proxy
}

stop_all(){
#stop_proxy
#stop_media
stop_meeting
stop_console
stop_dmgr
}

case "$1" in
start)
	case "$2" in
	basic)
		start_dmgr
		start_console
	;;
	console)
		start_console
	;;
	dmgr)
		start_dmgr
	;;
	meeting)
		start_meeting
	;;
	media)
		start_media
	;;
	proxy)
		start_proxy
	;;
	*)
		start_all
	;;
	esac
;;

stop)
	case "$2" in
	basic)
		stop_console
		stop_dmgr
	;;
	console)
		stop_console
	;;
	dmgr)
		stop_dmgr
	;;
	meeting)
		stop_meeting
	;;
	media)
		stop_media
	;;
	proxy)
		stop_proxy
	;;
	*)
		stop_all
	;;
	esac
;;
status)
	case "$2" in

	console)
		status_console
	;;
	dmgr)
		status_dmgr
	;;
	meeting)
		status_meeting
	;;
	media)
		status_media
	;;
	proxy)
		status_proxy
	;;
	*)
		exit 1
	;;
	esac
;;
reload|restart)
	case "$2" in
	basic)
		stop_console
		stop_dmgr
		start_dmgr
		start_console
	;;
	console)
		stop_console
		start_console
	;;
	dmgr)
		stop_dmgr
		start_dmgr
	;;
	meeting)
		stop_meeting
		start_meeting
	;;
	proxy)
		stop_proxy
		start_proxy
	;;
	media)
		stop_media
		start_media
	;;
	*)
		stop_all
		start_all
	;;
	esac
;;

*)
display_usage
exit 1
esac
exit 0