/* $Id: kitlists.c,v 1.1 1993/06/16 14:24:50 jik Exp jik $
 *
 * $Log: kitlists.c,v $
 * Revision 1.1  1993/06/16  14:24:50  jik
 * Initial revision
 *
 *
 * 
 */
/* This software is Copyright 1991 by Stan Barber. 
 *
 * Permission is hereby granted to copy, reproduce, redistribute or otherwise
 * use this software as long as: there is no monetary profit gained
 * specifically from the use or reproduction of this software, it is not
 * sold, rented, traded or otherwise marketed, and this copyright notice is
 * included prominently in any copy made. 
 *
 * The author make no claims as to the fitness or correctness of this software
 * for any use whatsoever, and it is provided as is. Any use of this software
 * is at the user's own risk. 
 */

#include <stdio.h>
#ifdef USG
#define index strchr
#endif
#define MAXKIT 100
#define MAXKITSIZE 64000
#define KITOVERHEAD 700
#define FILEOVERHEAD 80

long tot[MAXKIT];
FILE *outfp[MAXKIT];		/* of course, not this many file descriptors */

main(argc,argv)
int argc;
char **argv;
{
    FILE *inp, *popen();
    char buf[1024], filnam[128];
    char *index();
    register char *s;
    register int i, newtot;
#ifdef USG    
    sprintf(buf,"ls -l `awk '{print $1}' <%s` | awk '{print $9 \" \" $5}' | sort -nr +1", argc > 1 ? argv[1] : "MANIFEST.new");
#else
    sprintf(buf,"ls -l `awk '{print $1}' <%s` | awk '{print $8 \" \" $4}' | sort +1nr", argc > 1 ? argv[1] : "MANIFEST.new");
#endif

    inp = popen(buf,"r");

    while (fgets(buf,1024,inp) != (char *)NULL) {
	s = index(buf,' ');
	*s++ = '\0';
	for (i=1;
	  (newtot = tot[i] + atol(s) + FILEOVERHEAD) > MAXKITSIZE-KITOVERHEAD;
	  i++) 
	    ;
	if (!tot[i]) {
	    sprintf(filnam,"kit%d.list",i);
	    outfp[i] = fopen(filnam,"w");
	}
	tot[i] = newtot;
	printf("Adding %s to kit %d giving %d bytes\n",buf,i,newtot);
	fprintf(outfp[i],"%s\n",buf);
    }
}
