/*
**  Subsystem:   USENET Sources Archiver             
**  File Name:   article.c               
**                                                        
**  usage: article [ -adnvV ] [-f format ] newsarticle [ ... ]
**
**
** This software is Copyright (c) 1989, 1990, 1991 by Kent Landfield.
**
** Permission is hereby granted to copy, distribute or otherwise 
** use any part of this package as long as you do not try to make 
** money from it or pretend that you wrote it.  This copyright 
** notice must be maintained in any copy made.
**
** Use of this software constitutes acceptance for use in an AS IS 
** condition. There are NO warranties with regard to this software.  
** In no event shall the author be liable for any damages whatsoever 
** arising out of or in connection with the use or performance of this 
** software.  Any use of this software is at the user's own risk.
**
**  If you make modifications to this software that you feel 
**  increases it usefulness for the rest of the community, please 
**  email the changes, enhancements, bug fixes as well as any and 
**  all ideas to me. This software is going to be maintained and 
**  enhanced as deemed necessary by the community.
**		
**		Kent Landfield
**		uunet!sparky!kent
**		kent@sparky.imd.sterling.com
*/
char sccsid[] = "@(#)article.c	2.1 2/21/91";

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include "rkive.h"
#include "article.h"

#define ARCHIVE_ONLY  1
#define NO_ARCHIVE   -1

#define USAGE  "usage: %s [ -adnvV ] [-f format ] newsarticle [ ... ]\n"

char *progname;                 /* name of executable              */
char *format_ptr = NULL;        /* pointer to user supplied format */
int verify_archive = 0;         /* archive verification needed ?   */
struct group_archive *newsgrp;  /* newsgroup pointer for formating */
FILE *errfp;                    /* standard error file pointer     */
FILE *logfp;                    /* standard output file pointer    */

char *strchr();                 /* external function declaration   */
void article_header();          /* local function declaration      */

int main(argc, argv)
int argc;
char **argv;
{
   int getopt();
   int strlen();
   void version();

   int c;
   extern char *optarg;
   extern int optind;
   extern int opterr;

   opterr = 0;
   progname = argv[0];
   debug = verbose = 0;
   logfp = stdout;
   errfp = stderr;

   if (argc > 1) {
      while ((c = getopt(argc, argv, "adnvVf:")) != EOF) {
         switch (c) {
             case 'a':
                 verify_archive = ARCHIVE_ONLY;
                 break;
             case 'd':
                 debug++;
                 break;
             case 'f':
                 format_ptr = optarg;
                 break;
             case 'n':
                 verify_archive = NO_ARCHIVE;
                 break;
             case 'v':
                 verbose++;
                 break;
             case 'V':
                 version();
                 break;
             default:
                 (void) fprintf(errfp,USAGE, progname);
                 return(1);
         }
      }
   }

   if ((optind >= argc) || (argc == 1)) {  /* file from stdin */
        char buf[BUFSIZ];
        while (fgets(buf,sizeof buf,stdin) != NULL) {
               buf[strlen(buf) -1] = '\0';
               article_header(buf);
        }
   }
   else { 
        for (; optind < argc; optind++)    /* process files to print */
             article_header(argv[optind]);
   }
   return(0);                              /* terminate this process */
}

void article_header(flname)
    char *flname;
{
    int zfclose();
    char *strcpy();
    FILE *zfopen();
    void init_article();
    void store_line();
    void dump_article();
    char *format_output();

    char *dp;
    char *qp;
    FILE *file;
    int header_ok = 0;
    int last = TEXT;

    int default_format = ARTICLE;

    init_article();

    file = zfopen(flname,"r");

    (void) strcpy(article.newsarticle,flname);

    while (fgets(s,sizeof s,file) != NULL) {
        /*
        ** Check if the line is not a header line.
        ** Allow 2 lines to go by that are not header
        ** lines. In this manner, the auxilliary sources
        ** headers can be interpreted.
        */
        if (!isalpha(*s) || (strchr(s,':') == NULL)) {
           ++header_ok;
           if (header_ok >= 2) {
               /*
               ** If the second line is another blank line,
               ** keep searching. It may just be that the moderator
               ** placed and extra blank line in the article by mistake.
               ** Thanks to /r$ for the unique way of submitting bug
               ** reports.. :-) :-)
               */
               if (*s == '\n' && last == BLANK)
                   continue;
               /* 
               ** Has the user requested to produce 
               ** only an archive listing ?
               */
               if (verify_archive == ARCHIVE_ONLY) {
                  if (!header.archive_name[0] || !article.description[0])
                       break;
                   default_format = ARCHIVE;
               }
               /* 
               ** Has the user requested to produce 
               ** a listing excluding all archive 
               ** potential members ?
               */
               else if (verify_archive == NO_ARCHIVE) {
                   if (header.archive_name[0] && article.description[0]) 
                       break;
                   default_format = ARTICLE;
               }
               /* 
               ** Print out the information as requested
               */
               qp = format_output(format_ptr, flname, default_format);
               (void) fprintf(logfp, "%s\n", qp);

               break;
            }
            if (*s == '\n')
                last = BLANK;
            else
                last = TEXT;
            continue;
        }

        last = TEXT;
        dp = s;
        while (*++dp)
           if (*dp == '\n')
               *dp = '\0';
        /*
        ** Determine the type of the line and then
        ** store the line in its appropriate structure
	** element for later retrieval.
        */
        store_line();
    }
    (void) zfclose(file);

    if (verbose)
        dump_article();
}
