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

open(GRUB, "< /etc/grub.conf");
while(<GRUB>) {
   chomp;
   if ( $_ =~ /^\s*default=(\d+)/ ) {
      $default_kernel=$1;
   } elsif ( $_ =~ /^\s*title (.+)$/ ) {
      push(@kernels, $1);
   }
}
close GRUB;

sub write_grub {
   my $grub_out;
   open(GRUB, "< /etc/grub.conf");
   while(<GRUB>) {
      chomp;
      if ( $_ =~ /^\s*default=(\d+)/ ) {
         $_ =~ s/^(\s*default=)(\d+)/$1$selected_kernel/;
      }
      $grub_out .= "$_\n";
   }
   open(GRUB, "> /etc/grub.conf");
   print GRUB $grub_out;
   close GRUB;
}

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: Kernel selection                                                    ");
   $scr->normal();

   $line = 2;
   $count = 1;

   foreach $kernel ( @kernels ) {
      if ( $sel == $line ) {
         $selected_kernel = $count - 1;
      #   &info_message($scr, "Message about opt 1");
         $scr->at($line,1);
         $scr->reverse();
         $scr->cyan();
         $scr->inverse();
      } elsif ( $line == $default_kernel +2 ) {
         $scr->at($line,1);
         $scr->bold();
         $scr->cyan();
      } else {
         $scr->at($line,1);
         $scr->normal();
      }
      $out=sprintf(" %-76.76s ", $kernel);
      $scr->puts($out);
      $scr->normal($out);
      if ( $sel == $default_kernel +2) {
         &info_message($scr, "Currently selected as default");
      } else {
         &info_message($scr, " ");
      }
      $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 = $default_kernel+2;
while(1){
   $total = &write_page($select);
   $scr->flush_input();
   $key = $scr->getch();
   if ( $key =~ /kd/ or $key =~ /        / ) {
      if ( $total == $select ) {
#         $select = 2;
      } else {
         $select++;
      }
   } elsif ( $key eq 'ku' ) {
      if ( $select == 2 ) {
#         $select = $total;
      } else {
         $select--;
      }
   } elsif ( $key eq '
' or $key eq ' ' or $key eq 'kr' ) {
      while(1) {
         $scr->at($line+1,0);
         $scr->clreol();
         $scr->puts("Are you sure you want to make this kernel default? [y/n]: ");
         $conf = $scr->getch();
         if ( $conf eq 'y' ) {
            &write_grub();
            $default_kernel = $selected_kernel;
            $scr->at($line+1,0);
            $scr->clreol();
            $scr->puts("Done. Press any key to continue...");
            &info_message($scr, "Updated /etc/grub.conf: $kernels[$selected_kernel]", 2);
            $scr->getch();
            last;
         } elsif ( $conf eq 'n' ) {
            last;
         }

      }
   } 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

}
