#!/usr/bin/env perl


use v5.10;

use feature "switch";
use Pod::Usage;

use App::Vimpl;


use warnings;
use strict;

# TODO: Implementing Windows/Cross platform compatibility starts here
my $git_binary = `which git`;
if(!$git_binary){
	die "Git doesn't seem to be in the \$PATH\n";
}

my $pod_options = { -verbose => 1, -exitval => 1 };
my $vim_dir = $ENV{HOME} . '/.vim/';

my $command = shift @ARGV;
if(! $command){
	pod2usage($pod_options);
}

# There has to be a .vim dir and git repo before we can do anything else
given($command){
	when ('help') { pod2usage($pod_options); }
	when ('init') { App::Vimpl->init(); exit 0; }
	when ('restore') { App::Vimpl->restore(\@ARGV); exit 0; }
}

if( ! -d $vim_dir ){
	die "No .vim directory found in " . $ENV{HOME} . ".\n\tMaybe you should try 'vimpl init' or 'vimpl restore'\n";
}
chdir($vim_dir);

if( scalar(grep($_ =~ /^\s?([AM]|\?\?\s+bundle)/, `git status --porcelain`)) > 0){
	print "\nAn unclean git repository was found in $vim_dir.\n\tPlease commit or remove the pending changes before proceeding.\n";
	exit 1;
}
if( $? != 0 ){
	die "No git repository found in " . $vim_dir . ".\n\tMaybe you should try 'vimpl init' or 'vimpl restore'\n";
}


# Main command list
given($command){
	when ('update') {
		App::Vimpl->update_vim_scripts();

		exit 0;
	}
	when ('list') {
		my $modules = App::Vimpl->get_submodule_list();
	
		foreach my $module (sort(@{$modules->{list}})){
			print "\t$module\n";
		}
		print "\n";
	
		exit 0;
	}
	when (/backup|push/) {
		App::Vimpl->backup(\@ARGV);

		exit 0;
	}
	when ('install') {
		App::Vimpl->install_git_submodule(\@ARGV);
	
		exit 0;
	}
	when (/pull|upgrade/) {
		App::Vimpl->pull_update(\@ARGV);

		exit 0;
	}
	when (/remove|uninstall/) {
		App::Vimpl->remove_git_submodule(\@ARGV);
		print "\n";
	
		exit 0;
	}
	when (/search|find/) {
		App::Vimpl->search_vim_scripts(\@ARGV);
		exit 0;
	}
	default {
		pod2usage($pod_options);
	}
}


pod2usage($pod_options);



=head1 NAME

vimpl - Helps manage your vim plugins with Git

=head1 SYNOPSIS

Use Vimpl to manage your Vim scripts with Git and mirrors listed on Vim-scripts.org

	vimpl <command> [<args>]

=head1 OPTIONS
	
	init - Initializes a git repo in your $HOME/.vim folder and installs 
		Pathogen.vim as a git submodule.

	restore <gitrepo> - Clones the provided git repository into $HOME/.vim
		Initializes all git submodules and updates all submodules.

	search <vim plugins>... - Provides a list of matching plugins found
		on vim-scripts.org for each name provided.

	install [<vim plugin name>|<vim script number>|<git repo>]...- Installs
		provided Vim plugin names/numbers as git submodules from their
		respective vim-scripts.org mirrors.  Also will install a provided
		git repo as a submodule in .vim/bundle.

	list - Lists the installed Vim plugins (omits Pathogen.vim from
		the list)

	remove <vim plugins>... - Removes provided Vim plugins.
		Must match the names in 'list'.

	pull [config|plugin|all] - Does a 'git pull origin master' for either
		the remote for the $HOME/.vim repo , git submodules or both.
		If not specified, plugins only.

	update - Updates the local list of plugin mirrors from vim-scripts.org.

	backup - If the $HOME/.vim repo already has a remote origin,
		'backup' will push to it.

		backup github <name> [public|private] - Vimpl will attempt to
			create a repo for you on GitHub <name> using credentials in your
			$HOME/.gitconfig .  Vimpl will then push your .vim config to
			the fresh repo.

		backup <gitrepo> - Vimpl will attempt to set the provided repo as
			the remote origin in $HOME/.vim and push to it.

=head1 USAGE EXAMPLES

	$ vimpl init
	
	$ vimpl install SelectBuf SuperTab The-NERD-tree Wombat
	
	$ vimpl pull plugins

=head1 SEE ALSO

L<App::Vimpl>

=head1 AUTHOR

Colin Kennedy, C<< <moshen at cpan.org> >>

=cut
