#!/usr/bin/env perl

# vim: tabstop=4 expandtab

###### PACKAGES ######

use Modern::Perl;
use warnings FATAL      => 'all';
use Data::Printer alias => 'pdump';
use File::Basename;
use CLI::Driver;
use Util::Medley;
use File::ShareDir 'dist_file';

use Getopt::Long;
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('pass_through');
Getopt::Long::Configure('no_auto_abbrev');

###### CONSTANTS ######

###### GLOBALS ######

use vars qw(
  $Action
  $CliDriver
);

###### MAIN ######

$| = 1;

$CliDriver = CLI::Driver->new( path => get_cli_driver_path() );

parse_cmd_line();

my $action = $CliDriver->get_action( name => $Action );

if ($action) {
	$action->do;
}
else {
	$CliDriver->fatal("failed to find action in config file");
}

###### END MAIN ######

sub get_cli_driver_path {

	if ( -f "share/cli-driver.yml" ) {

		# local development location
		return "share";
	}

	my $file = dist_file( 'Util-Medley', 'cli-driver.yml' );

	return dirname $file;

	#	my $medley = Util::Medley->new;
	#	my $path   = $INC{"Util/Medley.pm"};
	#	$path =~ s/\.pm$//;

	#	return $path;
}

sub check_required {
	my $opt = shift;
	my $arg = shift;

	print_usage("missing arg $opt") if !$arg;
}

sub parse_cmd_line {

	my $help;
	GetOptions( "help|?" => \$help );

	if ( !@ARGV ) {
		print_usage();
	}
	elsif (@ARGV) {
		$Action = shift @ARGV;
	}

	if ($help) {
		if ($Action) {
			help_action();
		}
		else {
			print_usage();
		}
	}
}

sub help_action {

	my $action = $CliDriver->get_action( name => $Action );
	$action->usage;
}

sub print_actions {

	my $actions = $CliDriver->get_actions;
	my @list;

	foreach my $action (@$actions) {

		next if $action->name =~ /dummy/i;
		my $display = $action->name;

		if ( $action->is_deprecated ) {
			$display .= " (deprecated)";
		}

		push @list, $display;
	}

	say "\tACTIONS:";

	foreach my $action ( sort @list ) {
		print "\t\t$action\n";
	}
}

sub print_usage {
	print STDERR "@_\n\n" if @_;

	my $basename = basename($0);

	printf "\nusage: %s <action> [opts] [-?]\n\n", basename($0);
	print_actions();
	print "\n";

	exit 1;
}
