#!/usr/bin/perl

# Written by Greg Kurtzer to provide a basic interface to configure
# the startup and shutdown of services.

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-chkconfig is a curses based interface similiar to
      /sbin/chkconfig with several UI beneffits (starts/stops services,
      option for quickly configuring a minimal service lineup, among
      others). It is not meant to replace chkconfig, just offer similiar
      functionality from a general usability perspective.

   Options:
        --init      Default to only the necessary services
        --secure    Disable all services aside from the basic necessities
                    (good for a secure baseline)
    -h, --help      Show this help screen

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


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

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


%minimal_services = (
   "sshd"         => "1",
   "anacron"      => "1",
   "gpm"          => "1",
   "network"      => "1",
   "syslog"       => "1",
   "crond"        => "1",
   "xfs"          => "1",
   "acpid"        => "1",
   "messagebus"   => "1",
   "haldaemon"    => "1",
   "autofs"       => "1",
   "pcmcia"       => "1",
);

%ignore_service = (
   "halt"         => "1",
   "functions"    => "1",
   "killall"      => "1",
   "single"       => "1",
);

%hide_service = (
   "xinetd"       => "1",
);

chdir("/etc/rc.d/init.d");
@inits = glob ("*");
foreach $i (@inits) {
   $cont = ();
   $active = ();
   $service{"$i"}{DAEMON} = '1';
   open(INIT, "< $i");
   while(<INIT>) {
      chomp;
      if ( $_ =~ /^\s*#\s*chkconfig:\s*([\d|-]+)\s+(\d+)\s+(\d+)/ ) {
         if ( $1 eq '-' ) {
            $service{"$i"}{LEVELS} = "345";
         } else {
            $service{"$i"}{LEVELS} = $1;
         }
         $service{"$i"}{START} = $2;
         $service{"$i"}{STOP} = $3;
      } elsif ( $_ =~ /^\s*#\s*description:\s*(.+)(.)$/ ) {
         if ( $2 eq '\\' ) {
            $cont = 1;
            $service{"$i"}{DESCRIPTION} = "$1";
         } else {
            $cont = ();
            $service{"$i"}{DESCRIPTION} = "$1$2";
         }
      } elsif ( $_ =~ /^\s*#\s*(.+)(.)$/ and $cont ) {
         if ( $2 eq '\\' ) {
            $cont = 1;
            $service{"$i"}{DESCRIPTION} .= "$1";
         } else {
            $cont = ();
            $service{"$i"}{DESCRIPTION} .= "$1$2";
         }
      }
   }
   close INIT;
   for ($n=0; $n <= 6; $n++) {
      @c = glob("/etc/rc.d/rc$n.d/S*$i");
      foreach $c (@c ) {
         $c =~ /\/S\d+(.+)$/;
         if ( $1 eq $i ) {
            $active .= $n;
            last;
         }
      }
   }
   if ( $active ) {
      $service{"$i"}{ACTIVE} = '1';
      $service{"$i"}{DEFACTIVE} = '1';
   } else {
      $service{"$i"}{ACTIVE} = ();
      $service{"$i"}{DEFACTIVE} = ();
   }
}

