#include <stdio.h>
#include <unistd.h>
#include <x_types.h>
#include <utils.h>
#include <fileutil.h>
#include <packer.h>
#include <backup.h>


void
usage(UChar * prognam)
{
  fprintf(stderr, "Usage: %s <path>\n", FN_BASENAME(prognam));

  exit(2);
}


main(int argc, char ** argv)
{
  UChar		*line = NULL, *oldline, *c2, *c3, *pat;
  Int32		len, clen, llen, nall = 0, oldall;

  if(argc > 2)
    usage(argv[0]);

  pat = argc > 1 ? argv[1] : "";
  if(pat[0] && strcmp(pat, "/"))
    pat = strapp(pat, "/");

  len = strlen(pat);

  clen = strlen(CMDINOUTPREFIX);

  oldline = NEWP(UChar, oldall = 1024);
  strcpy(oldline, "");

  while(!feof(stdin)){
    if(fget_realloc_str(stdin, &line, &nall))
	continue;
    chop(line);

    if(!strncmp(line, CMDINOUTPREFIX, clen)){
	fprintf(stdout, "%s\n", line);
	fflush(stdout);
	continue;
    }

    if(!strncmp(pat, line, len)){
	c2 = line + len;
#if 0
	if(*c2 != FN_DIRSEPCHR)
	  continue;
#endif

	c3 = strchr(c2 + 1, FN_DIRSEPCHR);
	if(c3){
	  c2 = c3;
	  c3 = strchr(c2 + 1, FN_DIRSEPCHR);
	  if(c3)
	    *c3 = '\0';
	}

	if(!strcmp(line, oldline))
	  continue;

	fprintf(stdout, "%s\n", line);
	fflush(stdout);

	llen = strlen(line) + 1;
	if(llen > oldall)
	  oldline = RENEWP(oldline, UChar, oldall = llen);

	strcpy(oldline, line);
    }
  }

  exit(0);
}
