#!/usr/bin/perl

# Written by Greg Kurtzer to provide a basic interface to configure
# network devices for cAos Linux.

BEGIN {
   push (@INC, "./lib/");
   push (@INC, "../lib/");
   push (@INC, "/usr/lib/sidekick/");
   push (@INC, "/usr/lib64/sidekick/");
}


use Term::ScreenColor;
use Getopt::Long;
use Sys::Hostname;
use Sidekick;
#use strict;

Getopt::Long::Configure ("bundling", "pass_through");

my $usage = "USAGE: $0 [OPTIONS]...
   About:
      configure-hostname...

   Options:
        --init      Run for the first time including defaults
    -h, --help      Show this help screen

  This tool is part of cAos - Community Linux
     http://caos.caosity.org/
";


GetOptions(
   'help|h'  => \$help,
   'init' => \$init,
);

if ( $help ) {
   die $usage;
}


if ( $> != '0' ) {
   die "You need to run this as root!\n";
#   warn "You need to be root to make configuration changes!\n";
}

if ( -f "/etc/sysconfig/network") {
   @networkConf = &conf_array("/etc/sysconfig/network");
   %array = &conf_array_to_hash(@networkConf);
   $hostname = $array{"HOSTNAME"};
} else {
   push(@networkConf, "NETWORKING=yes");
   $hostname = "localhost.localdomain";
}

sub write_hosts {
   my ($namealias, $name, @NULL ) = @_;
   my $out;
   if ( -f "/etc/hosts" ) {
      open(HOSTS, "< /etc/hosts");
      while (<HOSTS>) {
         chomp;
         if ( $_ =~ /^(127.0.0.1\s+)(.+)$/ ) {
            $out .= "$1$namealias $name";
            if ( $namealias ne "localhost" ) {
               $out .= " localhost";
            }
            if ( $name ne "localhost.localdomain" ) {
               $out .= " localhost.localdomain";
            }
            $out .= "\n";
         } else {
            $out .= "$_\n";
         }
      }
      close HOSTS;
   } else {
      $out = "127.0.0.1\t\t$namealias $name";
      if ( $namealias ne "localhost" ) {
         $out .= " localhost";
      }
      if ( $name ne "localhost.localdomain" ) {
         $out .= " localhost.localdomain";
      }
      $out .= "$_\n";
   }
   open(HOSTS, "> /etc/hosts");
   print HOSTS $out;
   close HOSTS;
}

sub write_page {
   my ( $sel, @NULL) = @_;
   if ( ! $scr ) {
      $scr = new Term::ScreenColor;
      $scr->noecho();
   }
   $scr->clrscr();
   $scr->at(0,0);
   $scr->on_blue();
   $scr->white();
   $scr->puts("cAos Linux: System Hostname Configuration                                       ");
   $scr->normal();

   $line = 2;
   $count = 1;

   return($count);
}


$total = &write_page($select);
&info_message($scr, "Select the fully qualified domain name of your system (FQDN)", 1);
$scr->at($line+1,0);
$scr->clreol();
$scr->bold();
$scr->puts("Define your system's hostname:");
$scr->at($line+2,0);
$scr->clreol();
$scr->puts("> ");
$scr->normal();
$hostname = &get_str($scr,$line+2,3,'a-zA-Z0-9.\-_', $hostname);
@networkConf = &change_line("HOSTNAME", "$hostname", @networkConf);
&conf_write("/etc/sysconfig/network", @networkConf);
$hostname =~ /^([a-zA-Z0-9\-_]+)\..*$/;
$hostalias = $1;
&write_hosts($hostalias, $hostname);
&info_message($scr, "Updated the system's hostname (/etc/sysconfig/network, /etc/hosts)", 2);
$scr->at($line+1,0);
$scr->clreol();
$scr->bold();
$scr->puts("Done. Press any key to continue...");
$scr->getch();
$scr->normal();
$scr->cooked();
&info_message($scr, undef);
$scr=();

if ( ! $init ) {
   print "You will need to restart computer for all changes to take affect.\n";
}

