#!/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:
      sidekick is a basic system administrator's assistant.

   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 ( $init ) {
   push(@ARGV, "--init");
}

%modules = &get_module_config();


sub InitOrder {
   $a <=> $b;
}

sub SortOrder {
   $modules{$catagory_group_selected}{$a}{Name} cmp $modules{$catagory_group_selected}{$b}{Name};
}


sub write_init_page {
   my ( $sel, @NULL) = @_;
   if ( ! $scr ) {
      $scr = new Term::ScreenColor;
      $scr->clrscr();
   }
   $scr->at(0,0);
   $scr->on_cyan();
   $scr->blue();
   $scr->puts("cAos Linux: System administrator's assistant: \"Sidekick\" (initalization mode)   ");
   $scr->normal();

   $line = 2;
   $count = 1;
   $scr->at($line,1);
   $scr->bold();
   $scr->blue();
   $out=sprintf("  %-23.23s %-52.52s", "Option", "Description");
   $scr->puts($out);
   $scr->normal();
   $line++;
   $count++;




   foreach $num ( sort InitOrder keys %init ) {
      $catagory = $init{$num}{"catagory"};
      $module = $init{$num}{"module"};
      if ( $modules{"$catagory"}{"$module"}{Init} ) {
#         print "# $modules{$catagory}{$module}{Cmd} $modules{$catagory}{$module}{InitArg}\n";
#         system("$modules{$catagory}{$module}{Cmd} $modules{$catagory}{$module}{InitArg}");
         $scr->at($line,1);
         if ( $sel == $line ) {
            $scr->reverse();
            $scr->blue();
            $scr->inverse();
            $selected_cmd = $modules{$catagory}{$module}{Cmd};
            $selected_cmd_args = $modules{$catagory}{$module}{InitArgs};
         } else {
            $scr->normal();
         }
         $out=sprintf("  %-23.23s %-52.52s", $modules{$catagory}{$module}{Name}, $modules{$catagory}{$module}{Desc});
         $scr->puts($out);
         $line++;
         $count++;
         $scr->normal();
      }
   }

   $scr->at($line,1);
   if ( $sel == $line ) {
      $scr->reverse();
      $scr->blue();
      $scr->inverse();
      $selected_cmd = "/usr/bin/sidekick";
      $selected_cmd_args = '';
   } else {
      $scr->normal();
   }
   $out=sprintf("  %-23.23s %-52.52s", "Sidekick Main Menu", "View all sidekick configuration options");
   $scr->puts($out);
   $line++;
   $count++;
   $scr->normal();

   $info_done_line = sprintf("Command: %-67.67s", "$selected_cmd $selected_cmd_args");
   &info_message($scr, "$info_done_line");
   $scr->at($line+1,0);
   $scr->clreol();
   $scr->yellow();
   $scr->puts("Arrow keys to navigate, space/enter to select, 'd' when done, [ESC] to quit: ");
   $scr->normal();
   return($count);
}



if ( $init ) {
   foreach $catagory ( keys %modules ) {
      foreach $module ( keys %{$modules{"$catagory"}} ) {
         $tmp = $modules{"$catagory"}{"$module"}{"Init"};
         $init{$tmp}{"catagory"} = $catagory;
         $init{$tmp}{"module"} = $module;
      }
   }

   $select = 3;
   while(1){
      $total = &write_init_page($select);
      $scr->raw();
      $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 '   ' or $key eq 'kr' ) {
         $done{$select} = '1';
         $scr->normal();
         &info_message($scr, undef);
         $scr->at($line+3,0);
         $scr->cooked();
         $scr = ();
         print("# $selected_cmd $selected_cmd_args\n");
         system("$selected_cmd $selected_cmd_args");
         if ( $? != 0 or $selected_wait ) {
            print "\nPress [ENTER] to continue...";
            $foo = <STDIN>;
         }
         if ( $select < $total ) {
            for($i=3;$i < $total+3; $i++) {
               if (! $done{$i} ) {
                  $select = $i;
                  last;
               }
            }
         }
      } elsif ( $key eq 'd' ) {
         $inst = '1';
         last;
      } elsif ( $key eq 'q' or $key eq '' ) {
         last;
      } else {
      }
   }

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

   print "\n";

   print "\nUse the command 'sidekick' to invoke this tool later\n\n";
   sleep 2;
   exit;
}

