
static char acRcs[] =
    "$Header: mdist.c,v 1.3 94/07/29 15:33:38 k2mm Exp $";

#include <stdio.h>

#define BIG		1000

typedef char*		TEXT;

typedef struct w *	pW;
typedef struct w
{   char		acCall[ BIG];
    char		acFrom[ BIG];
}   W;

TEXT			tRef, tDest;
pW			pw, pwEnd;
W			w, aw[ BIG];

main( nArg, atArg)
int			nArg;
TEXT			atArg[];
{   FILE		*pf;
    char		ac[ BIG];
    char		acLine[ BIG];
    char		acFrom[ BIG];
    int			iGotFrom;

    if( nArg == 3)
	fprintf( stderr, "mdist: ref=%s dest=%s\n",
	    tRef = atArg[ 1], tDest = atArg[ 2]);
    else
    {   fprintf( stderr, "usage: mdist ref-file dest-dir\n");
	exit( -1);
    }
    pw = aw;
    if( (pf = fopen( tRef, "r")) == 0)
    {   fprintf( stderr, "can't open ref=%s\n", tRef);
	exit( 1);
    }
    while( fgets( acLine, BIG, pf))
    {   sscanf( acLine, "%s %s", pw->acCall, pw->acFrom);
	pw++;
    }
    fprintf( stderr, "ref: %d calls\n", (pwEnd = pw) - aw);
    fclose( pf);
    pf = 0;
    while( fgets( acLine, BIG, stdin))
    {   if( strncmp( acLine,  "From ", 5) == 0 &&
	    sscanf( acLine, "%*s %s", acFrom) == 1)
	{   if( pf)
		fclose( pf);
	    pf = 0;
	    LowerCase( acFrom);
	    for( pw = aw; pw < pwEnd; pw++)
		if( strcmp( pw->acFrom, acFrom) == 0 ||
		    strcmp( pw->acCall, acFrom) == 0)
			break;
	    if( pw < pwEnd)
	    {   sprintf( ac, "%s/%s", tDest, pw->acCall);
		pf = fopen( ac, "a");
		fprintf( stderr, "%s %s\n", pf ? "ok" : "NG", ac);
	    }
	    else
		fprintf( stderr, "UNK %s\n", acFrom);
	}
	if( pf)
	    fputs( acLine, pf);
    }
}

LowerCase( pc)
char			*pc;
{
    while( *pc)
    {   if( *pc >= 'A' && *pc <= 'Z')
	    *pc += 'a' - 'A';
	pc++;
    }
}


