/*
 * Copyright (C) 2005 Alex Murray <pragmatine@gmail.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
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "i2c-sys-sensors-interface.h"
#include "sensors-applet.h"

#define I2C_SYS_BASE_DIR "/sys"

/* for error handling */
#define I2C_SYS_DEVICE_FILE_ERROR (i2c_sys_sensors_interface_device_file_error_quark())

enum {
	I2C_SYS_DEVICE_FILE_OPEN_ERROR,
	I2C_SYS_DEVICE_FILE_READ_ERROR
};

static void i2c_sys_sensors_interface_add_sensor(SensorsApplet *sensors_applet, const gchar *path) {
	gchar *filename;
	gchar *id;
	gchar *label;
	gboolean enable;
	guint sensor_type;
	const gchar *icon_filename = NULL;

	filename = g_path_get_basename(path);

	/* setup temp2 as CPU sensor and enable it */
	if (g_ascii_strcasecmp(filename, "temp2_input") == 0) {
		id = g_strndup(filename, 5);
		label = g_strdup(_("CPU"));
		sensor_type = TEMP_SENSOR;
		enable = TRUE;
		icon_filename = CPU_ICON;
	} else {
		switch(filename[0]) {
		case 'c':
			/* either current or cpu?_vid sensor */
			if (filename[1] == 'u') { /* currents are curr?_input */
				id = g_strndup(filename, 5);
				label = g_strndup(filename, 5);
				sensor_type = CURRENT_SENSOR;
			}
			else { /* cpu_vid is cpu?_vid */
				id = g_strdup(filename);
				label = g_strdup(filename);
				sensor_type = VOLTAGE_SENSOR;
				icon_filename = VOLTAGE_ICON;
			}
			break;			
		case 'f':     /* fans are "fan?_input" */
			id = g_strndup(filename, 4);
			label = g_strndup(filename, 4);
			sensor_type = FAN_SENSOR;
			icon_filename = FAN_ICON;
			break;
		case 't':     /* temps are "temp?_input" */
			id = g_strndup(filename, 5);
			label = g_strndup(filename, 5);
			sensor_type = TEMP_SENSOR;
			break;
		case 'i':     /* voltages are "in?_input" */
			id = g_strndup(filename, 3);
			label = g_strndup(filename, 3);
			sensor_type = VOLTAGE_SENSOR;
			icon_filename = VOLTAGE_ICON;
			break;
		default:
			/* SHOULDN'T BE ABLE
			 * TO GET HERE!! */
			g_assert_not_reached();
		}
		/* disable all other sensors */
		enable = FALSE;
	}
	sensors_applet_add_sensor(sensors_applet,
				  path,
				  id,
				  label,
				  I2C_SYS,
				  enable,
				  sensor_type,
				  icon_filename);
	g_free(filename);
	g_free(id);
	g_free(label);
}	

/* recursive function to find sensors in a given path - once found
   will call function to add sensor to the sensors_list */
static void i2c_sys_sensors_interface_find_sensors(SensorsApplet *sensors_applet, const gchar *path) {
	GDir *dir;
	FILE *fp;
	const gchar* new_file;
	gchar *new_path, *filename;
	if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
		filename = g_path_get_basename(path);
		if (g_strrstr(filename, "_input") != NULL || 
		    ((g_strrstr(filename, "cpu") == filename) && (g_strrstr(filename, "vid") != NULL))) {
			/* also test can actually open file for
			   reading */
			if ((fp = fopen(path, "r")) != NULL) {
				fclose(fp);
				i2c_sys_sensors_interface_add_sensor(sensors_applet, path);

			}
		}
		g_free(filename);
		
	}
	/* if is a directory (but not a symlinked dir as this
	   will lead us in circular loops) descend into it and look
	   for a sensor dir
	*/
	if (g_file_test(path, G_FILE_TEST_IS_DIR) && !g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
		dir = g_dir_open(path, 0, NULL);
		if (dir != NULL) {
			while(new_file = g_dir_read_name(dir)) {
				new_path = g_strdup_printf("%s/%s", path, new_file);
				i2c_sys_sensors_interface_find_sensors(sensors_applet, new_path);
				g_free(new_path);
			}
			g_dir_close(dir);
		}
	}
}

/* to be called to setup for sys sensors */
void i2c_sys_sensors_interface_init(SensorsApplet *sensors_applet) {
	sensors_applet_register_sensors_interface(sensors_applet,
						  I2C_SYS,
						  i2c_sys_sensors_interface_get_sensor_value);
	/* call function to recursively look for sensors
	   starting at the defined base directory */
	i2c_sys_sensors_interface_find_sensors(sensors_applet, I2C_SYS_BASE_DIR);
		
}


/* for error handling */
static GQuark i2c_sys_sensors_interface_device_file_error_quark(void) {
	static GQuark quark = 0;
	gchar *string;

	if (quark == 0) {
		string = g_strdup_printf("%s-device-file-error", sensor_interface[I2C_SYS]);
		quark = g_quark_from_string(string);
		g_free(string);
	}

	return quark;
}

gdouble i2c_sys_sensors_interface_get_sensor_value(const gchar *path, 
						  const gchar *id, 
						  SensorType type,
						  GError **error) {

	/* to open and access the value of each sensor */
	FILE *fp;
	gfloat sensor_value;

	if (NULL == (fp = fopen(path, "r"))) {
		g_set_error(error, I2C_SYS_DEVICE_FILE_ERROR, I2C_SYS_DEVICE_FILE_OPEN_ERROR, "Error opening sensor device file %s", path);
		return;
	}

	if (fscanf(fp, "%f", &sensor_value) != 1) {
		g_set_error(error, I2C_SYS_DEVICE_FILE_ERROR, I2C_SYS_DEVICE_FILE_READ_ERROR, "Error reading from sensor device file %s", path);
		fclose(fp);
		return;
	}
	fclose(fp);

	if (type != FAN_SENSOR) {
		sensor_value /= 1000.0;
	}

	return (gdouble)sensor_value;
}


syntax highlighted by Code2HTML, v. 0.9.1