/*
 * Temperature monitor
 * Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.com)
 *
 * 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
 *
 */

 /*
  Modified for mrtg use - by dr_flubber Sept 2003 
  Further modifications by ShALLaX
 */
 
#include <fcntl.h>
#include "i2c-dev.h"
#include <linux/i2c.h>
#include <linux/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
	char *fname = "/dev/i2c/0";
	int fd, bt, ct, btf, ctf, cs, scale, cpu, board;

	// scale = 0; - Celsius
	// scale = 1; - Farenheit

	scale = @CHANGEME@;

	if ((fd = open(fname, O_RDWR)) < 0) {
		fprintf(stderr, "Error in open(%s): %s\n", fname, strerror(errno));
		return 1;
	}

	if (ioctl(fd,I2C_SLAVE_FORCE,0x10) < 0) {
		fprintf(stderr,"Error: Could not set address to %d: %s\n",0x10,strerror(errno));
		return 2;
	}

        ct = i2c_smbus_read_byte_data(fd,0x09);
	bt = i2c_smbus_read_byte_data(fd,0x0A);

	ctf = ((9.0 / 5.0) * ct) + 32.0;
	btf = ((9.0 / 5.0) * bt) + 32.0;
	
        cs = i2c_smbus_read_byte_data(fd,0x10);
        cs = cs * 2;

        if (scale == 1) {
		cpu = ctf;
		board = btf;
	} else {
		cpu = ct;
		board = bt;
	}
        
        printf("%i\n%i\n", cpu, board);

	close(fd);
	return 0;
}
