/******************************************************************************
 *  bwm-ng output                                                             *
 *                                                                            *
 *  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"

/* for csv output and time() */
#include <time.h>

extern char show_kb;
extern char netstat_i;
extern char show_all_if;
extern char show_packets;
extern char output_method;
extern char *iface_list;
extern char csv_char;
extern FILE *csv_file;


/* convert doube bytes/s value to some nice string */
char *convert_bytes(double bytes,char * buffer, int buf_size) {
    if (bytes<1024 && !show_kb)
        snprintf(buffer,buf_size,"%12.2f  B/s",bytes);
    else if (bytes<1048576 || show_kb)
        snprintf(buffer,buf_size,"%12.2f KB/s",bytes/1024);
    else snprintf(buffer,buf_size,"%12.2f MB/s",bytes/1048576);
    return buffer;
}



/* do the actual output */
void print_values(int y,int x,char *if_name,t_iface_stats new_stats,t_iface_stats stats,float multiplier) {
	FILE *out_file;
    switch (output_method) {
        case CURSES_OUT:
            mvwprintw(stdscr,y,x,"%12s:",if_name); /* output the name */
            if (netstat_i || show_packets) {
                /* show packets/s if asked for or netstat input */
                unsigned long long packets_out=new_stats.p_send-stats.p_send > 0 ? new_stats.p_send-stats.p_send : 0;
                unsigned long long packets_in=new_stats.p_rec-stats.p_rec > 0 ? new_stats.p_rec-stats.p_rec : 0;
                wprintw(stdscr,"%13.2f P/s ",(double)packets_out*multiplier);
                wprintw(stdscr,"%13.2f P/s ",(double)packets_in*multiplier);
                wprintw(stdscr,"%13.2f P/s ",(double)(packets_out+packets_in)*multiplier);
            } else {
                char bytes_buf[20];
                unsigned long long bytess=new_stats.send-stats.send > 0 ? new_stats.send-stats.send : 0;
                unsigned long long bytesr=new_stats.rec-stats.rec > 0 ? new_stats.rec-stats.rec : 0;
                /* output Bytes/s */
                wprintw(stdscr,"%s ",convert_bytes((double)(bytess*multiplier),bytes_buf,20));
                wprintw(stdscr,"%s ",convert_bytes((double)(bytesr*multiplier),bytes_buf,20));
                /* print total (send+rec) of current iface */
                wprintw(stdscr,"%s",convert_bytes((double)((bytess+bytesr)*multiplier),bytes_buf,20));
            }
            break;
        case PLAIN_OUT:
            printf("%12s:",if_name); /* output the name */
            if (netstat_i || show_packets) {
                /* show packets/s if asked for or netstat input */
                unsigned long long packets_out=new_stats.p_send-stats.p_send > 0 ? new_stats.p_send-stats.p_send : 0;
                unsigned long long packets_in=new_stats.p_rec-stats.p_rec > 0 ? new_stats.p_rec-stats.p_rec : 0;
                printf("%13.2f P/s ",(double)packets_out*multiplier);
                printf("%13.2f P/s ",(double)packets_in*multiplier);
                printf("%13.2f P/s\n",(double)(packets_out+packets_in)*multiplier);
            } else {
                char bytes_buf[20];
                unsigned long long bytess=new_stats.send-stats.send > 0 ? new_stats.send-stats.send : 0;
                unsigned long long bytesr=new_stats.rec-stats.rec > 0 ? new_stats.rec-stats.rec : 0;
                /* output Bytes/s */
                printf("%s ",convert_bytes((double)(bytess*multiplier),bytes_buf,20));
                printf("%s ",convert_bytes((double)(bytesr*multiplier),bytes_buf,20));
                /* print total (send+rec) of current iface */
                printf("%s\n",convert_bytes((double)((bytess+bytesr)*multiplier),bytes_buf,20));
            }
            break;
		case HTML_OUT:
			printf("<tr><td class='name'>%12s:</td>",if_name);
			if (netstat_i || show_packets) {
				/* show packets/s if asked for or netstat input */
                unsigned long long packets_out=new_stats.p_send-stats.p_send > 0 ? new_stats.p_send-stats.p_send : 0;
                unsigned long long packets_in=new_stats.p_rec-stats.p_rec > 0 ? new_stats.p_rec-stats.p_rec :0;
                printf("<td class='out'>%13.2f P/s </td>",(double)packets_out*multiplier);
                printf("<td class='in'>%13.2f P/s </td>",(double)packets_in*multiplier);
                printf("<td class='total'>%13.2f P/s</td><tr>\n",(double)(packets_out+packets_in)*multiplier);
            } else {
                char bytes_buf[20];
                unsigned long long bytess=new_stats.send-stats.send > 0 ? new_stats.send-stats.send : 0;
                unsigned long long bytesr=new_stats.rec-stats.rec > 0 ? new_stats.rec-stats.rec : 0;
                /* output Bytes/s */
                printf("<td class='out'>%s </td>",convert_bytes((double)(bytess*multiplier),bytes_buf,20));
                printf("<td class='in'>%s </td>",convert_bytes((double)(bytesr*multiplier),bytes_buf,20));
                /* print total (send+rec) of current iface */
                printf("<td class='total'>%s</td></tr>\n",convert_bytes((double)((bytess+bytesr)*multiplier),bytes_buf,20));
            }
            break;
        case CSV_OUT:
			out_file=csv_file==NULL ? stdout : csv_file;
            fprintf(out_file,"%i%c%s%c",(int)time(NULL),csv_char,if_name,csv_char);
            if (!netstat_i && !show_packets) {
                unsigned long long bytess=new_stats.send-stats.send > 0 ? new_stats.send-stats.send : 0;
                unsigned long long bytesr=new_stats.rec-stats.rec > 0 ? new_stats.rec-stats.rec : 0;
                /* output Bytes/s */
                fprintf(out_file,"%.2f%c%.2f%c%.2f%c",(double)(bytess*multiplier),csv_char,(double)(bytesr*multiplier),csv_char,(double)((bytess+bytesr)*multiplier),csv_char);
            }
            /* show packets/s if asked for or netstat input */
            unsigned long long packets_out=new_stats.p_send-stats.p_send > 0 ? new_stats.p_send-stats.p_send : 0;
            unsigned long long packets_in=new_stats.p_rec-stats.p_rec > 0 ? new_stats.p_rec-stats.p_rec : 0;
            fprintf(out_file,"%.2f%c%.2f%c%.2f\n",(double)packets_out*multiplier,csv_char,(double)packets_in*multiplier,csv_char,(double)(packets_out+packets_in)*multiplier);
            break;
    }
}