foreach $i ( sort keys %service ) {
   if ( $ignore_service{$i} ) {
      next;
   }
   if ( ! $service{$i}{LEVELS} ) {
      $service{$i}{LEVELS} = '345';
      $service{$i}{START} = '90';
      $service{$i}{STOP} = '10';
      $service{$i}{DESCRIPTION} = '**Unmanaged service... Assigning defaults**';
   }
   @levels = split(//, $service{$i}{LEVELS} );
   foreach $level ( @levels ) {
      $service{$i}{INIT}{$level} = '1';
   }
   push(@all_service_array, $i);
   if ( $hide_service{$i} ) {
      next;
   }
   push(@service_array, $i);
}

chdir("/etc/xinetd.d/");
@inits = glob ("*");
foreach $i (@inits) {
   $cont = ();
   $active = ();
   $service{"$i"}{XINETD} = '1';
   open(INIT, "< $i");
   while(<INIT>) {
      chomp;
      if ( $_ =~ /^\s*#\s*description:\s*(.+)(.)$/ ) {
         if ( $2 eq '\\' ) {
            $cont = 1;
            $service{"$i"}{DESCRIPTION} = "$1";
         } else {
            $cont = ();
            $service{"$i"}{DESCRIPTION} = "$1$2";
         }
      } elsif ( $_ =~ /^\s*#\s*(.+)(.)$/ and $cont ) {
         if ( $2 eq '\\' ) {
            $cont = 1;
            $service{"$i"}{DESCRIPTION} .= "$1";
         } else {
            $cont = ();
            $service{"$i"}{DESCRIPTION} .= "$1$2";
         }
      } elsif ( $_ =~ /^service\s+(\w+)/ ) {
         $service{"$i"}{SERVICE} = "$1";
         $service{"$i"}{CONFIGURED} = '1';
      } elsif ( $_ =~ /^\s*disable\s*=\s*(.+)$/ ) {
         if ( $1 eq 'yes' ) {
            $service{"$i"}{ACTIVE} = ();
            $service{"$i"}{DEFACTIVE} = ();
         } else {
            $service{"$i"}{ACTIVE} = '1';
            $service{"$i"}{DEFACTIVE} = '1';
         }
      }
   }
   close INIT;
}

foreach $i ( sort keys %service ) {
   if ( $ignore_service{$i}  ) {
      next;
   }
   if ( ! $service{$i}{CONFIGURED} ) {
      next;
   }
   push(@all_service_array, $i);
   if ( $ignore_service{$i}  ) {
      next;
   }
   push(@service_array, $i);
}

@service_array = sort(@service_array);

sub write_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: Configure Runtime Services                                          ");
      $scr->normal();
   }

   $line = 2;
   $count = 1;
   $scr->at($line,1);
   $scr->bold();
   $scr->blue();
   $out=sprintf("  %-2.2s  %-14.14s %-57.57s", "On", "Service", "Description");
   $scr->puts($out);
   $scr->normal();
   $line++;
   $count++;

   $service_count = @service_array;
   if ( $service_count < $show_end ) {
      $show_end = $service_count;
   } elsif ( $select >= $service_count ) {
      $select = $service_count-1;
   } elsif ( $select <= 0 ) {
      $select = 0;
   } elsif ( $select >= $show_end - 1 and $select + 1 ne $service_count ) {
      $show_start++;
      $show_end++;
   } elsif ( $select >= $show_end ) {
      $show_start++;
      $show_end++;
   } elsif ( $select <= $show_start ) {
      $show_start--;
      $show_end--;
   }
   for($c=$show_start;$c < $show_end; $c++) {
      $initscript = $service_array[$c];
      if ( $select == $c ) {
         if ( $service{$initscript}{DAEMON} ) {
            $out = sprintf(" Start Priority: %5.5s,   Stop Priority: %5.5s,   Activated Run Levels: %6.6s", $service{$initscript}{START}, $service{$initscript}{STOP}, $service{$initscript}{LEVELS});
         }elsif ( $service{$initscript}{XINETD} ) {
            $out = sprintf("Xinetd managed process: %s", $service{$initscript}{SERVICE});
         }
         &info_message($scr, $out);
         $scr->at($line,$col);
         $scr->reverse();
         $scr->blue();
         $scr->inverse();
         $current_initscript = $initscript;
      } else {
         if ( $service{$initscript}{ACTIVE} ne $service{$initscript}{DEFACTIVE} ) {
            $scr->yellow();
         } elsif ( $service{$initscript}{ACTIVE} ) {
            $scr->green();
         } else {
            $scr->normal();
         }
         $scr->at($line,$col);
      }
      if ( $service{$initscript}{ACTIVE} ) {
         $active = "on";
      } else {
         $active = "";
      }
      $out=sprintf("  [%-2.2s] %-14.14s %-57.57s", $active, $initscript, $service{$initscript}{DESCRIPTION});
      $scr->puts($out);
      $scr->normal();
      $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);
}


if ( $init ) {
   foreach $initscript ( keys %service ) {
      if ( $minimal_services{$initscript} ) {
         $service{$initscript}{ACTIVE} = '1';
         $service{$initscript}{DEFACTIVE} = '1';
         $service{$initscript}{ACTION} = 'start';
      } else {
         $service{$initscript}{ACTIVE} = ();
         $service{$initscript}{DEFACTIVE} = ();
         $service{$initscript}{ACTION} = 'stop';
      }
   }
}
if ( $secure ) {
   foreach $initscript ( keys %service ) {
      if ( $minimal_services{$initscript} ) {
         $service{$initscript}{ACTIVE} = '1';
         $service{$initscript}{ACTION} = 'start';
      } else {
         $service{$initscript}{ACTIVE} = ();
         $service{$initscript}{ACTION} = 'stop';
      }
   }
}


