/******************************************************************************
 *  bwm-ng parsing stuff                                                      *
 *                                                                            *
 *  Copyright (C) 2004 Volker Gropp (vgropp@pefra.de)                         *
 *                                                                            *
 *  for more info read README.                                                *
 *                                                                            *
 *  This program is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU General Public License as published by      *
 *  the Free Software Foundation; either version 2 of the License, or         *
 *  (at your option) any later version.                                       *
 *                                                                            *
 *  This program is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 *  GNU General Public License for more details.                              *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with this program; if not, write to the Free Software               *
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
 *                                                                            *
 *****************************************************************************/


#include "bwm-ng.h"


int if_count=0;
char PROC_NET_DEV[PATH_MAX];
unsigned int delay=500;
char show_kb=1;
char netstat_i=0;
char show_all_if=0;
char show_packets=0;
char output_method=CURSES_OUT;
char *iface_list=NULL;
char csv_char=';';
char csv_count=-1;
char daemonize=0;
int html_refresh=5;
FILE *csv_file=NULL;

/* prints a helpscreen and exists */
int printhelp() {
    print_version;
    printf("USAGE: bwm-ng [OPTION] ...\n");
    printf("displays current ethernet interfaces load in KB/s or Packets/s\n\n");
    printf("Options:\n");
    printf("  -t, --timeout <msec>    displays stats every <msec> (1msec = 1/1000sec) default: 500\n");
    printf("  -f, --file <filename>   filename to read raw data from. default: /proc/net/dev\n");
    printf("  -d, --dynamic           show values dynamicly (Byte KB or MB)\n");
    printf("  -a, --allif             show also down interfaces\n");
    printf("  -p, --packets           show packets/s instead of KB/s\n");
    printf("  -n, --netstat           always use netstat -i as input stream\n");
    printf("  -i, --interfaces <list> show only interfaces in <list> (comma seperated)\n");
    printf("  -o, --output <method>   output method, on of: curses, plain, csv, html\n");
    printf("      --csvchar <char>    delimiter for csv\n");
	printf("      --csvfile <file>    output file for csv (default stdout)\n");
	printf("      --htmlrefresh <num> meta refresh for html output\n");
    printf("      --count <num>       number of query/output for csv (ie 1 for one single output)\n");
	printf("      --daemon            fork into background and daemonize\n");
    printf("  -h, --help              displays this help\n");
    printf("  -V, --version           print version info\n");
    printf("\n");
    exit(0);
}


void get_cmdln_options(int argc, char *argv[]) {
	char o;
    /* get command line arguments, kinda ugly, wanna rewrite it? */
    while (1) {
        int option_index = 0;
        static struct option long_options[] = {
            {"timeout", 1, 0, 0},
            {"file",1,0,0},
            {"dynamic",0,0,0},
            {"netstat",0,0,0},
            {"help", 0, 0, 0},
            {"version",0,0,0},
            {"allif",0,0,0},
            {"packets",0,0,0},
            {"interfaces",1,0,0},
            {"output",1,0,0},
            {"csvchar",1,0,0},
			{"csvfile",1,0,0},
            {"count",1,0,0},
			{"daemon",0,0,0},
			{"htmlrefresh",1,0,0},
            {0,0,0,0}
        };
        o=getopt_long (argc,argv,"ht:f:dnVapi:o:",long_options, &option_index);
        if (o==-1) break;
        switch (o) {
            case '?': exit(0);
                      break;
            case 0: if (!strcasecmp(long_options[option_index].name,"help")) printhelp();
                        else if (!strcasecmp(long_options[option_index].name,"timeout")) {
                            if ((optarg) && atol(optarg)>0) { delay=atol(optarg); }
                        } else if (!strcasecmp(long_options[option_index].name,"file")) {
                            if (optarg && (strlen(optarg)<PATH_MAX)) strcpy(PROC_NET_DEV,optarg);
                        } else if (!strcasecmp(long_options[option_index].name,"interfaces")) {
                            if (optarg) iface_list=strdup(optarg);
                        } else if (!strcasecmp(long_options[option_index].name,"output")) {
                            if (optarg) {
                                if (!strcasecmp(optarg,"curses")) output_method=CURSES_OUT;
                                    else
                                if (!strcasecmp(optarg,"plain")) output_method=PLAIN_OUT;
                                    else
                                if (!strcasecmp(optarg,"csv")) output_method=CSV_OUT;
									else
								if (!strcasecmp(optarg,"html")) output_method=HTML_OUT;
                            }
                        } else if (!strcasecmp(long_options[option_index].name,"csvchar")) {
                            if (optarg) csv_char=optarg[0];
						} else if (!strcasecmp(long_options[option_index].name,"csvfile")) {
							if (optarg) { csv_file=fopen(optarg,"a"); }
                        } else if (!strcasecmp(long_options[option_index].name,"count")) {
                            if (optarg) csv_count=atol(optarg);
                        } else if (!strcasecmp(long_options[option_index].name,"dynamic")) {
                            show_kb=0;
                        } else if (!strcasecmp(long_options[option_index].name,"allif")) {
                            show_all_if=1;
                        } else if (!strcasecmp(long_options[option_index].name,"packets")) {
                            show_packets=1;
                        } else if (!strcasecmp(long_options[option_index].name,"netstat")) {
                            netstat_i=1;
						} else if (!strcasecmp(long_options[option_index].name,"daemon")) {
							daemonize=1;
						} else if (!strcasecmp(long_options[option_index].name,"htmlrefresh")) {
							if ((optarg) && atol(optarg)>0) { html_refresh=atol(optarg); }
                        } else if (!strcasecmp(long_options[option_index].name,"version")) {
                            print_version;
                            exit(0);
                        }
                    break;
            case 'h':
                printhelp();
                break;
            case 'f':
                if (optarg && (strlen(optarg)<PATH_MAX)) strcpy(PROC_NET_DEV,optarg);
                break;
            case 'i':
                if (optarg) iface_list=strdup(optarg);
                break;
            case 'o':
                if (optarg) {
                    if (!strcasecmp(optarg,"curses")) output_method=CURSES_OUT;
                        else
                    if (!strcasecmp(optarg,"plain")) output_method=PLAIN_OUT;
                        else
                    if (!strcasecmp(optarg,"csv")) output_method=CSV_OUT;
						else
					if (!strcasecmp(optarg,"html")) output_method=HTML_OUT;
                }
                break;
            case 'a':
                show_all_if=1;
                break;
            case 'p':
                show_packets=1;
                break;
            case 't':
                if ((optarg) && atol(optarg)>0) { delay=atol(optarg); }
                break;
            case 'd':
                show_kb=0;
                break;
            case 'n':
                netstat_i=1;
                break;
            case 'V':
                print_version;
                exit(0);
                break;
        }
    }
}
