/*
//
//   ADOBE SYSTEMS INCORPORATED
//   Copyright (C) 2000-2003 Adobe Systems Incorporated
//   All rights reserved.
//
//   NOTICE: Adobe permits you to use, modify, and distribute this file
//   in accordance with the terms of the Adobe license agreement
//   accompanying it. If you have received this file from a source other
//   than Adobe, then your use, modification, or distribution of it
//   requires the prior written permission of Adobe.
//
*/

#include "WatchFolder.h"
#include "TextExtractWorker.h"
#include "stdio.h"

#ifdef MAC_PLATFORM
#include "macUtils.h"
#endif

#ifdef UNIX_ENV
#include "stdlib.h"
#endif

/* From the Utils directory */
#include   "MyPDFLibUtils.h"
#include "SDKThreads.h"

int   main( int  argc,  char  *argv[],  char  *envp[]) 
{
                INIT_AUTO_POOL(autoReleasePool);                /* Required only on MAC platform */
               
                ThreadInfo  * myThreads =  NULL;
                ThreadArgs  * myThreadArgs =  NULL;
                const   char  * folderName =  NULL;
               
                ASInt32  numFiles = 10;
                ASInt32  numThreads = 3;
                if  (argc > 4) {
                               printf(  "Usage: %s folderPath [numfiles [numthreads]]\n" , argv[0] );
                               printf( "The folder path must be absolute\n" );
                                return  0;
               }
                if  (argc == 1)
                                /* For Mac, this works with command line launch of executable,
                                     not from app package */
                               folderName =  "../wFolder" ;
                else
                               folderName = argv[1];
               
                if  (argc > 2){
                                 numFiles = atoi(argv[2]);
                                  if  (numFiles == 0){
                                                               printf( "Zero files requested\n" );
                                                                return  0;
                                 }
               }
                if  (argc >3){
                               numThreads = atoi(argv[3]);
                                if  (numThreads == 0){
                                               printf( "No threads to work with\n" );
                                                return  0;
                               }
               }
               printf( "Watched folder defined as %s\n" ,folderName);
               printf( "Will extract the text of %d PDF files\n" ,numFiles);
               printf( "Will create %d threads\n" ,numThreads);
                MyPDFLInit();
               
                //create the ASPathName
#ifdef MAC_PLATFORM
                ASPathName  folderToWatch =  GetMacPath(folderName);
#else
                ASPathName  folderToWatch =  ASFileSysCreatePathName(NULL,ASAtomFromString( "Cstring" ),folderName,NULL);
#endif
                ASFileSysItemPropsRec  folderProps;
               folderProps.size  =  sizeof (ASFileSysItemPropsRec);
                ASErrorCode  err =  ASFileSysGetItemProps(NULL,folderToWatch,&folderProps);
                if  ((err != 0) || (folderProps.isThere  ==  false ) || (folderProps.type  !=  kASFileSysFolder)){
                               printf( "Watch Folder missing.\n" );
                                                return  -1;
               }
               
                // creating the watched folder object initialises the state of the application.
                // After the watched folder object is created, other threads can synchronise with it.
                WatchFolder  * myWF =  new   WatchFolder(folderToWatch, numFiles);
                ASFileSysReleasePath(NULL, folderToWatch);

                // Allocate the thread block
               myThreads =  static_cast< ThreadInfo  * > (ASmalloc(  sizeof (  ThreadInfo  ) * numThreads));

                // Allocate a ThreadArgs object for each thread
               myThreadArgs =  static_cast< ThreadArgs  * > (ASmalloc(  sizeof (  ThreadArgs  ) * numThreads));

                // Start the threads, passing their thread number to each
                for  (ASInt32  i = 0; i < numThreads; i++) {
                                char  *buff =  static_cast< char  * > (ASmalloc(40));
                                sprintf_safe(buff, 40,  "MTTextExtract thread %d" , i);
                               myThreadArgs[i].tName  = buff;

                               myThreadArgs[i].watchFolder  = myWF;

                                if  (!createThread(  GetWords, &myThreadArgs[i], myThreads[i] )){
                                               printf(  "Thread creation %d failed\n" , i );
                               }
               }

               myWF->watchFolder();
               fprintf(stderr, "Waiting for worker thread completion\n" );
                // Clean up the threads, after waiting for each to exit.
                for  (ASInt32  j = 0; j < numThreads; j++) {
                                waitThread(myThreads[j]);
                               printf( "Thread %s joined\n" ,myThreadArgs[j].tName);
                                destroyThread(myThreads[j]);
                                ASfree(myThreadArgs[j].tName);
               }
                ASfree(myThreads);
                ASfree(myThreadArgs);
                delete  myWF;
                MyPDFLTerm();
               
                RELEASE_AUTO_POOL(autoReleasePool);          /* Required only on MAC platform */
               
                return  0;
}