sub write_page {
#   my ( $sel, @NULL) = @_;
   if ( ! $scr ) {
      $scr = new Term::ScreenColor;
      $scr->noecho();
      $rows = $scr->rows();
   }
      $scr->clrscr();
   $scr->at(0,0);
   $scr->on_blue();
   $scr->white();
   $scr->puts("cAos Linux: System administrator's assistant: \"Sidekick\"                        ");
   $scr->normal();

   $line = 2;
   $count = 1;
   $scr->at($line,1);
   $scr->bold();
   $scr->blue();
   $out=sprintf("  %-32.32s %-55.55s", "Category", "Configuration Tool");
   $scr->puts($out);
   $scr->normal();
   $line++;
   $count++;
   $col = 0;
   foreach $catagory ( keys %modules ) {
      if ( $catagory_select == $count) {
         $catagory_group_selected = "$catagory";
         $scr->at($line,$col);
         if ( $catagory_selected ) {
            $scr->bold();
            $scr->cyan();
            $out=sprintf("   %-25.25s->", "$catagory");
         } else {
            $scr->reverse();
            $scr->cyan();
            $scr->inverse();
            $out=sprintf("   %-25.25s->", "$catagory");
         }
      } else {
         $scr->at($line,$col);
         $scr->normal();
         $out=sprintf("   %-27.27s", "$catagory");
      }
      $scr->puts($out);
      $line++;
      $count++;
   }
   $catagory_total = $count - 1;
#   if ( $catagory_selected ) {
      @modules = ();
      $line = 3;
      $col = 35;
      $count = 2;
      foreach $module ( sort SortOrder keys %{$modules{"$catagory_group_selected"}} ) {
         push(@modules, $module);
      }
      $module_count = @modules;
      if ( $module_count < $module_show_end ) {
         $module_show_end = $module_count;
      } elsif ( $select >= $module_count ) {
         $select = $module_count-1;
      } elsif ( $select <= 0 ) {
         $select = 0;
      } elsif ( $select >= $module_show_end - 1 and $select + 1 ne $module_count ) {
         $module_show_start++;
         $module_show_end++;
      } elsif ( $select >= $module_show_end ) {
         $module_show_start++;
         $module_show_end++;
      } elsif ( $select <= $module_show_start ) {
         $module_show_start--;
         $module_show_end--;
      }
      for($c=$module_show_start;$c < $module_show_end; $c++) {
         if ( $select == $c and $catagory_selected ) {
            $module_desc = $modules{"$catagory_group_selected"}{"$modules[$c]"}{"Desc"};
            $module_cmd = $modules{"$catagory_group_selected"}{"$modules[$c]"}{"Cmd"};
            $module_wait = $modules{"$catagory_group_selected"}{"$modules[$c]"}{"Wait"};
            $scr->at($line,$col);
            $scr->reverse();
            $scr->blue();
            $scr->inverse();
         } else {
            $scr->at($line,$col);
            $scr->normal();
         }
         $out=sprintf(" %-30.30s", $modules{"$catagory_group_selected"}{"$modules[$c]"}{"Name"});
         #$out=sprintf(" %-30.30s", "$module_show_start:$module_show_end:$module_count:$modules{$catagory_group_selected}{$modules[$c]}{Name}");
         $scr->puts($out);
#         &info_message($scr, "Configure: $catagory_group_selected/$module_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);
}


$catagory_select = 2;
$module_show_start = 0;
$module_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 ( $catagory_selected )  {
#      chomp ($date = `TZ=$catagory_group_selected/$module_file date +'%a %b %d %l:%M:%S %p'`);
#      $msg = sprintf("Timezone: %-25.25s %42.42s", "$catagory_group_selected/$module_file", $date);
#      &info_message($scr, $msg);
      &info_message($scr, "$module_desc ");
   } else {
#      $msg = sprintf("Timezone: %-25.25s", "$catagory_group_selected/");
      &info_message($scr, "Select a catagory");
   }
#   $scr->flush_input();
   $scr->at(21,77);
   $scr->raw();
   $key = $scr->getch();
   if ( $key =~ /kd/ or $key =~ /        / ) {
      if ( ! $catagory_selected ) {
         if ( $catagory_total == $catagory_select ) {
 #           $catagory_select = 2;
         } else {
            $catagory_select++;
            $module_show_start = 0;
            $module_show_end = 17;
            $select = 0;
         }
      } else {
         $select++;
      }
   } elsif ( $key eq 'ku' ) {
      if ( ! $catagory_selected ) {
         if ( $catagory_select == 2 ) {
 #           $catagory_select = $catagory_total;
         } else {
            $catagory_select--;
               $module_show_start = 0;
            $module_show_end = 17;
            $select = 0;
         }
      } else {
         $select--;
      }
   } elsif ( $key eq 'kl' ) {
      $scr->clrscr();
      $catagory_selected = ();
      $module_show_end = 17;
      $module_show_start = 0;
      $select = ();
   } elsif ( $key eq '
' or $key eq ' ' or $key eq 'kr' ) {
      if ( ! $catagory_selected ) {
         $catagory_selected = '1';
         $module_show_start = 0;
         $module_show_end = 17;
         $select = 0;
      } else {
 #        system("cp -ap /usr/share/zoneinfo/$catagory_group_selected/$module_file /etc/localtime");
#         $scr->at(21,0);
#         $scr->clreol();
#         $scr->puts("Configuration has been set to $catagory_group_selected:$module_file");
#         $saved = "$catagory_group_selected:$module_file";
#         if ( $init ) {
#            last;
#         }
         $scr->cooked();
         $scr->clrscr();
         $scr->at(0,0);
         $scr = ();
         print "# $module_cmd\n";
         system("$module_cmd");
         if ( $? != 0 or $module_wait ) {
            print "\nPress [ENTER] to continue...";
            $foo = <STDIN>;
         }
      }
   } elsif ( $key eq 'd' or $key eq 'q' or $key eq '' ) {
      if ( $catagory_selected ) {
         $scr->clrscr();
         $catagory_selected = ();
      } else{
         last;
      }
   } else {
   }
}

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

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

}
