#!/usr/bin/perl
#
# Author: Gustavo Alkmim (alkmim@ic.unicamp.br)
# Chagelog: 
#     Aug 09, 2013 -  First Version
#
# TODO: 
#     Give option to inform list of package to add m-a: foreign. 
#     Give options to override previous m-a information.
#     Give option to add (or not)  m-a: foreign for all arch: all packages.
use strict;
use warnings;
use Dpkg::Control::Info;

my $num_args = $#ARGV + 1;
if ($num_args != 3) {
  print "\nUsage: add-ma-foreign.pl package_name package_version key_to_sign\n";
  exit;
}

# Defining variables
my $package = $ARGV[0];
my $version = $ARGV[1];
my $keysign = $ARGV[2];
my $workdir = "./ma-foreign/";
my $arch = `dpkg-architecture -qDEB_BUILD_ARCH`;
chop($arch);

# 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 sources
$package = `apt-cache showsrc $package | grep "Package: " | cut -d' ' -f2`;
chop($package);
print "\n[INFO] Getting sources of $package=$version\n\n";
@args = ("apt-get", "source", $package . "=" . $version);
$output = system(@args);
print "\n";

# adding m-a: foreign field and warning if m-a field already present
while (grep(m/-/,$version)) { 
   chop($version);
}

my $control = Dpkg::Control::Info->new("$package-$version/debian/control");
my $srcpack = $control->get_source();
print "[INFO] Saving source information in control file\n";
open (FILE, '>control');
print FILE $srcpack;
print FILE "\n";
close (FILE);

my @binpacks = $control->get_packages();
my $binindex = 0;
foreach my $binary (@binpacks) {
   my @lines = split(/^/m, $binary);
   my $multiarch = "";
   my $package = "";
   my $architecture = "";
   foreach my $line (@lines) {
      $package = ( grep(m/Package: /, $line) ? $line : $package);
      $architecture = ( grep(m/Architecture: /, $line) ? $line : $architecture);
      $multiarch = ( grep(m/Multi-Arch: /, $line) ? $line : $multiarch);
   }

   chop $package;
   print "[INFO] Checking " . $package . "...\n";
   if (grep(m/Multi-Arch/,$multiarch)) {
      print "[INFO] Multi-Arch field already exists in binary $package... keeping original binary information\n";
   } elsif (grep(! m/all/,$architecture)) {
      print "[INFO] Binary $package is not Architecture: all... keeping original binary information\n";
   } else {
      print "[INFO] Changing binary information\n";
      splice(@lines,2,0,"Multi-Arch: foreign\n");
   }
   print "[INFO] Saving binary information in control file\n";
   open (FILE, '>>control');
   print FILE @lines;
   print FILE "\n";
   close (FILE);      
}
@args = ("mv", "control", "$package-$version/debian/control");
$output = system(@args);

# Bumping version and generating new source package
$path = $path . "/" . "$package-$version/";
chdir($path);
@args = ("dch", "-i", "Added 'Multi-Arch: foreign' in the control file");
$output = system(@args);
@args = ("dch", "-r", "\"\"");
$output = system(@args);
$output = `dpkg-buildpackage -S -uc -us -sa -d`;
#print ">>>>>> Output \n $output \n >>>>>> \n";
# Getting the generated .dsc filename
my @output = split(/\s/m, $output);
my @newdscfile = grep(m/$package.*\.dsc/, @output);
#print ">>>>>> newdscfile \n @newdscfile \n >>>>>> \n";

# Generating patch
chdir("../");
my $complete_version = `apt-cache showsrc $package | grep "Version: " | grep -v -e Standards | cut -d' ' -f2`;
chop($complete_version);
my $olddscfile = $package . "_" . $complete_version . "\.dsc";
@args = ("mkdir", "-p", "../patches/");
$output = system(@args);
#print ">> old = $olddscfile \n >> new = $newdscfile[0] \n";

$output = `debdiff $olddscfile $newdscfile[0]`;
open (FILE, ">../patches/$package-$version.patch");
print FILE $output;
close (FILE);

# Bulding new package
@args = ("sbuild", "-A", "-d", "unstable", $newdscfile[0]);
$output = system(@args);


# Uploading
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);
my $new_version = $newdscfile[0];
chop($new_version);
chop($new_version);
chop($new_version);
chop($new_version);
print "Signing $new_version" . "_" . $arch . "\.changes\n";
@args = ("debsign", "-k", "$keysign" , $new_version . "_" . $arch . "\.changes");
$output = system(@args);
print "Uploading $new_version" . "_" . $arch . "\.changes\n";
@args = ("dupload", $new_version . "_" . $arch . "\.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 $new_version\_$arch\.changes in reprepro\n";
`ssh 127.0.1.1 reprepro -b "~alkmim/public_html/debianrepo/" processincoming to_teste $new_version\_$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`;

