#!/usr/bin/perl

# To be run in the dresden ocl directory - will update using cvs, compile and
# install into the local maven repository

use strict;
use Getopt::Long;

my %libs = (
    "stdlib" => { target => "jar.ocl20stdlib", jar => "ocl20stdlib" },
    "injector" => { target => "jar.injector", jar => "ocl20injector" },
    "codegen-java" => { target => "jar.codegen.java", jar => "ocl20codegen-java" },
);

my $cvs = 0;
my $ant = 0;
my $version="1.0-SNAPSHOT";
my $help = 0;

GetOptions(
    "cvs!" => \$cvs,
    "ant!" => \$ant,
    "version=s" => \$version,
    "help!" => \$help
) or die ("Error parsing options: $!");

if($help){
    print "Usage: ocl20_maven_install [--[no]cvs] [--[no]ant] [--version <version>] [--help]\n";
    exit 0;
}

if($ENV{DRESDEN_HOME}){
    print "Changing working directory to \$DRESDEN_HOME = $ENV{DRESDEN_HOME}...\n";
    chdir $ENV{DRESDEN_HOME};
}

die ("You should be in the top-level Dresden OCL directory when executing this script (or set \$DRESDEN_HOME accordingly): $!") if(! -f "build.xml");

if($cvs){
    system("cvs update");
}
foreach my $i (keys %libs){
    if($ant or ! -f "lib/internal/".$libs{$i}{jar}.".jar"){
        system("ant ".$libs{$i}{target});
    }
    system("mvn install:install-file -Dfile=lib/internal/".$libs{$i}{jar}.".jar -DgroupId=tudresden.ocl20 -DartifactId=$i -Dversion=$version -Dpackaging=jar -DgeneratePom=true");
}
