# Copyright 2000, Sun/AOL Alliance Messaging Divlet. All Rights Reserved.
# All Rights Reserved
#
# This OFFLINE script is used by the VCS agent to stop 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 valid only in case of LDAP Servers

($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");

$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";       
} 

#
# Must stop Admin Server first. 
#
if ( -f "${AdminServInstance}/stop-admin" ) { 
	$admin_serv = getproc("ns-admin","$AdminServInstance");
  	if ($admin_serv =~ /[1-9]/ && system("${AdminServInstance}/stop-admin") != 0) { 
    	  print "OFFLINE ERROR: Admin Server stop failed.\n";
	} else {
    	  print "OFFLINE: Admin Server stopped successfully.\n";
	}
  } else {
  	print "OFFLINE ERROR: Admin Server not found.\n"; 
}

#
# Stop slapd
#
if ( -f "${LDAPInstance}/stop-slapd" ) { 
  	if (system("${LDAPInstance}/stop-slapd") != 0) { 
    	  print "OFFLINE ERROR: LDAP Server stop failed.\n";
  	} else {
    	  print "OFFLINE: LDAP Server stopped successfully.\n";
  	}
  } else {
    	print "OFFLINE NOTICE: LDAP Server not found locally.\n"; 
}

#
# Stop Msg Server.
#
if ( -f "${ServerRootPath}/sbin/stop-msg" ) { 
	if (system("${ServerRootPath}/sbin/stop-msg ha") != 0) { 
    	  print "OFFLINE ERROR: Messaging Server stop failed.\n";
    	  exit 1;
	} else {
    	  print "OFFLINE: Messaging Server stopped successfully.\n";
	}
  } else {
  	print "OFFLINE ERROR: Messaging Server not found.\n"; 
  	exit 1;
}

exit 0; 
