/*    bcnumsg.c,v 1.6 1999/04/01 13:05:47 jap Exp

 *    Copyright (c) 1998 John Phillips
 *
 *    You may distribute under the terms of the Artistic License
 *
 */

#include "bcnu.h"

char *id = "bcnumsg";
char *subver = "1.6";

char progname[80];		/* our name */

long timevar;			/* contains time returned by time() */
char *ctime ();			/* declare time formatting routine */

int dosyslog = 0;               /* don't log to syslog */

int s;
char buf[BCNU_MESS + 1];
char infile[80], otfile[80], filepos[12], pattern[20], posname[80];

char usage[] = "\n\
        -p string	the pattern(string) to match\n\
	-s		skip updating the .pos file\n\
	-n		name of the .pos file \n\
        -d		Switch on debug flag\n\
	-v		show version info\n\
        filename 	the name of the input file";


main (argc, argv)
     int argc;
     char *argv[];
{

  int c;
  extern char *optarg;
  extern int optind;
  int errflg = 0;
  int debug = 0;
  int search = 0;
  int ct = 0;
  int skip = 0;
  int pos = 0;
  FILE *in, *ot;
  off_t seekpos, lastseek;
  char *ptr;

  char *bcnuhome;

  if (argc < 2)
    errflg++;

  strcpy (infile, argv[argc - 1]);	/* get filename */

  while ((c = getopt (argc, argv, "vdp:sn:")) != EOF)
    switch (c)
      {
      case 'd':		/* debug flag */
	debug++;
	break;
      case 's':		/* debug flag */
	skip++;
	break;
      case 'p':		/* pattern */
	strcpy (pattern, optarg);
	search++;
	break;
      case 'n':		/* name the pos file */
	strcpy (posname, optarg);
	pos++;
	break;
      case 'v':		/* Version info */
	printf ("%s version %s.%s\n", id, BCNU_VER, subver);
	errflg++;
      case '?':
	errflg++;
      }
  if (errflg)
    {
      fprintf (stderr, "usage: %s -p string -s -d -v filename %s\n", argv[0], usage);
      exit (2);
    }
/*
   set up program name
 */

  strcpy (progname, argv[0]);

  bcnu_init ();
  if (pos)
    sprintf (otfile, "%s%s.pos", bcnuflags, posname);

  else
    {
      if ((ptr = strrchr (infile, '/')) == NULL)
	ptr = infile;
      else
	ptr++;

      sprintf (otfile, "%s%s.pos", bcnuflags, ptr);
    }
  if ((in = fopen (infile, "r")) == NULL)
    bcnu_error ("Unable to open input file");
  if ((ot = fopen (otfile, "r")) == NULL)
    lastseek = 0;
  else
    {
      fgets (filepos, 12, ot);
      lastseek = atol (filepos);
      fclose (ot);
    }

  fseek (in, 0L, SEEK_END);
  seekpos = ftell (in);
  if (seekpos < lastseek)
    {
      rewind (in);
      lastseek = ftell (in);
    }
  else if (seekpos == lastseek)
    exit (1);

  fseek (in, lastseek, SEEK_SET);
  while (fgets (buf, BCNU_MESS, in))
    {
      if (search)
	{
	  if ((strstr (buf, pattern)) != 0)
	    {
	      fputs (buf, stdout);
	      ct++;
	    }
	}
      else
	fputs (buf, stdout);
    }
  fseek (in, 0L, SEEK_END);
  seekpos = ftell (in);
  if (!skip)
    {
      if ((ot = fopen (otfile, "w")) == NULL)
	bcnu_error ("Unable to open output file");
      fprintf (ot, "%d\n", seekpos);
      fclose (ot);
    }
  fclose (in);
  if (ct)
    exit (0);
  else
    exit (1);

}
