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

@zonegroups = qw(
   US
   Africa
   America
   Antarctica
   Arctic
   Asia
   Atlantic
   Australia
   Brazil
   Canada
   Chile
   Europe
   Indian
   Mexico
   Mideast
   Pacific
);

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

   $line = 2;
   $count = 1;
   $scr->at($line,1);
   $scr->bold();
   $scr->blue();
   $out=sprintf("  %-35.35s %-52.52s", "Prefix", "Zone");
   $scr->puts($out);
   $scr->normal();
   $line++;
   $count++;
   $col = 0;
   foreach $zonegroup ( @zonegroups ) {
      if ( $zone_select == $count) {
         $zone_group_selected = $zonegroup;
         $scr->at($line,$col);
         if ( $zone_selected ) {
            $scr->bold();
            $scr->cyan();
            $out=sprintf("   %-25.25s->", $zonegroup);
         } else {
            $scr->reverse();
            $scr->cyan();
            $scr->inverse();
            $out=sprintf("   %-25.25s->", $zonegroup);
         }
      } else {
         $scr->at($line,$col);
         $scr->normal();
         $out=sprintf("   %-27.27s", $zonegroup);
      }
      $scr->puts($out);
      $line++;
      $count++;
   }
   $zone_total = $count - 1;
#   if ( $zone_selected ) {
      $line = 3;
      $col = 35;
      $count = 2;
      chdir("/usr/share/zoneinfo/$zone_group_selected");
      @zones = glob("*");
      $zone_count = @zones;
      if ( $zone_count < $zone_show_end ) {
         $zone_show_end = $zone_count;
      } elsif ( $select >= $zone_count ) {
         $select = $zone_count-1;
      } elsif ( $select <= 0 ) {
         $select = 0;
      } elsif ( $select >= $zone_show_end - 1 and $select + 1 ne $zone_count ) {
         $zone_show_start++;
         $zone_show_end++;
      } elsif ( $select >= $zone_show_end ) {
         $zone_show_start++;
         $zone_show_end++;
      } elsif ( $select <= $zone_show_start ) {
         $zone_show_start--;
         $zone_show_end--;
      }
      for($c=$zone_show_start;$c < $zone_show_end; $c++) {
         if ( $select == $c and $zone_selected ) {
            $zone_file = $zones[$c];
            $scr->at($line,$col);
            $scr->reverse();
            $scr->cyan();
            $scr->inverse();
         } else {
            $scr->at($line,$col);
            $scr->normal();
         }
         $out=sprintf(" %-30.30s", $zones[$c]);
         $scr->puts($out);
#         &info_message($scr, "Configure: $zone_group_selected/$zone_file");
         $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-1);
}


$zone_select = 2;
         $zone_show_start = 0;
         $zone_show_end = 17;
         $select = 0;
while(1){
   $total = &write_page();
   if ( $saved ) {
      &info_message($scr, "Updated default system zone to '$saved' (/etc/localtime)",2);
   } elsif ( $zone_selected )  {
      chomp ($date = `TZ=$zone_group_selected/$zone_file date +'%a %b %d %l:%M:%S %p'`);
      $msg = sprintf("Timezone: %-25.25s %42.42s", "$zone_group_selected/$zone_file", $date);
      &info_message($scr, $msg);
   } else {
      $msg = sprintf("Timezone: %-25.25s", "$zone_group_selected/");
      &info_message($scr, $msg);
   }
#   $scr->flush_input();
   $key = $scr->getch();
   if ( $key =~ /kd/ or $key =~ /        / ) {
      if ( ! $zone_selected ) {
         if ( $zone_total == $zone_select ) {
#            $zone_select = 2;
         } else {
            $zone_select++;
         }
         $zone_show_start = 0;
         $zone_show_end = 17;
         $select = 0;
      } else {
         $select++;
      }
   } elsif ( $key eq 'ku' ) {
      if ( ! $zone_selected ) {
         if ( $zone_select == 2 ) {
#            $zone_select = $zone_total;
         } else {
            $zone_select--;
         }
         $zone_show_start = 0;
         $zone_show_end = 17;
         $select = 0;
      } else {
         $select--;
      }
   } elsif ( $key eq 'kl' ) {
      if ( ! $zone_selected ) {
         last;
      }
      $scr->clrscr();
      $zone_selected = ();
      $zone_show_end = 17;
      $zone_show_start = 0;
      $select = ();
   } elsif ( $key eq '
' or $key eq ' ' or $key eq 'kr' ) {
      if ( ! $zone_selected ) {
         $zone_selected = '1';
         $zone_show_start = 0;
         $zone_show_end = 17;
         $select = 0;
      } else {
         system("cp -ap /usr/share/zoneinfo/$zone_group_selected/$zone_file /etc/localtime");
         $scr->at(21,0);
         $scr->clreol();
         $scr->puts("Configuration has been set to $zone_group_selected:$zone_file");
         $saved = "$zone_group_selected:$zone_file";
         &info_message($scr, "Updated default system zone to '$saved' (/etc/localtime)",2);
         $scr->at(21,0);
         $scr->clreol();
         $scr->cyan();
         $scr->puts("Done. Press any key to continue...");
         $scr->normal();
         $scr->getch();
         $scr->cooked();
         &info_message($scr, undef);
         $scr = ();
         exit;
      }
   } elsif ( $key eq 'd' or $key eq 'q' or $key eq '' ) {
      if ( $zone_selected ) {
         $scr->clrscr();
         $zone_selected = ();
      } else{
         last;
      }
   } else {
   }
}

$scr->normal();
&info_message($scr, undef);
$scr=();

print "\n";

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

}
