/**
 *  File:
 *    OSRLogFile.ycp
 *
 *  Module:
 *    YaST2 OS Repair. Automatic error detection & repair tool for Linux.
 *
 *  Summary:
 *    YaST2 OS Repair. Automatic error detection & repair tool for Linux.
 *
 *  Author:
 *    Michael Koehrmann <curry@suse.de>
 *    Johannes Buchhold <jbuch@suse.de>
 *
 * $Id: OSRLogFile.ycp 14418 2004-02-19 16:32:26Z jsuchome $
 */
{
  module "OSRLogFile";

  /**
   * The name for the log file
   */
  string file = "";

  /**
   *  Returns the path of the global temporary directory, e.g.
   *  "/tmp/YaST2-02274".
   *  @return string The path of the temporary directory.
   *  @example string tmpdir = GetTmpDir();
   */
  global define string GetTmpDir() ``{
      return (string) WFM::Read (.local.tmpdir, "");
  };

  /**
   *  Tests if the specified logfile exists, is readable, writable, ...
   *  If the specified file not exists, it will be created with the correct
   *  file permissions.
   *
   *  For internal use only.
   *
   *  @param string new_file Name of the log file.
   *  @return True if the specified logfile was created/set up successfully.
   */
  define boolean SetFile(string new_file) ``{

      // set the class attribute file to the specified file name
      file = new_file;

      // write the actual date to the log file
      if (WFM::Execute(.local.bash,
	    sformat("/bin/date >> %1; echo >> %1", new_file))  == 0 )
      {
	  // check if the new file is written correctly
	  if ( WFM::Execute(.local.bash, "/usr/bin/test -f " + new_file) == 0)
	  {
	      y2milestone("The log file was set up correctly: %1", new_file);
	      return true;
	  }
      }
      y2error("The log file was not set up correctly: %1", new_file);
      return false;
  };

  /**
   *  Constructor.
   *
   *  For internal use only.
   */
  global define void OSRLogFile() ``{

      if (! SetFile( GetTmpDir() + "/osr_log" ))
      {
	  y2error("Setup of the log file failed.");
      }
  };

  /**
   *  This function returns the name of the global log file.
   *  @return string The name of the global log file.
   */
  global define string GetLogFileName() ``{
      return file;
  };

  /**
   *  Writes the specified string to the end of the log file.
   *
   *  @param string the_string The string that should be written
   *	to the end of the log file.
   *  @return True if the specified string was successfully written
   */
  global define boolean Add(string the_string) ``{

      if (WFM::Execute(.local.bash,
	    sformat("/bin/echo \'%1\' >> %2", the_string, file) ) == 0 )
      {
	  y2milestone("String %1 successfully written to log file %2",
	    the_string, file);
	  return true;
      }
      else
      {
	  y2error("String: %1 not written to log file %2", the_string, file);
	  return false;
      }
  };
}
