#!/usr/local/bin/perl

eval 'exec /usr/local/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# Author: Emanuel Haupt <ehaupt@FreeBSD.org>
#
# $Id: distilator.pl,v 1.4 2010/10/19 09:25:11 ehaupt Exp $
#

use warnings;
use strict;

use FreeBSD::Ports::Distilator;
use Getopt::Long;

sub main() {
	my %opt;
	GetOptions(\%opt, 'help', 'threads=i', 'timeout=i', 'version');

	my $distilator=FreeBSD::Ports::Distilator->new();

	if(exists($opt{'threads'}) && $opt{'threads'} > 0) {
		$distilator->max_threads($opt{'threads'});
	}

	if(exists($opt{'timeout'})) {
		$distilator->timeout($opt{'timeout'});
	}

	if(exists($opt{'help'})) {
		$distilator->usage();
		exit 0;
	}

	if(exists($opt{'version'})) {
		printf("%s\n", $distilator->version());
		exit 0;
	}

	if(defined($ARGV[0])) {
		my $port=$ARGV[0];
		chomp($port);
		if(-d $port) {
			$distilator->check($port);
		} else {
			printf("%s: port does not exist.\n", $port);
			exit 1;
		}
	} else {
		$distilator->usage();
		exit 1;
	}
}

main();
