/*!
  @header FTAbstractTool
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  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; either
  version 2.1 of the License, or (at your option) any later version.

  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
  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, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  27.07.06 ola     initial version
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <FTAbstractTool.h>
#include <Encore/Encore.h>

@implementation FTAbstractTool

+ (void) startTool: (FTAbstractTool *) tool {
  if( [tool cmdLineArgumentsOK] ) {
    [tool run];
  } else {
    [tool cmdLineArgumentsOverview];
  }
}


- init {
  self = [super init];

  self->server = nil;
  self->session = nil;
  
  return self;
}


- (void) dealloc {
  [self->server release];
  [self->session release];
  
  [super dealloc];
}


- (NSEnumerator *) cmdLineArguments {
  return [[[NSProcessInfo processInfo] arguments]
    objectEnumerator];
}


- (BOOL) cmdLineArgumentsOK {
  [[[ECNotImplementedException alloc]
    initWithOperationInformation: @"FTAbstractTool::cmdLineArgumentsOK"]
    raise];
    
  return NO;
}


- cmdLineArgumentsOverview {
  [[[ECNotImplementedException alloc]
    initWithOperationInformation: @"FTAbstractTool::cmdLineArgumentsOK"]
    raise];
    
  return self;
}


- (ECLogger *) log {
  [[[ECNotImplementedException alloc]
    initWithOperationInformation: @"FTAbstractTool::log"]
    raise];
    
  return nil;
}


- run {
  [[[ECNotImplementedException alloc]
    initWithOperationInformation: @"FTAbstractTool::cmdLineArgumentsOK"]
    raise];
    
  return self;
}


- (id <FTServer>) server {
  return self->server;
}


- (id <FTSession>) session {
  return self->session;
}


- startServerUsingLogin: (NSString *) login
  usingPassword: (NSString *) aPassword {

  FTBootstrap *bootstrap;
  id <FTSessionManager> sessionManager;
  
  if( [[self log] isInfoEnabled] ) {
    [[self log]
      info: @"Starting server..."];
  }
  
  bootstrap = [FTBootstrap bootstrap];
  self->server = [[bootstrap startServer] retain];
  sessionManager = [[server sessionManager] retain];
  self->session = [sessionManager 
    loginAs: login 
    withPassword: aPassword];

  if( [[self log] isInfoEnabled] ) {
    [[self log] info:@"Server up and running..." ];
  }

  return self;
}


- closeSessionAndStopServer {
  if( [[self log] isInfoEnabled] ) {
    [[self log]
      info: @"Shutting down the server..."];
  }
 
  [(id <FTAdministrationSession>) self->session shutdownServer];

  [self->session release];
  self->session = nil;

  return self;
}
@end

