#!/usr/bin/perl -w
## pocc-checkout for pocc in /Users/pouchet
##
## Made by Louis-Noel Pouchet
## Contact: <louis-noel.pouchet@inria.fr>
##
## Started on  Tue Apr 14 17:43:47 2009 Louis-Noel Pouchet
## Last update Tue Jul 12 21:28:20 2011 Louis-Noel Pouchet
##
#
# pocc-checkout: this file is part of the PoCC project.
#
# PoCC, the Polyhedral Compiler Collection package
#
# Copyright (C) 2009 Louis-Noel Pouchet
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# The complete GNU General Public Licence Notice can be found as the
# `COPYING.LESSER' file in the root directory.
#
# Author:
# Louis-Noel Pouchet <Louis-Noel.Pouchet@inria.fr>
#

use Switch;
use Term::ANSIColor;

## Some global variables.
$CONFIG_DATA = "";
$CONFIG_DRIVER = "";
$BDIR_CO = ".";
$MODE = "";


## Args: soft name, destination directory, url
sub checkout_git
{
    my ($soft, $dir, $url) = @_;
    my @urls = split(/@/, $url);
    $url = $urls[0];
    my $revision = "";
    if ($#urls >= 1) {
	$revision = $urls[1];
    }
    print "[PoCC-checkout] Git cloning/updating $soft (from $url) in $dir...\n";
    system("if [ -d \"$BDIR_CO/$dir/$soft/.git\" ]; then cd $BDIR_CO/$dir/$soft && git pull && if ! [ -z \"$revision\" ]; then git checkout $revision; fi; else git clone $url $BDIR_CO/$dir/$soft; if ! [ -z \"$revision\" ]; then cd $BDIR_CO/$dir/$soft && git checkout $revision; cd -; fi; fi");
}

## Args: soft name, destination directory, url
sub checkdiff_git
{
    my ($soft, $dir, $url) = @_;
    print "[PoCC-checkout] Git diff $soft:\n";
    system("cd $BDIR_CO/$dir/$soft && git diff | grep 'diff --git'");
}


