#!/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");

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

my @servers = qw(
   time-a.nist.gov
   time-b.nist.gov
   time-a.timefreq.bldrdoc.gov
   time-b.timefreq.bldrdoc.gov
   utcnist.colorado.edu
   clock.psu.edu
);

sub network_time_sync {

   $l = @servers;
   $count = 0;
   while(1){
      $r = rand($l);
      $r = int $r;
      &info_message($scr, "Syncronizing from: $servers[$r]");
      system("rdate -s $servers[$r] >/dev/null 2>&1");
      if ( $? == '0' ) {
         $server = $servers[$r];
         last;
      } else {
         if ( $count >= 10 ) {
            &info_message($scr, "ERROR: Could not connect to servers! Check networking configuration!\n", 2);
            last;
         }
      }
      $count ++;
   }
   return $server;
}

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: Local time configuration                                            ");
   $scr->normal();

   $line = 2;
   $count = 1;
   $scr->at($line,1);
   $scr->bold();
   $scr->blue();
   $out=sprintf("  %-76.76s", "How do you wish to configure the system's time?");
   $scr->puts($out);
   $scr->normal();
   $line++;
   $count++;

   if ( $sel == $line ) {
      &info_message($scr, "Synchronize time via a publically available network time server");
      $selected = 'autonet';
      $scr->at($line,1);
      $scr->reverse();
      $scr->cyan();
      $scr->inverse();
   } else {
      $scr->at($line,1);
      $scr->normal();
   }
   $out=sprintf("  %-76.76s", "Probe for current time over the internet");
   $scr->puts($out);
   $line++;
   $count++;

   if ( $sel == $line ) {
      &info_message($scr, "Enter the address of your own network time server");
      $selected = 'netmanual';
      $scr->at($line,1);
      $scr->reverse();
      $scr->cyan();
      $scr->inverse();
   } else {
      $scr->at($line,1);
      $scr->normal();
   }
   $out=sprintf("  %-76.76s", "Connect to a specific network time server");
   $scr->puts($out);
   $line++;
   $count++;

   if ( $sel == $line ) {
      &info_message($scr, "Message about opt 1");
      $selected = 'manual';
      $scr->at($line,1);
      $scr->reverse();
      $scr->cyan();
      $scr->inverse();
   } else {
      $scr->at($line,1);
      $scr->normal();
   }
   $out=sprintf("  %-76.76s", "Enter time manually");
   $scr->puts($out);
   $line++;
   $count++;


   $scr->normal();
   $scr->at(21,0);
   $scr->clreol();
   $scr->cyan();
   $scr->puts("Arrow keys to navigate, space/enter to select, 'd' when done, [ESC] to quit: ");
   $scr->normal();
   return($count);
}


$select = 3;
while(1){
   $total = &write_page($select);
   $scr->flush_input();
   $key = $scr->getch();
   if ( $key =~ /kd/ or $key =~ /        / ) {
      if ( $total == $select ) {
#         $select = 3;
      } else {
         $select++;
      }
   } elsif ( $key eq 'ku' ) {
      if ( $select == 3 ) {
#         $select = $total;
      } else {
         $select--;
      }
   } elsif ( $key eq '
' or $key eq ' ' or $key eq 'kr' ) {
      if ( $selected eq 'autonet' ){
         $server = &network_time_sync();
         chomp ($date = `date +'%a %b %d %l:%M:%S %p'`);
         $out = sprintf("Updated from: %-20.20s %43.43s", $server, "Local time is now: $date");
         &info_message($scr, $out);
         $scr->at($line+1,0);
         $scr->clreol();
         $scr->puts("Done. Press any key to continue...");
         $scr->getch();
         $scr->normal();
         &info_message($scr, undef);
         $scr=();
         print "\n";
         exit;
      } elsif ( $selected eq 'netmanual' ) {
         $scr->at($line+1,0);
         $scr->clreol();
         $scr->bold();
         $scr->puts("What network time server should be used?");
         $scr->at($line+2,0);
         $scr->clreol();
         $scr->puts("> ");
         $scr->normal();
         $servername = &get_str($scr,$line+2,3,'a-zA-Z0-9.:/\-_',undef);
         system("rdate -s $servername >/dev/null 2>&1");
         if ( $? eq '0' ) {
            chomp ($date = `date +'%a %b %d %l:%M:%S %p'`);
            $out = sprintf("Updated from: %-20.20s %43.43s", $servername, "Local time is now: $date");
            &info_message($scr, $out);
            $scr->at($line+1,0);
            $scr->clreol();
            $scr->puts("Done. Press any key to continue...");
            $scr->getch();
            $scr->normal();
            &info_message($scr, undef);
            $scr=();
            print "\n";
            exit;
         } else{
            &info_message($scr, "ERROR: Could not reach $servername!");
            $scr->at($line+1,0);
            $scr->clreol();
            $scr->puts("ERROR. Press any key to continue...");
            $scr->getch();
         }
      } elsif ( $selected eq 'manual' ) {
         $scr->at($line+1,0);
         $scr->clreol();
         $scr->bold();
         $scr->puts("What is the current date: (month/day/year): ");
         $scr->normal();
         chomp ($date = `date +'%D'`);
         $date = &get_str($scr,$line+1,44,'0-9/',"$date");
         $scr->at($line+1,0);
         $scr->clreol();
         $scr->bold();
         $scr->puts("What is the current time: (hour:minute): ");
         $scr->normal();
         chomp ($time = `date +'%I:%M'`);
         $time = &get_str($scr,$line+1,41,'0-9:',$time);
         system("date -s '$date' >/dev/null 2>&1");
         if ( $? == '0' ) {
            system("date -s '$time' >/dev/null 2>&1");
            if ( $? == '0' ) {
               chomp ($date = `date +'%a %b %d %l:%M:%S %p'`);
               $out = sprintf("Updated Manually                   %43.43s", "Local time is now: $date");
               &info_message($scr, $out);
               $scr->at($line+1,0);
               $scr->clreol();
               $scr->puts("Done. Press any key to continue...");
               $scr->getch();
               $scr->normal();
               &info_message($scr, undef);
               $scr=();
               print "\n";
               exit;
            } else {
               &info_message($scr, "ERROR: Could not set time $time!");
               $scr->at($line+1,0);
               $scr->clreol();
               $scr->puts("ERROR. Press any key to continue...");
               $scr->getch();
            }
         } else {
            &info_message($scr, "ERROR: Could not set date $date!");
            $scr->at($line+1,0);
            $scr->clreol();
            $scr->puts("ERROR. Press any key to continue...");
            $scr->getch();
         }
         $out = sprintf("Did't actually do anything yet...  %43.43s", "Local time is now: $date");
         &info_message($scr, $out);
      }
   } elsif ( $key eq 'd' ) {
      $post = '1';
      last;
   } elsif ( $key eq 'q' or $key eq 'kl' or $key eq '' ) {
      last;
   } else {
   }
}

$scr->normal();
&info_message($scr, undef);
$scr=();
print "\n";

if ( $post ) {
   # Do soemthing else useful hery

}
