#define _GNU_SOURCE
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <ctype.h>

#include "lsfpu.c"
#include "lsmem.c"
#include "lsalu.c"
#include "lscompile.c"
#include "lsmedia.c"
#include "lscompress.c"
#include <math.h>       /* for sin, exp etc.           */
#include <stdio.h>      /* standard I/O                */
#include <string.h>     /* for strcpy - 3 occurrences  */
#include <stdlib.h>     /* for exit   - 1 occurrence   */
#include <time.h>
double ge0,ge1,lsgfx;


void title() {
        printf("\tLSBENCH is a product of the linuxsociety team\n");
        printf("\tThis is version beta-0.2.0 dated 12/04/2007\n");
        printf("\tIt is not recommended that you run this benchmark on systems with less than 64MB of physical memory\n\n");
return;
}

void usage() {
        printf("Usage: lsbench -[option]\n");
        printf("Options:\n");
	printf("        -h: display this help menu\n");
	printf("        -a: run all tests\n");
        printf("        -1: memory bandwidth tests\n");
        printf("        -2: fpu tests\n");
        printf("        -3: alu tests\n");
        printf("        -4: compiling tests\n");
        printf("        -5: media conversion tests\n");
        printf("        -6: archiving/compression tests\n");
	printf("        -7: graphics tests\n");
        printf("        -s: silent, suppress all output except final indexes\n");
        printf("\n");
        printf("\n");
}


int All() {
        title();
        printf("\nRunning Memory tests (3 for now):\n");
        memtests();
        printf("\nRunning FPU tests (6 for now):");
        Section1();
        Problem0();
        Problem1();
        printf("\nRunning Compiler tests:\n");
        sleep (5);
        Compile1();
        printf("\tdone.\n");
        printf("\nRunning Media conversion tests:\n");
        Media1();
        printf("\tdone.\n");
        printf("\nRunning Compression/Archiving tests:");
        Compress1();
        printf("\tdone.\n");
	
	printf("\nRunning Simple graphics tests:");
        gettimeofday(&starttime, NULL);
        system("gfxdir/lsgfx");
        gettimeofday(&endtime, NULL);
        ge0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
        printf("\tdone.\n");
	
        lsalu = (prob1 + prob0) / 80000;
        lsfpu = pass0 / 1200000 / 8.5;
        lscomp = comp1 * 3;
        lsmedia = media1 * 23;
        lscompress = compress1 * 25;
	lsgfx = 150/ge0;

	printf("\n\n\tfinished.\n");
        printf("\nLSBENCH Results:\n");
        printf("\tMemory index:           %.3f \n", lsmem);
        printf("\tFPU index:              %lf\n", lsfpu);
        printf("\tALU index:              %lf\n", lsalu);
        printf("\tCompile index:          %lf\n", lscomp);
        printf("\tMedia index:            %lf\n", lsmedia);
        printf("\tCompression index:      %lf\n", lscompress);
	printf("\tGraphics index:         %lf\n\n", lsgfx);
        return 0;
}


//int argc, char** argv
int main(int argc, char **argv) {
	int o;
        while((o=getopt(argc, argv, "ha1234567s")) != EOF) {
                switch(o) {
                        case 'h':
                                usage();
                                exit(1);
                                break;
                        case 'a': 
				All();                                
				exit(1);
                                break;
                        case '1': 
				title();
				printf("\nRunning Memory tests (3 for now):\n");
				memtests();
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
				printf("\n\tMemory index:           %.3f \n", lsmem);                                
				exit(1);
                                break;
                        case '2':
				title();
				printf("\nRunning FPU tests (6 for now):");
				Section1();
				lsfpu = pass0 / 1200000 / 8.5;
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
				printf("\n\tFPU index:              %lf\n", lsfpu); 
                                exit(1);
                                break;
                        case '3':
				title();
				Problem0();
				Problem1();
				lsalu = (prob1 + prob0) / 80000;
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
				printf("\n\tALU index:              %lf\n", lsalu);
                                exit(1);
                                break;
                        case '4':
				title();
			        printf("\nRunning Compiler tests:\n");
				sleep (5);
				Compile1();
				printf("\tdone.\n");
				lscomp = comp1 * 3;
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
				printf("\n\tCompile index:          %lf\n", lscomp);
				exit(1);
                                break;
                        case '5':
				title();
			        printf("\nRunning Media conversion tests:\n");
			        Media1();
        			printf("\tdone.\n");
				lsmedia = media1 * 23;
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
				printf("\n\tMedia index:            %lf\n", lsmedia);
				exit(1);
				break;
                        case '6':
				title();
				printf("\nRunning Compression/Archiving tests:\n");
        			Compress1();
			        printf("\tdone.\n");
				lscompress = compress1 * 25;
				printf("\n\n\tfinished.\n");
				printf("\nLSBENCH Results:");
        			printf("\n\tCompression index:      %lf\n\n", lscompress);
				exit(1);
				break;
                       case '7': 
                                title();
                                printf("\nRunning Simple Graphics test:\n");
				gettimeofday(&starttime, NULL);
				system("gfxdir/lsgfx");
				gettimeofday(&endtime, NULL);
				ge0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
				lsgfx = 150/ge0;
                                //printf("\tdone.\n");
                                printf("\n\n\tfinished.\n");
                                printf("\nLSBENCH Results:");
                                printf("\n\tGraphics index:      %lf\n\n", lsgfx);
                                exit(1);
                                break;
                        case 's':
				title();
				printf("\n\tWill be added soon!\n\n");
				exit(1);
				break;
                        default:
			        break;
			}
        }
        while(optind) {
        	title();
                usage();
		printf("\nError: No run options specified!\n\n");
                exit(1);
        }


return 0;
}
