/*
 * Beans WWW Statistic Reporter
 * 
 * Tom Coppeto
 * MIT Network Group
 * 25 December 1997
 * 
 * $Source: $
 * $Author: $
 * $Locker: $
 */

#ifndef lint
static char rcsid[] = "$Header: $";
#endif /* lint */

#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/stat.h>

#include <beans.h>
#include <report.h>

char *prog;

int main(int argc, char **argv) {
	char *email = (char *) NULL;
	char *url   = (char *) NULL;
	char *db = DB;
	int all = 0;
	int send = 0;
	int sock;
	int i;

	prog = argv[0];
	openlog(prog, LOG_PID, LOG_LOCAL2);
	while(*++argv) {
		if(!strcmp(*argv, "-db")) {
			if(!*++argv) {
				usage(prog);
				exit(1);
			}
			db = *argv;
			continue;
		}

		if(!strcmp(*argv, "-email")) {
			if(!*++argv) {
				usage(prog);
				exit(1);
			}
			email = *argv;
			continue;
		}	

		if(!strcmp(*argv, "-url")) {
			if(!*++argv) {
				usage(prog);
				exit(1);
			}
			url = *argv;
			continue;
		}	

		if(!strcmp(*argv, "all")) {
			all = 1;
			continue;
		}	

		if(!strcmp(*argv, "-send")) {
			send = 1;
			continue;
		}	

		if(!strcmp(*argv, "-h")) {
			usage(prog);
			exit(0);
		}			
		logmsg(LOG_ERR, "", "bad usage");
		usage(prog);
		exit(1);
	}
	
	if(!email && !url && !all) {
		logmsg(LOG_ERR, "", "bad usage");
		usage(prog);
		exit(1);
	}

	if(!(sock = db_open(db))) {
                logmsg(LOG_ERR, "", "unable to open db %s", db);
                exit(1);
        }

	if(url) {
		report_by_url(sock, url, send ? email : "");
	}
	exit(0);
}




int report_by_url(int db, char *url, char *email) {
	struct hit *h, *hp;
	struct hit start, end;
	char buf[BUFSIZ];
	char *c, *d;
	int short_format = 1;

	memset(&start, 0, sizeof(start));
	memset(&end, 0, sizeof(end));

	c = url;
	if(strncasecmp(c, "http://", 7) == 0)
		c = c += 7;
	if(!(d = strchr(c, '/')))
		return(BADNESS);
	
	sprintf(start.path, "%s", d);
	strncpy(start.host, c, d - c);
	start.host[d - c] = '\0';

	start.year = 1999;
	end.year=1999;
	start.month=1;
	end.month=12;

	if(!strchr(url, '%')) {
		strcpy(start.country, "%");
		short_format = 0;
	}

	if(!(h = db_hitback(db, &start, &end, ""))) {
		logmsg(LOG_ERR, "", "no data available");
	}
	
	hp = h;
	while(hp) {
		if(short_format) {
			sprintf(buf, "%8d (%d%%)", 
				hp->hits, hp->lhits*100/hp->hits);
			fprintf(stdout, "%02d/%d  %-20s   %s %s\n", 
				hp->month, hp->year, buf, hp->host, hp->path);
		} else {
			sprintf(buf, "%8d (%d%%)", 
				hp->hits, hp->lhits*100/hp->hits);
			fprintf(stdout, "%02d/%d  %-20s   %s %s\n", 
				hp->month, hp->year, buf, hp->country, 
				hp->country_name);
		}
		hp = hp->next;
	}
	freeHit(h);
}




void usage(char *prog) {
	fprintf(stderr, "usage: %s [-url <url>] [-email <address>] [-send] [all]\n",
		prog);
	return;
}





/*
 * Local Variables:
 * c-indent-level: 8
 * c-argdecl-indent: 8
 * c-continued-statement-offset: 8
 * c-continued-brace-offset: -8
 * c-label-offset: -8
 * c-brace-offset: -8
 * End:
 */
 
