#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils 'any';
use Getopt::Long;

# Printing help information
if (any { /-h/ || /--help/ } @ARGV) {
   print_help();
   exit;
} 

# Calling the menu mode
if (any { /-m/ || /--menu/ } @ARGV) {
   menu_mode();
} 

# Calling the config mode using a configuration file
if (any { /-c/ || /--configfile/ } @ARGV) {
   config_mode();
} 

# entering normal mode
normal_mode();

sub print_help {
   print "Automated Bootstrap Tool\n";
   print "Developped and Maintained by Gustavo Prado Alkmim\n";
   print "mail: alkmim\@ic\.unicamp\.br\n";
   print "\n";
   print "Usage: \n";
}

sub menu_mode {

}

sub config_mode {
   # parsing configuration file
   print "Parsing configuration file\n";
   my @parameters;
   # calling normal mode with parsed parameters
    print "Calling normal mode with parsed parameters\n";
   normal_mode(@parameters);
}

sub normal_mode {
#   my $num_args = $#ARGV + 1;
#   if ($num_args != 4) {
#      print "\nUsage 1: add-ma-foreign.pl buildarch hostarch repository botchdir\n";
#      print "\nUsage 2: menu mode\n";
#      print "\nUsage 3: config file\n";
#      exit;
#   }

   # Parsing options
#   print "Parsing options";
#   parse_options();

   my $buildarch = $ARGV[0];
   my $hostarch = $ARGV[1];
   my $botch_dir = $ARGV[2];
   my $repository = $ARGV[3];
   my $suite = $ARGV[4];
   my $component = $ARGV[5];
#   my $sources_list = $ARGV[6];
   my $chroot = $ARGV[6];
   my $snapshot_dist = $ARGV[7];
   my $bootstrapped_dist = $ARGV[8];
   my $sources_list = $ARGV[9];
   my $keysign = "158FA584";
   my $workdir = "./workdir/";

   # Creating the workdir and entering on it

   my @args = ("mkdir", "-p", $workdir);
   my $output = system(@args);
   my $path = `pwd`;
   chop $path;
   $path = $path . "/" . $workdir;
   chdir($path);

   # Getting the packages list and the sources list from the repository.
   @args = ("wget", "-c", $repository . "/dists/" . $snapshot_dist . "/main/" . "binary-$buildarch" . "/Packages.gz");
   $output = system(@args);
   @args = ("wget", "-c", $repository . "/dists/" . $snapshot_dist . "/main/" . "source" . "/Sources.gz");
   $output = system(@args);
   
   # Running Botch to get the build_order
   #my @args = ("./botch_build_order.sh", $buildarch, $hostarch $buildpackages $sources);
   #my $output = system(@args)
   system("cp", "-vu", "../order1.lst", "./");
   system("cp", "-vu", "../order2.lst", "./");

   # Generating sources.list
#   open(FILE, '>./sources.list');
#   print FILE "deb " . $repository . " " . "$snapshot_dist" . " " . "main\n";
#   print FILE "deb-src " . $repository . " " . "$snapshot_dist" . " " . "main\n";
#   close FILE;
   system("cp", "-vu", "../sources.list", "./");

   # Reading botch cycle free order and (cross)building.
   open(FILE, 'order1.lst');
   foreach my $line (<FILE>) {
      my @packages = split(/ /m, $line);
      splice(@packages, 0, 1);

      foreach my $src (@packages) {
         my $path2 = $path;
         $src = substr $src, 4;
         my $version_complete = `apt-cache showsrc $src | grep "Version: " | grep -v -e Standards | cut -d' ' -f2`;
         chop($version_complete);
         my $version = `apt-cache showsrc $src | grep "Version: " | grep -v -e Standards | cut -d' ' -f2 | cut -d'-' -f1`;
         chop($version);
         my $arch = `apt-cache showsrc $src | grep "Architecture: " | cut -d' ' -f2 `;
         chop($arch);
#         print "\n\nAction: " . "apt-get" . " source" .  " -o" . " Dir::Etc::SourceList=\"./sources.list\" " . $src . "\n\n";
         my $output = system("apt-get", "source", "-o", "Dir::Etc::sourcelist=sources.list", $src);
#         print "path antes: $path2 \n";
#         $path2 = $path2 . "/" . "$src-$version";
#         print "src: $src \n";
#         print "version: $version \n";
#         print "path2: $path2 \n";
#         chop($path2);
#         chdir($path2);
          $output = system("sbuild", "-A", "--build=$buildarch", "--host=$hostarch", "-c", "$chroot", "-d", "unstable", "$src\_$version_complete.dsc");
#         $path2 = $path2 . "/../";
#         chdir($path2);
#         $output = system("dupload", "-f", -c "./config_file", "$(src)_$(version_complete)_$(arch).changes", "> /dev/null 2>&1")
         # uploading packages to the repository
         print "Writing dupload configuration file";
         open (FILE, ">./dupload.conf");
         print FILE "package config;\n";
         print FILE "\$preupload{'changes'} = '/usr/share/dupload/gpg-check %1';\n";
         print FILE "\$cfg{'destination'} = {\n";
         print FILE "        fqdn => '127.0.1.1',\n";
         print FILE "        method => \"scpb\",\n";
         print FILE "        incoming => \"~alkmim/public_html/debianrepo/incoming/\",\n";
         print FILE "};\n";
         print FILE "1;\n";
         close (FILE);
         print "Signing $version_complete" . "_" . $buildarch . "\.changes\n";
         @args = ("debsign", "-k", "$keysign" , $src . "_" . $version_complete . "_" . $buildarch . "\.changes");
         $output = system(@args);
         print "Uploading $src\_$version_complete" . "_" . $buildarch . "\.changes\n";
         @args = ("dupload", $src . "_" . $version_complete . "_" . $buildarch . "\.changes", "-c", "-t", "destination", "--nomail");
         $output = system(@args);
#         print "Signing $new_version" . "_" . "source" . "\.changes\n";
#         @args = ("debsign", "-k", "$keysign" , $new_version . "_" . "source" . "\.changes");
#         $output = system(@args);
#         print "Uploading $new_version" . "_" . "source" . "\.changes\n";
#         @args = ("dupload", $new_version . "_" . "source" . "\.changes", "-c", "-t", "destination", "--nomail");
#         $output = system(@args);
         print "Processing incoming $version_complete\_$arch\.changes in reprepro\n";
         `ssh 127.0.1.1 reprepro -b "~alkmim/public_html/debianrepo/" processincoming to_teste $version_complete\_$arch\.changes`;
#         print "Processing incoming $new_version\_source\.changes in reprepro\n";
#         `ssh 127.0.1.1 reprepro -b "~alkmim/public_html/debianrepo/" processincoming to_teste $new_version\_source\.changes`;
      }
   }
}