## Args: soft name, destination directory, url
sub checkout_wget
{
    my ($soft, $dir, $url) = @_;
    print "[PoCC-checkout] Wgetting $soft (from $url) in $dir...\n";
    system("mkdir -p $BDIR_CO/$dir && cd $BDIR_CO/$dir && wget -N $url");
    my @tmp = split(/\//, $url);
    my $filename = $tmp[$#tmp];
    if ($filename =~ /.tar.gz$|.tgz$/) {
	system("cd $BDIR_CO/$dir && tar xzf $filename");
	$filename =~ s/.tar.gz$//g;
	$filename =~ s/.tgz$//g;
    }
    else {
	if ($filename =~ /.zip$/) {
	    system("cd $BDIR_CO/$dir && unzip $filename");
	    $filename =~ s/.zip$//g;
	}
    }
    system("cd $BDIR_CO/$dir && mv $filename $soft");
}


## Args: soft name, destination directory, url
sub checkout_local
{
    my ($soft, $dir, $url) = @_;
    print "[PoCC-checkout] Copying $soft (from $url) in $dir...\n";
    system("mkdir -p $BDIR_CO/$dir && cp $url $BDIR_CO/$dir");
    my @tmp = split(/\//, $url);
    my $filename = $tmp[$#tmp];
    if ($filename =~ /.tar.gz$|.tgz$/) {
	system("cd $BDIR_CO/$dir && tar xzf $filename");
	$filename =~ s/.tar.gz$//g;
	$filename =~ s/.tgz$//g;
    }
    else {
	if ($filename =~ /.zip$/) {
	    system("cd $BDIR_CO/$dir && unzip $filename");
	    $filename =~ s/.zip$//g;
	}
    }
    system("cd $BDIR_CO/$dir && mv $filename $soft");
}


## Args: soft name, destination directory, url, mode
sub checkout_svn
{
    my ($soft, $dir, $url, $mode) = @_;
    my $svnuser = "";
    $svnuser = "--username lppcc --password lppcc" if ($mode =~ /base/);
    print "[PoCC-checkout] SVN checkouting $soft (from $url) in $dir...\n";
    my $svn_options = "";
    $svn_options = $ENV{'SVN_OPTIONS'} if (defined($ENV{'SVN_OPTIONS'}));
    system("svn co $svnuser $svn_options $url $BDIR_CO/$dir/$soft");
}

sub checkdiff_svn
{
    my ($soft, $dir, $url, $mode) = @_;
    print "[PoCC-checkout] SVN diff $soft:\n";
    my $svn_options = "";
    $svn_options = $ENV{'SVN_OPTIONS'} if (defined($ENV{'SVN_OPTIONS'}));
    system("svn $svn_options diff $BDIR_CO/$dir/$soft | grep Index");
}


## Args: destination directory (category), soft name, retrieve-method, location
sub checkout_soft
{
    my ($category, $soft, $method, $link) = @_;
    $category =~ s/\s//g;
    checkout_svn ($soft, $category, $link, $MODE) if ($method =~ /svn/);
    checkout_git ($soft, $category, $link) if ($method =~ /git/);
    checkout_wget ($soft, $category, $link) if ($method =~ /wget/);
    checkout_local ($soft, $category, $link) if ($method =~ /local/);
}


## Args: destination directory (category), soft name, retrieve-method, location
sub checkdiff_soft
{
    my ($category, $soft, $method, $link) = @_;
    $category =~ s/\s//g;
    if ($method =~ /svn/) {
	checkdiff_svn ($soft, $category, $link, $MODE);
    }
    else { 
	if ($method =~ /git/) {
	    checkdiff_git ($soft, $category, $link);
	}
	else {
	    print "[PoCC-check-diff] Unsupported software. Only SVN and git repository are handled\n";
	}
    }
}


## Args: checkout method
sub checkout
{
    my ($basedir, $repos) = @_;
    $BDIR_CO = $basedir;
    $CONFIG_DATA = "$BDIR_CO/config/configure.cfg";
    $CONFIG_DRIVER = "$BDIR_CO/config/driver.cfg";
    print (colored ("[PoCC-checkout] Checkout of PoCC. This may take a while...", "green green"), "\n");
    print "[PoCC-checkout] Checkout: $repos\n";
    my %config_data = read_config_file($CONFIG_DATA);
    my %config_driver = read_config_file($CONFIG_DRIVER);
    $MODE = $config_driver{"pocc-driver"}{"configuration"};;
    if ($repos eq "all") {
	for $soft (keys %config_data) {
	    checkout_soft($config_data{$soft}{"category"}, $soft,
			  $config_data{$soft}{"retrieve-method"},
			  $config_data{$soft}{"location"});
	}
    }
    else {
	if (! defined($config_data{$repos})) {
	    die "Unkown software $soft. Please update config/configure.cfg\n";
	}
	checkout_soft($config_data{$repos}{"category"}, $repos,
		      $config_data{$repos}{"retrieve-method"},
		      $config_data{$repos}{"location"});
    }
    print (colored ("[PoCC-checkout] Done", "green green"), "\n");
}


## Args: checkdiff method
sub checkdiff
{
    my ($basedir, $repos) = @_;
    $BDIR_CO = $basedir;
    $CONFIG_DATA = "$BDIR_CO/config/configure.cfg";
    $CONFIG_DRIVER = "$BDIR_CO/config/driver.cfg";
    print (colored ("[PoCC-check-diff] Check edits of PoCC module(s). This may take a while...", "green green"), "\n");
    my %config_data = read_config_file($CONFIG_DATA);
    my %config_driver = read_config_file($CONFIG_DRIVER);
    $MODE = $config_driver{"pocc-driver"}{"configuration"};;
    if ($repos eq "all") {
	for $soft (keys %config_data) {
	    checkdiff_soft($config_data{$soft}{"category"}, $soft,
			   $config_data{$soft}{"retrieve-method"},
			   $config_data{$soft}{"location"});
	}
    }
    else {
	if (! defined($config_data{$repos})) {
	    die "Unkown software $soft. Please update config/configure.cfg\n";
	}
	checkdiff_soft($config_data{$repos}{"category"}, $repos,
		      $config_data{$repos}{"retrieve-method"},
		      $config_data{$repos}{"location"});
    }
    print (colored ("[PoCC-check-diff] Done", "green green"), "\n");
}

