#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <unistd.h>
#define DEFAULT_SEC 5

/* debug flag used in InitMBInfo() */
int debug_flag = 0;

main(int argc, char *argv[])
{
	int n, sec;
	float temp1 = 0.0, temp2 = 0.0, temp3 = 0.0;
	float vc0, vc1, v33, v50p, v12p, v12n, v50n;
	int rot1, rot2, rot3;
	extern char *optarg;
	extern int optind;
#ifdef HAVE_SMBUS
	int ch,method='S';
#else
	int ch,method='I';
#endif
	char *name;
	name=argv[0];
	while((ch=getopt(argc,argv,"ISd"))!=-1){
	  switch(ch){
	  case 'I':
	    method='I';
	    break;
	  case 'S':
	    method='S';
	    break;
	  case 'd':
		debug_flag = 1;
	    break;
	  default:
	    fprintf(stderr, "Usage: %s -[I|S] [-d (debug)] <seconds for sleep>"\
		    " (default %d sec)\n", argv[0], DEFAULT_SEC);
	    exit(1);
	  }
	} 
	argc-=optind;
	argv+=optind;
	if (argc >= 1) {
	  if((n = atoi(argv[0])) > 0) {
		sec = n;
		printf("%d\n",n);
	  } else {
	    fprintf(stderr, "Usage: %s -[I|S] [-d (debug)] <seconds for sleep>"\
		    " (default %d sec)\n", argv[0], DEFAULT_SEC);
		exit (1);
	  }
	} else {
		sec = DEFAULT_SEC;
	}
	if (InitMBInfo(method)!=0){
		perror("InitMBInfo");
		fprintf(stderr," this program needs \"setuid root\" !!\n");
		exit (1);
	}
	if (debug_flag)
		exit (0);

	while(1){

/* Temperature */

	getTemp(&temp1, &temp2, &temp3);

	printf("Temp.= %4.1f, %4.1f, %4.1f;",temp1, temp2, temp3);

/* Fan Speeds */

	getFanSp(&rot1, &rot2, &rot3);

	printf(" Rot.= %4d, %4d, %4d\n", rot1, rot2, rot3);

/* Voltages */

	getVolt(&vc0, &vc1, &v33, &v50p, &v50n, &v12p, &v12n);

	printf(" Vcore = %4.2f, %4.2f; Volt. = %4.2f, %4.2f, %5.2f, %6.2f, %5.2f\n", vc0, vc1, v33, v50p, v12p, v12n, v50n);

/* sleep */
	sleep(sec);

	}

}

