<?php
	if(count($argv) < 2)
	{
    	print "Usage: configure (install | upgrade <version> | configure | remove | enable | disable)\n";
    	exit(1);
	}

	$command = $argv[1];
	switch ($command) {
		case "install":
			install();
			break;
		case "enable":
			enable();
			break;
		case "disable":
			disable();
			break;
		case "remove":
			remove();
			break;
		case "configure":
			configure();
			break;
	}
	exit(0);

function configure()
	{
	echo "0";
	}

function install()
	{
		$username = getenv('SETTINGS_GroupAccountName');
		$userpasswd  = getenv('SETTINGS_GroupAccountPwd');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_aadminuser');
		$product = getenv('SETTINGS_product');
		$adminpwd = getenv('SETTINGS_adminpwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

		    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
		    $result = $client->CreateGroup(array(
			"request" => array(
			    "name" => "$username",
			    "password" => "$userpasswd",										
			    "credentials" => array(
				"server" => "$server_ip",
				"user" => "$adminuser",
				"password" => "$adminpwd"
			    )
			)
		    ));
		    if ($result->CreateGroupResult->result->errorCode == 0)
		    {
			print "<h1>Succeeded.</h1>";
//SEND MAIL
		$username = getenv('SETTINGS_GroupAccountName');
		$userpasswd  = getenv('SETTINGS_GroupAccountPwd');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_groupemail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');
		$mailbody = getenv('SETTINGS_zzmailbody');
		$product = getenv('SETTINGS_ProductName');
		
		$USER = $username;
		$PWD = $userpasswd;

		$string = "$mailbody";
                $patterns[0] = "/USER/";
                $patterns[1] = "/PWD/";
                $patterns[3] = "/PRODUCTNAME/";
                $replacements[0] = $USER;
                $replacements[1] = $PWD;
                $replacements[3] = $product;
		$replace = preg_replace($patterns, $replacements, $string);

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = $replace;

		$headers  = "Content-type: text/html; \r\n";
		$headers .= "From: <$adminemail>\r\n";
		$headers .= "Cc: <$Cc>\r\n";

		mail("$to", $subject, $message, $headers);

		    }
		    else
		    {
			print "Unable to create a group.";
			print $result->result->errorText."\n";
			exit(1);
		    }
// Create first account
/*		$username = getenv('SETTINGS_FirstAccountName');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_GroupAccountName');
		$product = getenv('SETTINGS_ProductName');
		$adminpwd = getenv('SETTINGS_GroupAccountPwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

		    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
		    $result = $client->CreateUser(array(
			"request" => array(
			    "username" => "$username",									
			    "product" => "$product",											
			    "credentials" => array(
				"server" => "$server_ip",
				"user" => "$adminuser",
				"password" => "$adminpwd"
			    )
			)
		    ));
		    if ($result->result->errorCode == 0)
		    {
			print "<h1>Succeeded.</h1>";
			print "Username: ".$result->username."</br>";
			print "Password: ".$result->password."</br>";
*/
/*SEND MAIL
		$username = getenv('SETTINGS_FirstAccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_AccountEmail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');
		$mailbody = getenv('SETTINGS_zzmailbody');

		$GUSER = getenv('SETTINGS_GroupAccountName');
		$product = getenv('SETTINGS_ProductName');
		$GPWD = getenv('SETTINGS_GroupAccountPwd');
		
		$USER = $result->username;
		$PWD = $result->password;

		$string = "$mailbody";
                $patterns[0] = "/USER/";
                $patterns[1] = "/PWD/";
//                $patterns[2] = "/GRNAME/";
//                $patterns[3] = "/GPAWD/";
                $patterns[4] = "/PRODUCTN/";
                $replacements[0] = $USER;
                $replacements[1] = $PWD;
//                $replacements[2] = $GUSER;
//                $replacements[3] = $GPWD;
                $replacements[4] = $product;
		$replace = preg_replace($patterns, $replacements, $string);

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = $replace;

		$headers  = "Content-type: text/html; \r\n";
		$headers .= "From: <$adminemail>\r\n";
		$headers .= "Cc: <$Cc>\r\n";

		mail("$to", $subject, $message, $headers);

		    }
		    else
		    {
			print "Unable to create a account.";
			exit(1);
		    } */
//	}
}

function remove()
	{
		$username = getenv('SETTINGS_GroupAccountName');
		$userpasswd  = getenv('SETTINGS_GroupAccountPwd');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_aadminuser');
		$product = getenv('SETTINGS_product');
		$adminpwd = getenv('SETTINGS_adminpwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

		    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
		    $result = $client->DeleteGroup(array(
			"request" => array(
			    "groupName" => "$username",									
			    "credentials" => array(
				"server" => "$server_ip",
				"user" => "$adminuser",
				"password" => "$adminpwd"
			    )
			)
		    ));
		    if ($result->DeleteGroupResult->result->errorCode == 0)
		    {
			print "<h1>Succeeded.</h1>";


//SEND MAIL
		$username = getenv('SETTINGS_GroupAccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_groupemail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html>
		    <head> 
			<title></title> 
		    </head>
		    <body> 
			<p>Group account info</p>
			<p>Group account ".$username." has been removed.</p>
			<p></p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n";

		mail("$to", $subject, $message, $headers);

		    }
		    else
		    {
			print "Unable to remove a group.";
			print $result->result->errorText."\n";
			exit(1);
		    }
	}

// UNSUSPEND

function enable()
	{
		$nextWeek = time() + (14 * 24 * 60 * 60);
		$username = getenv('SETTINGS_GroupAccountName');
		$userpasswd  = getenv('SETTINGS_GroupAccountPwd');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_aadminuser');
		$product = getenv('SETTINGS_product');
		$adminpwd = getenv('SETTINGS_adminpwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
    $result = $client->SetExpirationDateForGroup(array(
        "request" => array(
            "groupname" => "$username",
            "unix-timestamp" => "$nextWeek",
            "credentials" => array(
                "server" => "$server_ip",
                "user" => "$adminuser",
                "password" => "$adminpwd"
            )
        )
    ));
    if ($result->result->errorCode == 0)
    {
        print "<h1>Succeeded.</h1>";
		$username = getenv('SETTINGS_GroupAccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_groupemail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html> 
		    <body> 
			<p>Group account info</p>
			<p>Group account ".$username." has been unsuspened.</p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n"; 

		mail("$to", $subject, $message, $headers);
    }
    else
    {
        print "Unable to enable a group.";
	print $result->result->errorText."\n";
	exit(1);
    }
}


// SUSPEND

function disable()
	{
		$oldWeek = time() - (1 * 24 * 60 * 60);
		$username = getenv('SETTINGS_GroupAccountName');
		$userpasswd  = getenv('SETTINGS_GroupAccountPwd');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_aadminuser');
		$product = getenv('SETTINGS_product');
		$adminpwd = getenv('SETTINGS_adminpwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
    $result = $client->SetExpirationDateForGroup(array(
        "request" => array(
            "groupname" => "$username",
            "unix-timestamp" => "$oldWeek",
            "credentials" => array(
                "server" => "$server_ip",
                "user" => "$adminuser",
                "password" => "$adminpwd"
            )
        )
    ));
    if ($result->result->errorCode == 0)
    {
        print "<h1>Succeeded.</h1>";

		$username = getenv('SETTINGS_GroupAccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_groupemail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html> 
		    <body> 
			<p>Group account info</p>
			<p>Group account ".$username." has been suspened.</p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n"; 

		mail("$to", $subject, $message, $headers);
    }
    else
    {
        print "Unable to disable a group.";
	print $result->result->errorText."\n";
	exit(1);
    }
}
		
?>

