/******************************************** -*-c-*- ***********************************************/
/*                                                                                                  */
/* testMain.c:                                                                                      */
/*                                                                                                  */
/****************************************************************************************************/

/****************************************************************************************************/
/*                                                                                                  */
/*     Copyright (C) 2006 Joerg Kunze                                                               */
/*                                                                                                  */
/*     This file is part of siliconBrain documentation.                                             */
/*                                                                                                  */
/*     siliconBrain documentation is free software; you can redistribute it and/or modify           */
/*     it under the terms of the GNU General Public License as published by                         */
/*     the Free Software Foundation; either version 2 of the License, or                            */
/*     (at your option) any later version.                                                          */
/*                                                                                                  */
/*     siliconBrain documentation 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 General Public License for more details.                                                 */
/*                                                                                                  */
/*     You should have received a copy of the GNU General Public License                            */
/*     along with this program; if not, write to the Free Software                                  */
/*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                    */
/*                                                                                                  */
/****************************************************************************************************/
char *siliconBrainRelease       = "$siliconBrainRelease: 0.1.1 $";
char *siliconBrainRcsIdentifier = "$Id: testMain.main.c,v 1.4 2006/05/02 23:09:36 joerg Exp $";
char *siliconBrainSaveStamp     = "$siliconBrainSaveStamp: 2006/04/26 22:52:12, Joerg Kunze$";

/****************************************************************************************************/
/*                                                                                                  */
/* include:                                                                                         */
/*                                                                                                  */
/****************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

#include <regex.h>

#include <pthread.h>

#include "siliconBrainLib"

static String programName = c( "testMain: " );
static int    nullFile;

/****************************************************************************************************/
/*                                                                                                  */
/* infoPrint:                                                                                       */
/*                                                                                                  */
/****************************************************************************************************/
#define infoPrint( message ) print( programName ); println( (message) );

/****************************************************************************************************/
/*                                                                                                  */
/* main:                                                                                            */
/*                                                                                                  */
/****************************************************************************************************/
static String callFormater( Obstack *memory, String *formatedInputString, char *commandOptions ) {

   /*-----------------------*/
   /* two pipes: in and out */
   /*-----------------------*/
   int source[ 2 ]; pipe( source );
   int target[ 2 ]; pipe( target );

   /*-------------------------*/
   /* write text into command */
   /*-------------------------*/
   void *feedFormater( void *nonUsedParameter ) {

      write( source[ 1 ], formatedInputString->begin, formatedInputString->length );
      close( source[ 1 ] );
      return 0;
   }

   pthread_t feederThread;
   pthread_create( &feederThread, 0, &feedFormater, 0 );

   /*----------------*/
   /* start command  */
   /*----------------*/
   String formatedPrintName = which( memory, c( "formatedPrint" ) );

   Command formatedPrint = command( formatedPrintName, commandOptions, 0 );

   int status = 0;
   pid_t commandPid = executeRaw( formatedPrint, source[ 0 ], target[ 1 ], nullFile );

   close( target[ 1 ] );
   close( source[ 0 ] );

   /*--------------------------*/
   /* read result from command */
   /*--------------------------*/
   String result = pipeGet( target[ 0 ], memory );

   close( target[ 0 ] );

   /*-------------*/
   /* synchronize */
   /*-------------*/
   pthread_join( feederThread, 0          );
   waitpid     ( commandPid  , &status, 0 );

   if( status ) {
      infoPrint( c( "error in formatedPrint command" ) );
      exit( WEXITSTATUS( status ) );
   }

   return result;
}

/****************************************************************************************************/
/*                                                                                                  */
/* main:                                                                                            */
/*                                                                                                  */
/****************************************************************************************************/
int main() {

   nullFile = open( "/dev/null", O_RDONLY );

   puts( "testMain: starting all tests for siliconBrain documentation ..." );

   Memory memory;

   memoryInit( memory );

   String result;

   //------------------------------------------------------------------------------------------------
   puts( "testMain: testing documentation.specification ..." );
   //------------------------------------------------------------------------------------------------
   result = executeGet( &memory, (char *[]){ "temporary/documentation.specification", 0 } );
   testAssert( search( &c( "deployTargetHost" ), &result ) );
   testAssert( search( &c( "deployTargetWeb"  ), &result ) );

   //------------------------------------------------------------------------------------------------
   puts( "testMain: testing deploy command ..." );
   //------------------------------------------------------------------------------------------------
   #define testweb "temporary/testweb"

   String rm = which( &memory, c( "rm" ) );
   Command rmRf = command( rm, "-Rf", testweb, 0 );
   execute( rmRf, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO );

   mkdir( testweb, 0755 );

   char *pwd = get_current_dir_name();
   String deployTargetWeb = stringNull;
   collect_a( deployTargetWeb, c( "--deployTargetWeb=" ), string( pwd ), c( "/" ), string( testweb ) );

   result = executeGet( &memory, (char *[]){
      "distribution/programs/deploy",
      "--deployTargetHost=localhost",
      toCa( deployTargetWeb ),
      0
   } );

   testAssertExist( testweb "/index.htm"  );
   testAssertExist( testweb "/ranges.htm" );
   testAssertExist( testweb "/gpl.html"   );

   //------------------------------------------------------------------------------------------------
   puts( "testMain: testing formatedPrint ..." );
   //------------------------------------------------------------------------------------------------
   result = callFormater(
      &memory,
      &c(
         "hallo\n"
         "This text is inter@red{rupted}by a text attribute\n"
         "@comment this is a comment\n"
         "@c this is a comment too\n"
         "\n"
         "\n"
      ),
      "--format=plainText"
   );

   /*--------------*/
   /* check result */
   /*--------------*/
   testAssert(  search( &c( "hallo"       ), &result ) );
   testAssert(  search( &c( "interrupted" ), &result ) );
   testAssert( !search( &c( "joerg"       ), &result ) );
   testAssert( !search( &c( "comment"     ), &result ) );

   memoryDestroy( memory );

   return 0;
}

/*
$Log: testMain.main.c,v $
Revision 1.4  2006/05/02 23:09:36  joerg
published for new release 0.1.1

Revision 1.3  2006/04/26 23:19:14  joerg
ftp all htm and html files to the web site

Revision 1.2  2006/04/05 22:55:51  joerg
first do nothing version of deploy command

Revision 1.1  2006/04/04 22:27:44  joerg
first test; project specification


*/