/***********************************************************************
 *
 *  Copyright (C) 2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library 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
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 *
 *  To contact Novell about this file by physical or electronic mail,
 *  you may find current contact information at www.novell.com.
 *
 *  Authors: Juan Carlos Luciani <jluciani@novell.com>
 *           Greg Richardson <grichardson@novell.com>
 *
 ***********************************************************************/

import java.util.Properties;
import java.io.File;

/**
 * Unconfigure Class.
 * <p>
 * This class readies the Authentication Token Service to be un-installed.
 */
public class Unconfigure
{
   // Configured properties
   Properties  m_properties;

   // Completion code
   int m_rc;

   /**
    * Constructor.
    *
    * @param properties Configuration properties.
    */
   Unconfigure(Properties  properties)
   {
      m_rc = AtsConfigurator.ERROR_NO_ERROR;

      AtsConfigurator.log("Unconfigure()- Start");

      try
      {
         // Save the properties
         m_properties = properties;

         // delete the ATS service
         m_rc = deleteATSService();
      }
      catch (Exception e)
      {
         m_rc = AtsConfigurator.ERROR_EXCEPTION;
         AtsConfigurator.log("Exception" + e.getMessage());
      }

      AtsConfigurator.log("Unconfigure()- End, rc= ", m_rc);
   }

   /**
    * Delete ATS Service.
    *
    * @return     Return code.
    */
   int deleteATSService()
   {
      int rc = AtsConfigurator.ERROR_NO_ERROR;

      AtsConfigurator.log("Unconfigure.deleteATSService()- Start");

      // Determine the path to the Tomcat5 executable
      String sExe = (String) m_properties.get(AtsConfigurator.TOMCAT5_PROPERTY);
      if (sExe == null)
         sExe = ((String) m_properties.get(AtsConfigurator.TOMCAT_HOME_PROPERTY)) + "\\bin\\tomcat5.exe";

      /*
      * Note that in the following code we do not bother to check the return of the invokeExternalCommand
      * call. This is because I have found that some versions of tomcat5.exe do not always return success
      * even though they should.
      */
      String[] commandArray = {
         sExe, "//SS//CasaAuthTokenService", "--LogPath", AtsConfigurator.m_logFolderPath, "--LogPrefix", "AtsSvcUninstall.log"
      };
      AtsConfigurator.invokeExternalCommand(commandArray);

      String[] commandArray1 = {
         sExe, "//DS//CasaAuthTokenService", "--LogPath", AtsConfigurator.m_logFolderPath, "--LogPrefix", "AtsSvcUninstall.log"
      };
      AtsConfigurator.invokeExternalCommand(commandArray1);

      AtsConfigurator.log("Unconfigure().deleteATSService- End, rc= ", rc);

      return rc;
   }
}