#!/usr/bin/env perl

use warnings;
use diagnostics;

use lib "../../lib";
use HotSaNICparser;
use HotSaNICshellio;

my @CONFmod=HotSaNICparser::read_settings(".",$MODNAME);

if ( ! -e "settings" ) { $OUTFILE="settings"; } else { $OUTFILE="settings.new"; }

open OUTFILE,">$OUTFILE" || die "could not open $MODNAME settings file for writing.\n";

print OUTFILE "# SHORT DESCRIPTION
#
# all entries have to be of the form:
# SENSOR=<device-file>,<dbname>,<description>,<entry>,<scale-factor>,<add>,<unit>
#
# <device-file>  path to the file under /proc which holds the device-data
#
# <dbname>       an unique filename for the database
#
# <description>  a brief description for the webpage
#
# <entry>        2 for fan-speed and 3 for temperature and voltage
#                this represents the position of the value in the
#                proc-file
#
# <scale-factor> correction-factor to scale the data correctly
#                ( to understand this look at the lm_sensors howto )
#
# <add>          value to add to shift the data correctly
#                ( for e.g. +12V - see lm_sensors howto )
#
# <unit>         unit to be shown in diagrams and on webpage
#                ( for example °C, V, Volts, RPM etc... )
#
# Example:
#
# SENSOR=/proc/sys/dev/sensors/gl518sm-i2c-0-2d/temp,temp,CPU temp,3,1,0,°C
#
#
#
# FreeBSD Users:
# The only fields required are:
#   <dbname>		which is an item from the left column in 'mbmon -rc1'
#   <description>	as above
#   <unit>		as above
#
# Example:
# SENSOR=,TEMP0,Motherboard Temp,,,,C
";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^SENSOR *=/; }
  }
else {
  my @files=();
  if ( -e "/proc/sys/dev/sensors") {
    require File::Find;
    $File::Find::name="";  # suppress stupid warning...
    File::Find::find( {wanted => sub { lstat($_) && -f _ && push @files, $File::Find::name; } }, "/proc/sys/dev/sensors");
    }

  print "\n";

  for $file (@files) {
    if ( $file !~ /sensors\/(eeprom|chips)/ ) {
      print $file,":\n";
      open FILE,$file;
      $line=<FILE> || "";
      chomp $line;
      print "  ",$line,"\n";
      close FILE;
      $input=HotSaNICshellio::askyesno("Do you want to use this file","n");
      if ($input eq "y") {
        # configure sensor
        #
        @entries=split /\s/,$line;
        ($dbname=$file) =~ s/.*\///g;
        $descr="value";
        $unit="units";
        $device="some";
        if ( $dbname =~ /temp/ ) { $unit="°C"; $descr="temperature"; }
        elsif ( $dbname =~ /v/ ) { $unit="Volts"; $descr="voltage"; }
        elsif ( $dbname =~ /fan/ ) { $unit="RPM"; $descr="speed"; }
        if ( $dbname =~ /vdd/) { $device="CPU"; }
        print "SENSOR=$file,$dbname,$device $descr,".($#entries+1).",1,0,$unit\n"
        }
      }
    print "\n";
    }
  }

close OUTFILE;

if ($OUTFILE eq "settings.new") {
  HotSaNICparser::backup_file("settings");
  rename "settings.new","settings";
  }

if ($^O eq "freebsd") {
  print "FreeBSD support is ONLY via xmbmon
(http://www.nt.phys.kyushu-u.ac.jp/shimizu/download/) version 2.0 or greater.
Please ensure you have this installed.
";
  }

print "Please check the settings file and adapt it to satisfy your needs.
for your info: this configuration is a bit tricky\n";



