/*
 * W-NEWS       A simple NEWS processing package.
 *              Rnews is used by UUCP to unpack the incoming news batches
 *              and queue them for unbatching by the 'unbatch' program.
 *              Since all this unpacking (decompressing) takes lots of
 *              time, the 'unbatch' program also takes care of the actual
 *              decompressing and unpacking.  This file moves the data file
 *              into a spool directory of the W-NEWS system.
 *
 * Usage:       rnews [-d] [-U] [ < ] input_batch_file
 *
 * Version:     @(#)rnews.c	4.40	24-Nov-1993
 *		Rnews is not used anymore starting at Wnews 4.43. It's
 *		functionality has been moved into inews.
 *
 * Authors:	Fred N. van Kempen, <waltje@infomagic.com>
 *		Edvard Tuinder, <ed@ow.org>
 *		Copyright 1988-1993 MicroWalt Corporation
 *
 *		This program 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.
 */
#include "wnews.h"


char *Release = VERSION;
char *Version = "@(#) rnews 4.40 (24-Nov-1993)";


char *program;				/* for use in logmsg()		*/
int unbatch = 0;                        /* call 'unbatch' for the work  */


_PRO( int mk_batch, (FILE *input)					);


int
mk_batch(input)
register FILE *input;
{
  char batch[128], buff[1024];
  register FILE *fp;
  struct tm *tm;
  time_t now;
  int st;

  /* Get the current time. */
  (void) time(&now);
  tm = localtime(&now);

  /* Create a batch file name. */
  sprintf(batch, "%s/%s/%2d%02d%02d%02d%02d%04x",
	SPOOLDIR, INCOMING,
	tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
	tm->tm_hour, tm->tm_min, getpid());
  if ((fp = fopen(batch, "wb")) == (FILE *)NULL) {
	logmsg(LOGERR|LOGSYS, "cannot create(%s)\n", batch);
	return(ERROR);
  } else logmsg(LOGINFO, "creating batch %s\n", batch);

  /* Copy the incoming batch to the file. */
  do {
	st = fread(buff, sizeof(char), 1024, input);
	fwrite(buff, sizeof(char), st, fp);
  } while (st > 0);
  if (ferror(input) || ferror(fp)) {
	logmsg(LOGERR, "error while copying batch file\n");
	(void) fclose(input);
	(void) fclose(fp);
	return(ERROR);
  }
  (void) fclose(input);
  (void) fclose(fp);
  return(OK);
}


void
usage()
{
  fputs("Usage: rnews [-d] [-U] < input_batch_file\n", stderr);
  exit(ERROR);
}


int
main(argc, argv)
int argc;
char *argv[];
{
  char cmd[128];
  char *fname;
  FILE *fp;
  int st;

  program = *argv;
#ifndef COHERENT
  opterr = 0;
#endif
  while ((st = getopt(argc, argv, "dU")) != EOF) switch(st) {
	case 'd':
		debug = 1;
		break;

	case 'U':
		unbatch = 1;
		break;

	default:
		usage();
  }

  (void) umask(077);            /* set protection mask to rwx------ */

  if (unbatch == 1) {
	if (argc != optind) usage();
	sprintf(cmd, "%s/unbatch%s", LIBDIR, debug ? " -d" : "");
	exit(wsystem(cmd));
  } else {
	argc--;
 	fp = stdin;
	if (optind == argc) {
		fname = argv[optind];
		fp = fopen(fname, "rb");
		if (fp == (FILE *)NULL) {
			logmsg(LOGERR, "cannot open %s\n", fname);
			exit(-1);
		}
	} else if (optind <= argc) usage();

	st = mk_batch(fp);
	exit(st);
  }
}
