/* orysplit.c:
 *   Split "rec.humor.oracle" archive files into single news articles again,
 *   also cuts down these pesky dashed lines to at most 75 characters.
 *   A filename like "001-005" is supposed to contain articles 1 through 5,
 *   each starting with either a "Path: " or "From " line.
 *   When done, it prints to stdout a number which it thinks is now the
 *   highest article number in the directory, plus 1. (Go figure what that
 *   might be useful for. ;)
 * Cookie mode:
 *   When you specify "-c", oracularities are copied to stdout, with lines 
 *   of "%%" separating them (standard fortune cookie format).
 */                          
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#define LINELEN 100

void help()
{
  printf( "usage:\n  orysplit [ options ] <filename> \n" );
  printf( "where <filename> must start with a digit,\n" );
  printf( "and options are:\n" );
  printf( "  -c    create cookies (written to stdout)\n" );
}


int main( int argc, char* argv[] )
{
  FILE *arcf, *artf;
  int art, question = 1;
  int cookiemode = 0, bodytext, legacy;
  char line[ LINELEN ], fname[ 20 ];
  char *s, *inpname = NULL;

  while( --argc )
    {
      argv++;
      if( isdigit( *argv[0] ) )
        inpname = argv[0];
      else if( strcmp( argv[0], "-c" ) == 0 )
        cookiemode = 1;
      else
        {
          help();
          return 10;
        }
    }
  if( inpname == NULL )
    {
      help();
      return 10;
    }
  /* art = atoi( inpname ); */
  art = strtol( inpname, NULL, 10 );
  arcf = fopen( inpname, "r" );
  if( !arcf )
    {
      fprintf( stderr, "Can't open %s\n", inpname );
      return 10;
    }
  artf = NULL;
  bodytext = 0;
  while( fgets( line, LINELEN, arcf ) != NULL )
    {
      if( strncmp( line, "Path: ", 6 ) == 0
       || strncmp( line, "From ", 5 ) == 0 )
        {
          fprintf( stderr, "Article %d\n", art );
          sprintf( fname, "%d", art );
          if( artf )
            fclose( artf );
          if( !cookiemode )
            {
              artf = fopen( fname, "w" );
              if( !artf )
                {
                  fprintf( stderr, "Can't open %s for output\n", fname );
                  return 10;
                }
            }
          art++;
          question = 1;
        }
      if( cookiemode )
        {
          if( (*line == '>' && bodytext == 0)
           || (strncmp( line, "The ", 4 ) == 0
            && strstr( line, "has pondered" ) != NULL) )
            {                                   /* start of an oracularity */
              bodytext = 1;
              legacy = ( strncmp( line+4, "Internet", 8 ) != 0 );
              printf( "%s Oracularity #%03d-%02d\n\n", 
                legacy ? "Usenet" : "Internet", art-1, question );
              question++;
            }
          if( strncmp( line, "And in response", 15 ) == 0 )
            bodytext = 2;                       /* the Oracle's answer */
          if( *line == '}' && bodytext == 2 )
            bodytext = 3;
          if( *line != '}' && bodytext == 3 )
            {                                   /* end of answer */
              bodytext = 0;                     /* -> end of oracularity */
              printf( "%%%%\n" );
            }
          if( bodytext )
            printf( "%s", line );
        }
      else if( artf )
        {
          if( strspn( line, "-=" ) > 75 )        /* line of dashes */
            {
              line[75] = '\n';
              line[76] = '\0';
            }
          fprintf( artf, "%s", line );
        }
      else
        {
          if( (s = strchr( line, '\n' )) )
            *s = '\0';
          fprintf( stderr, "Had to drop a line: %.55s\n", line );
        }
    }
  fclose( arcf );
  if( artf )
    fclose( artf );
  if( bodytext )
    printf( "%%%%\n" );
  if( !cookiemode )
    {                           /* propose a contents for the ".next" file */
      artf = NULL;
      do
        {
          sprintf( fname, "%d", art );
          if( artf )
            fclose( artf );
          artf = fopen( fname, "r" );
          art++;
        }
      while( artf );
      printf( "%d\n", art-1 );
    }
  return 0;                  
}