$show_start = 0;
$show_end = 17;
$select = 0;
while(1){
   $total = &write_page($select);
   $scr->raw();
   $scr->flush_input();
   $key = $scr->getch();
   if ( $key =~ /kd/ or $key =~ /        / ) {
      $select++;
   } elsif ( $key eq 'ku' ) {
      $select--;
   } elsif ( $key eq '
' or $key eq ' ' or $key eq '   ' or $key eq 'kr' ) {
      $scr->normal();
      if ( $service{$current_initscript}{ACTIVE} ) {
         $service{$current_initscript}{ACTIVE} = ();
         $service{$current_initscript}{ACTION} = 'stop';
      } else {
         $service{$current_initscript}{ACTIVE} = '1';
         $service{$current_initscript}{ACTION} = 'start';
      }

   } elsif ( $key eq 'd' or $key eq 'kl' ) {
      $inst = '1';
      last;
   } elsif ( $key eq 'q' or $key eq '' ) {
      last;
   } else {
   }
}

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

foreach $service ( keys %service ) {
   if ( $service{$service}{XINETD} ) {
      if ( $service{$service}{ACTIVE} ) {
         $need_xinetd = '1';
      }
      if ( ( $service{$service}{ACTIVE} ne $service{$service}{DEFACTIVE} ) or $init ) {
         if ( $service{$service}{ACTIVE} ) {
            $restart_xinetd = '1';
            $out = ();
            open(CONF, "< /etc/xinetd.d/$service");
            while(<CONF>) {
               chomp;
               if ( $_ =~ /^(\s*disable\s*=\s*).+$/ ) {
                  $out .= "$1no\n";
               } else {
                  $out .= "$_\n";
               }
            }
            close CONF;
            open(CONF, "> /etc/xinetd.d/$service");
            print CONF $out;
            close CONF;
         } else {
            $out = ();
            open(CONF, "< /etc/xinetd.d/$service");
            while(<CONF>) {
               chomp;
               if ( $_ =~ /^(\s*disable\s*=\s*).+$/ ) {
                  $out .= "$1yes\n";
               } else {
                  $out .= "$_\n";
               }
            }
            close CONF;
            open(CONF, "> /etc/xinetd.d/$service");
            print CONF $out;
            close CONF;
         }
      }
   }
}

if ( $restart_xinetd ) {
   if ( $service{xinetd}{ACTIVE} ) {
      $service{xinetd}{DEFACTIVE} = ();
      $service{xinetd}{ACTION} = 'restart';
   } else {
      $service{xinetd}{ACTIVE} = '1';
      $service{xinetd}{ACTION} = 'start';
   }
} elsif ( ! $need_xinetd ) {
   $service{xinetd}{ACTIVE} = ();
   $service{xinetd}{ACTION} = 'stop';
}

foreach $service ( @all_service_array ) {
   if ( ( $service{$service}{ACTIVE} ne $service{$service}{DEFACTIVE} ) or $init ) {
      if ( $service{$service}{DAEMON} ) {
         @scripts = glob("/etc/rc.d/rc*.d/*$service");
         foreach $script ( @scripts ) {
            unlink("$script");
         }
         for ($i=0; $i <= 6; $i++) {
            if ( $service{$service}{INIT}{$i} and $service{$service}{ACTIVE} and $service{$service}{START} ) {
               symlink("../init.d/$service", "/etc/rc.d/rc$i.d/S$service{$service}{START}$service");
            } elsif ( $service{$service}{STOP} ) {
               symlink("../init.d/$service", "/etc/rc.d/rc$i.d/K$service{$service}{STOP}$service");
            }
         }
         if ( ! $init ) {
            if ( $service{$service}{ACTION} eq 'start' ) {
               system("/sbin/service $service start");
               $exit_code += $?;
            } elsif ( $service{$service}{ACTION} eq 'restart' ) {
               system("/sbin/service $service restart");
               $exit_code += $?;
            } elsif ( $service{$service}{ACTION} eq 'stop' ) {
               system("/sbin/service $service stop");
               $exit_code += $?;
            }
         }
      }
   }
}

exit($exit_code);
