#!/usr/bin/perl

# Copyright (c) 2008  Krzysztof Suchomski
#
# Based on Jim Weirich rake tool
#
# License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#++

our $VERSION = '0.21';

use strict;
use warnings;
no warnings qw(misc);


use Pake::Application;
use Getopt::Std;
use Pake::Syntax;
use Pake::TestLib::TestTask;

my %opts;

#Reding options and passing them to the Pake::Application
getopts('hHTtVDr:d:f:',\%opts);
my $file = "Pakefile";
$file=$opts{"f"} if $opts{"f"};

Pake::Application::Pakefile($file);

Pake::Application::options(\%opts);
undef %opts;

#Showing help -h switch
if(Pake::Application::options()->{"h"} || Pake::Application::options()->{"H"}){    
    Pake::Application::usage();
    exit 0;
}

if(Pake::Application::options()->{"V"}){
    print "Pake VERSION $VERSION\n";
}

#Changing working directory -d switch
if(Pake::Application::options()->{"d"}){
    unless(chdir(Pake::Application::options()->{"d"})){
	print "Cannot change the working directory to: "
	    . Pake::Application::options()->{"d"} . "\n"; 

	exit;
    }
}

#Loading Pakefile
pake_dependency(Pake::Application::Pakefile());

if(Pake::Application::options()->{"r"}){
    print "Pake file script loaded properly\n";
    exit 0;
}

#priniting all tasks with descriptions -T -t switches
if(Pake::Application::options()->{"T"} || Pake::Application::options()->{"t"}){
    Pake::Application::printTasks();
    exit 0;
}

#priniting all tasks with descriptions -D switch
if(Pake::Application::options()->{"D"}){
    Pake::Application::printDeps();
    exit 0;
}

#No tasks in pake arguments
unless(@ARGV){
    # run default task if exists
    if(Pake::Application::Env()->{"default"}){
	Pake::Application::runTask(Pake::Application::Env()->{"default"});
    } else {
	print "No tasks where specified\n";
    }
}

#running all task specified in args
foreach my $arg (@ARGV){
    Pake::Application::runTask($arg);
}


1;

__END__

=head1 NAME

    pake 

=head1 AUTHOR

Krzysztof Suchomski

    Contact:
    krzysztof.suchomski at gmail dot com

=head1 SYNOPSIS

Calling program:

    pake [options] [tasks]

=head1 DESCRIPTION

Pakefile is a script where you define tasks and their dependencies. Within the Pakefile you can access any previosuly defined object using Pake::Application 

pake program run task specified in the command line and executes it with dependencies defined in Pakefile.

pake comes with predefined tasks for file and directory manipulations and for running tests. For details refer to other Modules

Pakefile script is a normal perl scipt, by default executed with:

	use strict;
	use warnings;

=head1 OPTIONS
    
    -T, prints all tasks with descriptions defined in the Pakefile 
    -f FILE, changes the script file name to the specified, default Pakefile
    -h, print help contents
    -d DIR, changes the working directory
    -D,  prints all tasks with dependencies
    -r, Dry run, executes Pakefile to check if script is properly formed
    -V, prints pake version

=head1 EXAMPLE PAKEFILE

	#default task
  	default "test2";
	
	desc "task simply prints test";
	task {
		print "test\n";
	} "test";
	
	desc "task test1 depends on test"
	task {
		print "test1\n";
	} "test1" => ["test"];
	
	desc "task test2 depends on test and test1";
	task {
		print "test2\n";
	}

	#run: pake src/com/test
	desc "Directory Task, it will be executed only if directory does not exists";
	directory{
	
	} "src/com/test";
	
	#file dependencies are executed only if the files created by tasks changed
	file {} "biblioteka.c" => [];
	
	file {
		`gcc -c biblioteka.c`
	} "biblioteka.o" => ["src/com/test","biblioteka.c"];
	
	file {
	    `gcc -o program program.c biblioteka.o`
	} "program" => ["biblioteka.o"];
	
	#End script with something true
	1;

=cut
