#!/usr/bin/perl -w
# script to generate OOo bash_completion file for the main applications
# written by Rene Engelhard <rene@debian.org>, Public Domain
# updated for ooo-build by Petr Mladek <pmladek@suse.cz>, Public Domain
# yes, this script probably is not real good code :) but still easier
# to maintain than adding those entries statically many times in
# a file...

use strict;

my @DRAWDOCS=("sxd", "std", "dxf", "emf", "eps", "met", "pct", "sgf", "sgv", "sda",
	   "sdd", "vor", "svm", "wmf", "bmp", "gif", "jpg", "jpeg", "jfif", "fif",
	   "jpe", "pcd", "pcx", "pgm", "png", "ppm", "psd", "ras", "tga", "tif",
	   "tiff", "xbm", "xpm", "odg", "otg", "odc", "odi", "sds");

my @IMPRESSDOCS=("sxi", "sti", "ppt", "pps", "pot", "sxd", "sda", "sdd", "sdp",
	      "vor", "cgm", "odp", "otp");

my @TEMPLATES=("stw", "dot", "vor", "stc", "xlt", "sti", "pot", "std", "stw");

my @MATHDOCS=("sxm", "smf", "mml", "odf");

my @MASTERDOCS=("sxg", "xgl", "txt", "odm", "sgl");

my @WRITERDOCS=("doc", "dot", "rtf", "sxw", "stw", "sdw", "vor", "txt", "htm?",
	     "xml", "wp", "wpd", "odt", "ott");

my @WEBDOCS=("htm", "html", "stw", "txt", "vor", "oth");

my @BASEDOCS=("odb");

my @CALCDOCS=("sxc", "stc", "dif", "dbf", "xls", "xlw", "xlt", "rtf", "sdc", "vor",
	   "slk", "txt", "htm", "html", "wk1", "wks", "123", "xml", "ods", "ots",
	   "csv");


my @CMDARGS=("-accept", "-display", "-invisible", "-minimized", "-norestore",
	  "-p", "-pt", "-quickstart", "-server", "-terminate_after_init");


# default names of oowrappers
# use "" if you want to disable any wrapper
my %APPS = (
	office		=> "ooffice",
	master		=> "",
	base		=> "oobase",
	calc		=> "oocalc",
	draw		=> "oodraw",
	impress		=> "ooimpress",
	math		=> "oomath",
	template	=> "oofromtemplate",
	web		=> "ooweb",
	writer		=> "oowriter",
);

sub usage()
{
    print "Script to Generate bash completion for OOo wrappers\n\n";

    print "Usage: $0 --help\n";
    print "       $0 [--binsuffix=suffix]\n";
    print "\t\t[--with-options[=\"option1 option2 ...\"]]\n";
    print "\t\t[--office=wrapper_name]\n";
    print "\t\t[--master=wrapper_name]\n";
    print "\t\t[--base=wrapper_name]\n";
    print "\t\t[--calc=wrapper_name]\n";
    print "\t\t[--draw=wrapper_name]\n";
    print "\t\t[--impress=wrapper_name]\n";
    print "\t\t[--math=wrapper_name]\n";
    print "\t\t[--template=wrapper_name]\n";
    print "\t\t[--web=wrapper_name]\n";
    print "\t\t[--writer=wrapper_name]\n";
    print "\t\toutput_file\n\n";

    print "Options:\n";
    print "\t--help\t\tprint this help\n";
    print "\t--binsuffix\tdefines a suffix that is added after each wrapper\n";
    print "\t--with-options\tsuggest OOo wrapper options in bash completion\n\n";

    print "The other options allows to redefine the wrapper names.\n";
    print "The value \"\" can be used to disable any wrapper.\n\n";
}

my $filename;
my $binsuffix = '';
my $with_options;

