# Copyright 2000, Sun/AOL Alliance Messaging Divlet. All Rights Reserved.
# All Rights Reserved
#
# This ONLINE script is used by the VCS agent to start a data service.
# The script is known to be compatiable with VCS 1.1.1. agent API but
# not guaranteed to be working with other versions of VCS. In which
# case modification may be required.

eval 'exec $VCS_HOME/bin/perl5 -S $0 ${1+"$@"}'
        if 0;

# InstanceName here is pertinent only to LDAP
($ResID, $State, $InstanceName, $LogHostName, $PrtStatus, $DebugMode) = @ARGV;

if ($DebugMode eq "true" || $DebugMode eq "True") {
	print "$ResID, $InstanceName, $LogHostName \n";
	}

require ("/opt/VRTSvcs/bin/MsgSrv/sub.pl");

# There is no MessagingServer Instance in IMS6.x releases

$ServerRootPath = parse("RootPath", "", "");
$LDAPInstance = "${ServerRootPath}/slapd-${InstanceName}";
$AdminServInstance = get_admin_server_root("$ServerRootPath");

if ($DebugMode eq "true" || $DebugMode eq "True") {
	#print "MsgInstance = msg-$InstanceName \n";	
	print "ServerRootPath = $ServerRootPath \n";	
	print "LDAPInstance = $LDAPInstance \n";	
	print "AdminServerInstance = $AdminServInstance \n";
}
#
# Start slapd:
#
if ( -f "${LDAPInstance}/start-slapd" ) { 
  	if (system("${LDAPInstance}/start-slapd") != 0) { 
    	  print "ONLINE ERROR: LDAP Server start failed.\n";
    	  exit 1;
  	} else {
    	  print "ONLINE: LDAP Server started successfully.\n";
  	}
  } else {
    	print "ONLINE NOTICE: LDAP Server not found locally.\n"; 
}

# Start watcher first:
#
# There is no MessagingServer Instance in IMS6.x releases
if ( -f "${ServerRootPath}/sbin/start-msg" ) { 
  	if (system("${ServerRootPath}/sbin/start-msg watcher") != 0) { 
    	    print "ONLINE ERROR: Messaging Server watcher failed to start. make sure to start the watcher\n";
    	    exit 1;
	  } else {
    	  print "ONLINE: Messaging Server stored started successfully.\n";
	  print " Sleeping for 20 seconds for the watcher to start and stabilize. \n";
	  sleep(20);
	}
  } else {
  	print "ONLINE ERROR: Messaging Server not found.\n"; 
  	exit 1;
}

# Start rest of the Msg Server:
# Start the following Messaging Server Components:
# IMAP, POP, HTTP, LMTP, job controller, dispatcher, ENS, SMS Gateway Server
#
if ( -f "${ServerRootPath}/sbin/start-msg" ) { 
  	if (system("${ServerRootPath}/sbin/start-msg ha") != 0) { 
    	  print "ONLINE ERROR: Starting of the rest of the Messaging Server start failed.\n";
    	  exit 1;
	} else {
    	  print "ONLINE: The rest of the Messaging Server started successfully.\n";
	  sleep (10);
	}
  } else {
  	print "ONLINE ERROR: Messaging Server not found.\n"; 
  	exit 1;
}

#
# Start Admin Server:
#

if ( -f "${AdminServInstance}/start-admin" ) { 
  	if (system("${AdminServInstance}/start-admin") != 0) { 
    	  print "ONLINE ERROR: Admin Server start failed.\n";
    	  exit 1;
	} else {
    	  print "ONLINE: Admin Server started successfully.\n";
	}
  } else {
  	print "ONLINE ERROR: Admin Server not found.\n"; 
  	exit 1;
}

exit 0; 
