#!/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
#
# path to iptables or ipchains command
#
";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^IPTABLES/; }
  }
else {
  my @list=grep /iptables$/,HotSaNICparser::locate_files("bin/iptables");
  if (! @list) {
    @list=grep /ipchains$/,HotSaNICparser::locate_files("bin/ipchains");
    }
  $COMMAND=shift @list;
  print OUTFILE "IPTABLES=$COMMAND\n";
  }

print OUTFILE "
# Which kind of legend shall be used
# set  STYLE=\"bytes\"  to display all values in bytes/s
# set  STYLE=\"bits\"   to display all values in bits/s
#
";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^STYLE/; }
  }
else {
  print OUTFILE "STYLE=bytes\n";
  }

print OUTFILE "
# list of interfaces for internal / external hosts
#   INTIF=<interfaces connected to your local network (intranet)>
#   EXTIF=<interfaces connected to the internet>
#
";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^EXTIF/; }
  for (@CONFmod) { print OUTFILE $_,"\n" if /^INTIF/; }
  }
else {
  my $INT="";
  my $EXT="";
  print "Configuring local interfaces.
(i)nternal means an interface pointiong to your local machines (intranet)
(e)xternal means an interface connecten with the internet
(n)one means you don't want to account this interface.\n";
  open LIST,"netstat -i|";
  while (<LIST>) {
    if (/[0-9]/) {
      ($interface)=split;
      print "found $interface - (i)nternal, (e)xternal or (n)one?";
      $input=lc HotSaNICshellio::readkey_list("ien","n");
      if ($input eq "i") {
        if ($INT ne "") { $INT=$INT.",$interface"; }
	else { $INT=$interface; }
        }
      elsif ($input eq "e") {
        if ($EXT ne "") { $EXT=$EXT.",$interface"; }
	else { $EXT=$interface; }
        }
      }
    }
  print OUTFILE "INTIF=\"$INT\"\n";
  print OUTFILE "EXTIF=\"$EXT\"\n";
  }

print OUTFILE "
# list hosts on INTERNAL devices here, multiple lines of the form:
#  DEVINT=\"<host>,<description>\"
#  DEVINT=\"!<host>,<description>\"
#  DEVINT=\"<network>/<netmask>,<description>\"
#  DEVINT=\"!<network>/<netmask>,<description>\"
#\n";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^DEVINT/; }
}

print OUTFILE "
# list hosts on EXTERNAL devices here, multiple lines of the form:
#  DEVEXT=\"<host>,<description>\"
#  DEVEXT=\"!<host>,<description>\"
#  DEVEXT=\"<network>/<netmask>,<description>\"
#  DEVEXT=\"!<network>/<netmask>,<description>\"
#\n";

if ($OUTFILE eq "settings.new") { 
  for (@CONFmod) { print OUTFILE $_,"\n" if /^DEVEXT/; }
}

close OUTFILE;

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

print "Please check the settings file and adapt it to satisfy your needs.\n";

