#!/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:
      configure-mirror is used to define the package location that should be
      used to obtain new packages and updates.

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

my $yum_conf = "/etc/yum.conf";
my $yum_conf_d = "/etc/yum.repos.d/";
my $default_mirror = "http://mirror.caosity.org/cAos-2";

my $yumconf_tpl = '[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
pkgpolicy=newest
distroverpkg=caos-release
installonlypkgs=linux linux-smp kernel kernel-smp
exactarch=1
';

sub write_yumconf {
   my ( $mirror, $repo, @NULL ) = @_;
   if ( $init or ! -f "/etc/yum.conf" ) {
      open(YUM, "> $yum_conf");
      print YUM $yumconf_tpl;
      close YUM;
   }
   open(CAOSYUM, "> $yum_conf_d/caos-2-core.repo");
   print CAOSYUM "[core]\n";
   print CAOSYUM "enabled=1\n";
   print CAOSYUM "name=cAos-2 core system repository\n";
   print CAOSYUM "baseurl=$mirror/core/\$basearch\n";
   close CAOSYUM;
   open(CAOSYUM, "> $yum_conf_d/caos-2-stable.repo");
   print CAOSYUM "[stable]\n";
   if ( $repo eq 'stable' or ! $repo ) {
      print CAOSYUM "enabled=1\n";
   } else {
      print CAOSYUM "enabled=0\n";
   }
   print CAOSYUM "name=cAos-2 extended stable system repository\n";
   print CAOSYUM "baseurl=$mirror/ext/stable/\$basearch\n";
   close CAOSYUM;
   open(CAOSYUM, "> $yum_conf_d/caos-2-current.repo");
   print CAOSYUM "[current]\n";
   if ( $repo eq 'current' ) {
      print CAOSYUM "enabled=1\n";
   } else {
      print CAOSYUM "enabled=0\n";
   }
   print CAOSYUM "name=cAos-2 extended current system repository\n";
   print CAOSYUM "baseurl=$mirror/ext/current/\$basearch\n";
   close CAOSYUM;
   open(CAOSYUM, "> $yum_conf_d/caos-2-testing.repo");
   print CAOSYUM "[testing]\n";
   if ( $repo eq 'testing' ) {
      print CAOSYUM "enabled=1\n";
   } else {
      print CAOSYUM "enabled=0\n";
   }
   print CAOSYUM "name=cAos-2 extended testing system repository\n";
   print CAOSYUM "baseurl=$mirror/ext/testing/\$basearch\n";
   close CAOSYUM;
   open(FSTAB, "< /etc/fstab");
   while(<FSTAB>) {
      chomp;
      if ( $_ =~ /(\/media\/[a-zA-Z0-9_]+)/ ) {
         push(@media_repos, $1);
      }
   }
   close FSTAB;
   open(CAOSYUM, "> $yum_conf_d/localmedia.repo");
   print CAOSYUM "[localmedia]\n";
   print CAOSYUM "name=Local media repository\n";
   print CAOSYUM "enabled=0\n";
   foreach (@media_repos) {
      if ( $done_first ) {
         print CAOSYUM "        file://$_/RPMS\n";
      } else {
         print CAOSYUM "baseurl=file://$_/RPMS\n";
         $done_first=1;
      }
   }
   close CAOSYUM;

   &info_message($scr, "Updated the YUM configuration files",2);
   $scr->at($line+1,0);
   $scr->clreol();
   $scr->bold();
   $scr->cyan();
   $scr->puts("Using $mirror ('$repo' repository)");
   $scr->normal();
   $scr->at($line+2,0);
   $scr->clreol();
   $scr->at($line+3,0);
   $scr->clreol();
   $scr->puts("Done. Press any key to continue...");
   $scr->getch();
}

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: Package Mirror Location                                             ");
   $scr->normal();

   $line = 2;
   $count = 1;

   return($count);
}


$total = &write_page($select);
&info_message($scr, "Define the mirror you want to use. (should end up at distro dir)", 1);
$scr->at($line+1,0);
$scr->clreol();
$scr->bold();
$scr->puts("URL to mirror:");
$scr->at($line+2,0);
$scr->clreol();
$scr->puts("> ");
$scr->normal();
$mirror = &get_str($scr,$line+2,3,'a-zA-Z0-9.:/\-_', $default_mirror);
$repo = 'stable';
while (1) {
   &info_message($scr, "Select one of the following: stable, current, testing", 1);
   $scr->at($line+1,0);
   $scr->clreol();
   $scr->bold();
   $scr->puts("Repository to use: ");
   $scr->normal();
   $scr->at($line+2,0);
   $scr->clreol();
   $scr->puts("> ");
   $repo = &get_str($scr,$line+2,3,'a-zA-Z0-9.:/\-_', $repo);
   if ( $repo eq 'stable' or $repo eq 'current' or $repo eq 'testing' ) {
      last;
   }
}
&write_yumconf($mirror, $repo);

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

print "\n";

