#! /usr/bin/perl

=pod

B<source> the following line in your I<~/.bashrc>:

B<complete> -o default -C perl_completion.pl test_command

Your script (perl_completion.pl) has to be executable and somewhere in the path; 
it will receive these arguments:

@ARGV
|- 0 = command
|- 1 = word_to_complete
`- 2 = word_before_the_word_to_complete 

You return possible  completion you want separated by I<\n>. Return nothing if you
want the default bash completion to be run which is possible because of the <-o defaul>
passed to the B<complete> command.

Note! You may have to re-run the B<complete> command after you modify your perl script.

=cut

use strict;
use Tree::Trie;

my @completions = qw( aeode calliope clio erato euterpe melete melpomene mneme polymnia terpsichore thalia urania ) ;

my($trie) = new Tree::Trie;
$trie->add(@completions) ;

#~ use Data::TreeDumper ;
#~ warn DumpTree \@ARGV, "\ncompletion script arguments" ;

my $word_to_complete = $ARGV[1] ;
if(defined $word_to_complete)
	{
	#todo: handle option?? for verbose output
	if(substr($word_to_complete, -1, 1) eq '?')
		{
		substr($word_to_complete, -1, 1, '')  ;
		
		my $help = '' ;
		
		if(length $word_to_complete)
			{
			my @possible_completion = $trie->lookup($word_to_complete) ;
			$help = join("\n", map{sprintf '%-10s: does something with soemthing else that is  ...', $_} @possible_completion) ;
			}
		else
			{
			#todo: make sure we get lines
			#todo: replace tabs with spaces
			
			$help =  "Command help\n\n" . 'extremely long line' . 'aaaaa ' x 20 ;
			}
		
		warn <<EOH ;

$help
EOH
		print "option?\n" ;
		#~ print "option_verbose??\n" ;
		print "?\n" ;
		}
	else
		{
		print join("\n", $trie->lookup($ARGV[1])) ;
		}
	}
else
	{
	print join("\n", $trie->lookup('')) ;
	}