my $opt;
foreach my $arg (@ARGV) {
	if ( $arg =~ /--help/ ) {
		usage();
		exit 0;
	} elsif ( $arg =~ /--binsuffix=(.*)/ ) {
		$binsuffix="$1";
	} elsif ( $arg =~ /--with-options=?(.*)/ ) {
		$with_options=1;
		@CMDARGS = split (/\s/, $1) if ($1);
	} elsif ( $arg =~ /--office=(.*)/ ) {
		$APPS{'office'} = "$1";
	} elsif ( $arg =~ /--master=(.*)/ ) {
		$APPS{'master'} = "$1";
	} elsif ( $arg =~ /--base=(.*)/ ) {
		$APPS{'base'} = "$1";
	} elsif ( $arg =~ /--calc=(.*)/ ) {
		$APPS{'calc'} = "$1";
	} elsif ( $arg =~ /--draw=(.*)/ ) {
		$APPS{'draw'} = "$1";
	} elsif ( $arg =~ /--impress=(.*)/ ) {
		$APPS{'impress'} = "$1"
	} elsif ( $arg =~ /--math=(.*)/ ) {
		$APPS{'math'} = "$1";
	} elsif ( $arg =~ /--template=(.*)/ ) {
		$APPS{'template'} = "$1";
	} elsif ( $arg =~ /--web=(.*)/ ) {
		$APPS{'web'} = "$1";
	} elsif ( $arg =~ /--writer=(.*)/ ) {
		$APPS{'writer'} = "$1"
	} elsif ( $arg =~ /^-.*/ ) {
		printf STDERR "Error: invalid option \"$arg\", try --help\n";
		exit 1;
	} elsif ( $filename ) {
		printf STDERR "Error: output file defined twice, try --help\n";
		exit 1;
	} else {
		$filename = "$arg";
	}
}
	
unless ( $filename ) {
	printf STDERR "Error: undefined output file, try --help\n";
	exit 1;
}
    
#add binsuffix
foreach my $app (keys %APPS) {
    $APPS{$app} .= "$binsuffix" unless ( "$APPS{$app}" eq "" );
}

open (BCFILE, "> $filename") || die "could not open target file!!";

print BCFILE "\# programmable bash_completion file for the main OpenOffice.org";
print BCFILE " applications\n\n";

my $extensions;

foreach my $app (keys %APPS) {
	# skip the disabled wrapper
	next if ( $APPS{$app} eq "" );

	print BCFILE "_" . $APPS{$app} . "() {\n";
	print BCFILE "\tlocal prev cur options IFS=\$'\\t\\n'\n\n";
	print BCFILE "\tCOMPREPLY=()\n";
	print BCFILE "\tcur=\${COMP_WORDS[COMP_CWORD]}\n";
	print BCFILE "\tprev=\${COMP_WORDS[COMP_CWORD-1]}\n";
	if ($with_options) {
		print BCFILE "\toptions='";
		foreach(@CMDARGS) {
			print BCFILE $_;
			print BCFILE " ";
		}
		print BCFILE "'\n";
	}
	print BCFILE "\tcase \$prev in\n";
	print BCFILE "\t\t*)\n";
	print BCFILE "\t\t\tCOMPREPLY=( \$(\n";
	if ($app eq "draw" ) { $extensions="@DRAWDOCS"; }
	if ($app eq "writer") { $extensions="@WRITERDOCS"; }
	if ($app eq "web") { $extensions="@WEBDOCS"; }
	if ($app eq "math") { $extensions="@MATHDOCS"; }
	if ($app eq "impress") { $extensions="@IMPRESSDOCS"; }
	if ($app eq "base") { $extensions="@BASEDOCS"; }
	if ($app eq "calc") { $extensions="@CALCDOCS"; }
	if ($app eq "master") { $extensions="@MASTERDOCS"; }
	if ($app eq "template") { $extensions="@TEMPLATES"; }
	# ooffice should contain all...
	if ($app eq "office") {
		$extensions="@DRAWDOCS @WRITERDOCS @MATHDOCS @IMPRESSDOCS";
		$extensions="$extensions @BASEDOCS @CALCDOCS @MASTERDOCS";
		$extensions="$extensions @TEMPLATES @WEBDOCS";
	}
	foreach (split(/ /,$extensions)) {
		print BCFILE "\t\t\t\tcompgen -G \"\${cur}*\" -X \'!*.$_\'\n";
	}
	if ($with_options) {
	        print BCFILE "\t\t\t\tcompgen -W \"\$options\" | grep \"^\$cur\"\n";
	}
	print BCFILE "\t\t\t) )\n";
	print BCFILE "\t\t;;\n";
	print BCFILE "\tesac\n\n";
	print BCFILE "\treturn 0\n\n";
	print BCFILE "}\n";
	print BCFILE "complete -d -X \'.[^./]*\' -F _$APPS{$app} -o filenames -o dirnames $APPS{$app}\n\n";
}

close(BCFILE)
