/*****************************************************************************

 (C) Copyright 1989 by Sneakers Computing             All Rights Reserved
                       2455 McKinley Ave.
                       West Lawn, PA 19609
                       (215) 678-8984

 Program name: CUnBatcher.c

 Version:      Beta Version 0.10

 Created:      02/11/1989

 Programmer:   Dan "Sneakers" Schein

 Description:  Read a compressed/batched file, remove the header, uncompress
               the file, and proccess the file via the correct unbatcher.

 Modification History:

     Date          Description
  ----------    -------------------------------------------------------------


 Known problem's or bugs:

     Date          Description
  ----------    -------------------------------------------------------------


*****************************************************************************/

# include <stdio.h>
# include "config.h"

# define PROG_NAME "cunbatcher"


main(argc, argv)
int  argc;
char argv[];
{
   FILE *fp, *ef;
   char buf[128];
   char cmd[50];
   int ch, rc=0;

   if (!(ef = fopen(ERRORFILE,"a"))) {
      printf("Can not open ERROR file %s\n", ERRORFILE);
      return(-1);
   }

   fp = fopen("uucp:mail/UseNet.Z","w");
   if (fp == (char *)NULL) {
      fprintf(ef, "%s: Can't open temp workfile!\n",PROG_NAME);
      fclose(ef);
      return(-1);
   }

   fgets(buf, 13, stdin);
   if (strncmp(buf,"#! cunbatch", 11)) {
      fprintf(ef, "%s:News is not in compressed batch format.\n",PROG_NAME);
      fclose(ef);
      fclose(fp);
      return(-1);
   }

   while ((ch = getchar()) != EOF) {
      fputc(ch,fp);
   }

   fclose(fp);
   fclose(ef);

   rc = system("uucp:c/compress -d uucp:mail/UseNet.Z");

   if (rc == 0) {
      remove("uucp:mail/UseNet.Z");
      strcpy (cmd, "uucp:c/");
      sprintf(cmd, BATCH_PROG);
      rc = system(cmd);
      return(0);
   }
   else
      return(-1);
}
