#!/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 Sidekick;
use Getopt::Long;
use Sys::Hostname;

GetOptions(
   'init' => \$init,
);

if ( $> != '0' ) {
   die "You need to run this as root!\n";
}

my $pci_list = "/etc/sysconfig/pcilist";
my $modprobe_conf = "/etc/modprobe.conf";

sub probePCI {
   my %ret;
   my $addr;
   my $pciid;
   my $class;
   my $kmod;
   open(HW, "/sbin/detect -ai |");
   while (<HW>) {
      chomp;
      ($addr, $pciid, $class, $kmod) = split(/\s/, $_);
      $ret{$addr}{CLASS} = $class;
      $ret{$addr}{PCIID} = $pciid;
      $ret{$addr}{KMOD} = $kmod;
      $ret{$addr}{DEFINED} = "1";
   }
   close HW;
   return %ret;
}

sub listPCI {
   my %ret;
   my $addr;
   my $pciid;
   my $class;
   my $kmod;
   open(HW, "$pci_list");
   while (<HW>) {
      chomp;
      ($addr, $pciid, $class, $kmod) = split(/\s/, $_);
      $ret{$addr}{CLASS} = $class;
      $ret{$addr}{PCIID} = $pciid;
      $ret{$addr}{KMOD} = $kmod;
      $ret{$addr}{DEFINED} = "1";
   }
   close HW;
   return %ret;
}

sub listModprobeAliases {
   my %ret;
   my $alias;
   my $dev;
   my $kmod;
   open(MODPROBE, "$modprobe_conf");
   while (<MODPROBE>) {
      chomp;
      ($alias, $dev, $kmod) = split(/\s+/, $_);
      if ( $alias eq "alias" ) {
         $ret{$dev} = $kmod;
      }
   }
   close MODPROBE;
   return %ret;
}

sub removedPCI {
   my %lastPCI = &listPCI();
   my %currentPCI = &probePCI();
   my $addr;
   my %ret;
   foreach $addr (keys %lastPCI ) {
      if ( $currentPCI{$addr}{PCIID} ne $lastPCI{$addr}{PCIID} ) {
         %{$ret{$addr}} = %{$lastPCI{$addr}};
      }
   }
   return %ret;
}

sub addedPCI {
   my %lastPCI = &listPCI();
   my %currentPCI = &probePCI();
   my $addr;
   my %ret;
   foreach $addr (keys %currentPCI ) {
      if ( $currentPCI{$addr}{PCIID} ne $lastPCI{$addr}{PCIID} ) {
         %{$ret{$addr}} = %{$currentPCI{$addr}};
      }
   }
   return %ret;
}

sub getDeviceString {
   my ($id, @NULL) = @_;
   my ($venID, $devID) = split(/:/, $id);
   my $vendor;
   my $device;
   my $kmod;
   my $string;
   my $ret;
   open(PCITABLE, "/usr/share/hwdata/pcitable");
   while(<PCITABLE>) {
      chomp;
      ( $vendor, $device, $kmod, $string ) = split(/\t/, $_);
      if ( $vendor eq "0x$venID" and $device eq "0x$devID" ) {
         $ret = $string;
         last;
      }
   }
   close PCITABLE;
   $ret =~ s/^"(.+)"$/$1/;
   return $ret;
}

sub updateHWConfs {
   my %pci = &probePCI();
   my $addr;
   my $pcic;
   open(PCILIST, "> $pci_list");
   foreach $addr ( keys %pci ) {
      print PCILIST "$addr $pci{$addr}{PCIID} $pci{$addr}{CLASS} $pci{$addr}{KMOD}\n";
      if ( $pci{$addr}{CLASS} eq "PCMCIA" ) {
         $pcic = "$pci{$addr}{KMOD}";
      }
   }
   close PCILIST;
   open(PCMCIA,"> /etc/sysconfig/pcmcia");
   if ( $pcic ) {
      print PCMCIA "PCMCIA=yes\n";
      print PCMCIA "PCIC=$pcic\n";
      system("/sbin/chkconfig pcmcia on");
   } else {
      print PCMCIA "PCMCIA=no\n";
      print PCMCIA "PCIC=\n";
   }
   print PCMCIA "PCIC_OPTS=\n";
   print PCMCIA "CORE_OPTS=\n";
   print PCMCIA "CARDMGR_OPTS=-f\n";
   close PCMCIA;
   system("/sbin/modconf-generator > $modprobe_conf");

   return;
}

