/*!
  @header FTExport
  @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

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

#include <Foundation/Foundation.h>
#include <FTExport.h>
#include <FTGraphVisitor.h>

@implementation FTExport

+ (ECLogger *) log {
  return [ECLogging loggerForContext: @"FTExport"];
}

- init {
  self = [super init];
  
  self->output = [[ECStandardOutput alloc] init];
  self->fileEncoding = NSUTF8StringEncoding;
  
  self->login = nil;
  self->passwd = nil;
  
  return self;
}


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


- (BOOL) cmdLineArgumentsOK {
  BOOL gotGraphIdString = NO;
  BOOL gotExportDir = NO;
  BOOL gotBaseExportFilename = NO;
  BOOL gotLogin = NO;
  BOOL gotPasswd = NO;
  
  EC_AUTORELEASEPOOL_BEGIN
  NSString *arg;
  
  NSEnumerator *arguments = [[[NSProcessInfo processInfo] arguments]
    objectEnumerator];
  
  while( (arg = [arguments nextObject]) ) {
    if( [arg isEqualToString: @"-graphId"] ) {
      self->graphIdString = [arguments nextObject];
      gotGraphIdString = nil != self->graphIdString;
      continue;
    } else if( [arg isEqualToString: @"-exportDir"] ) {
      NSFileManager *fileMgr = [NSFileManager defaultManager];
      self->exportDir = [arguments nextObject];
      BOOL isDir;
      
      if( nil == self->exportDir ) {
        /* incorrect usage, so: */
        break;
      }
      
      if( ![fileMgr fileExistsAtPath: self->exportDir isDirectory: &isDir] ) {
        [self->output 
          writeString: [NSString 
            stringWithFormat: @"Directory \"%@\" for export does not exist!\n",
            self->exportDir]];
        break;
      }
      
      if( !isDir) {
        [self->output 
          writeString: [NSString 
            stringWithFormat: @"Given export directory \"%@\" is not a "\
              "directory!\n",  self->exportDir]];
        break;
      }
      
      if( ![fileMgr isWritableFileAtPath: self->exportDir] ) {
        [self->output 
          writeString: [NSString 
            stringWithFormat: @"Have no permissions to write into directory "\
              "\"%@\"\n", self->exportDir]];
        break;
      }
       
      gotExportDir = YES;
    } else if( [arg isEqualToString: @"-baseExportFilename"] ) {
      self->baseExportFilename = [arguments nextObject];
      gotBaseExportFilename = nil != self->baseExportFilename;
      continue;
    } else if( [arg isEqualToString: @"-login"] ) {
      self->login = [arguments nextObject];
      gotLogin = YES;
      continue;
    } else if( [arg isEqualToString: @"-password"] ) {
      self->passwd = [arguments nextObject];
      gotPasswd = YES;
    }
  }
  EC_AUTORELEASEPOOL_END  
  return gotGraphIdString && gotExportDir && gotBaseExportFilename 
    && gotLogin && gotPasswd;
}


#define _argumentsOverview \
  @"\nCommand line parameters:\n"\
    "\t-configFile <FT config file>\n\t\tFT configuration file\n"\
    "\t-graphId <graph identifier>\n\t\tspecifies a graph. Assumes that the "\
      "graph identifier is of type string\n\n"\
    "\t-exportDir <export director>\n\t\t"\
    "Specifies the directory where the output files have to be written to\n\n"\
    "\t-baseExportFilename <base filename for export files>\n"\
    "\t\tSpecifies the base filename for all generated files\n"\
    "\t-login <adminlogin>\n\t\tlogin account for an admin user\n"\
    "\t-password <passwd>\n\t\tpassword of the specified login"

- cmdLineArgumentsOverview {
  [self->output writeString:  _argumentsOverview];
  return self;
}


- (int) fileEncoding {
  return self->fileEncoding;
}


- (ECLogger *) log {
  return [FTExport log];
}


- run {
  EC_AUTORELEASEPOOL_BEGIN
  FTGraphVisitor *visitor;
  
  [self startServerUsingLogin: self->login
    usingPassword: self->passwd];
  
  if( ![[self session] 
    conformsToProtocol: @protocol(FTAdministrationSession)] ) {
    [self->output writeString: @"Specified login session does not have "\
      "administrative rights!"];
    EC_AUTORELEASEPOOL_RELEASE;
    return self;
  }
  
  visitor = [[[FTGraphVisitor alloc] 
    initForSession: ( id <FTAdministrationSession>) [self session]
    graphId: self->graphIdString
    exportDirectory: self->exportDir
    baseFilename: self->baseExportFilename encoding: self->fileEncoding] 
      autorelease];
    
  [visitor exportContent];
  EC_AUTORELEASEPOOL_END
  
  return self;
}

@end


int main( int argc, char *argv[] ) {
  EC_AUTORELEASEPOOL_BEGIN
  FTExport *exportTool;
  
  NSLog( @"FTExport Version %@", FTEXPORT_VERSION );
  
  exportTool = [[[FTExport alloc] init] autorelease];
  
  [FTAbstractTool startTool: exportTool];
  
  EC_AUTORELEASEPOOL_END
  
  exit(  0 );
}