sub loadModules {
   my %aliases = &listModprobeAliases();
   my $alias;
   foreach $alias ( sort keys %aliases ) {
      system("modprobe $aliases{$alias} 2>/dev/null");
   }
   system("/sbin/service pcmcia restart >/dev/null 2>&1");
}



$scr = new Term::ScreenColor;
   unless ($scr) { die "Ack! Couldn't get control of the screen!\n"; }
$scr->noecho();
$count = "0";
$scr->clrscr();

#$scr->at(0,0);
#$scr->puts("Hardware Configuration Probe");
$scr->on_blue();
$scr->white();
$scr->puts("cAos Linux: Hardware Configuration Probe                                        ");
$scr->normal();


my %addedPCI = &addedPCI();
if ( %addedPCI ) {
   $count += 2;
   $scr->at($count,0);
   $scr->puts("Hardware found:");
#   print "\nHardware Found:\n";
   $count++;
   foreach $addr ( keys %addedPCI ) {
      $devString = &getDeviceString($addedPCI{$addr}{PCIID});
      if ( $devString ) {
         $scr->at($count,3);
         $out = sprintf("%-76.76s", "$addr/$devString");
         $scr->puts($out);
#         printf("   %-76.76s\n", "$addr/$devString");
         $hardware_changed = '1';
         $count++;
      }
   }
   $count++;
}
my %removedPCI = &removedPCI();
if ( %removedPCI ) {
   $count += 2;
   $scr->at($count,0);
   $scr->puts("Hardware removed:");
#   print "\nHardware Removed:\n";
   $count++;
   foreach $addr ( keys %removedPCI ) {
      $devString = &getDeviceString($removedPCI{$addr}{PCIID});
      if ( $devString ) {
         $scr->at($count,3);
         $out = sprintf("%-76.76s", "$addr/$devString");
         $scr->puts($out);
#         printf("   %-76.76s\n", "$addr/$devString");
         $hardware_changed = '1';
         $count++;
      }
   }
   $count++;
}

print "\n\n";

#$scr = new Term::Screen;
#   unless ($scr) { die "Ack! Couldn't get control of the screen!\n"; }
#my $rows = $scr->rows();
#$scr->noecho();

if ( $hardware_changed ) {
   $count += 2;
   while (1) {
      $scr->at($count,0);
      if ( $init ) {
         &updateHWConfs();
         &loadModules();
         open(PCMCIA, "/etc/sysconfig/pcmcia");
         while(<PCMCIA>){
            chomp;
            if ( $_ eq "PCMCIA=yes" ) {
               $scr->puts("PCMCIA devices should now be (re)inserted to initalize");
               $count++;
               last;
            }
         }
         close PCMCIA;
         $scr->at($count+1,0);
         $scr->puts("Press any key to continue... ");
         $scr->flush_input();
         $update = $scr->getch();
         last;
      } else {
         $scr->puts("Do you wish to update the hardware configuration profile? (y/n): ");
         $update = $scr->getch();
         if ( $update eq 'y' or $update eq 'kr' ) {
            $scr->at($count+2,0);
            $scr->puts("Hardware configuration update and initalization in progress...");
            &updateHWConfs();
            &loadModules();
            last;
         } elsif ( $update eq 'n' or $update eq 'kl' or $update eq '' ) {
            last;
         }
      }
   }
} else {
   $count += 2;
   $scr->at($count,0);
   $scr->puts("No new hardware Found... Press any key to continue: ");
   $scr->getch();
}
$scr->cooked();
$scr = ();
print "\n";
