#!/usr/bin/perl

use strict;
use File::Spec;

# Library path bootstrap
my @path = File::Spec->splitdir($0);
splice @path, -2; # ... /bin/zim
my $libdir = File::Spec->catdir(@path, 'lib');
unshift @INC, $libdir if -d $libdir;

eval q/
	use File::BaseDir;
	use Gtk2 '-init';
	use Zim;
	use Zim::Repository;
/;
die $@ if $@;

# Data dir bootstrap
my $sharedir = File::Spec->catdir(@path, 'share');
if (-d $sharedir) {
	$ENV{XDG_DATA_DIRS} =
		join ':', $sharedir, File::BaseDir->xdg_data_dirs;
}

# Parse command line options
my ($root, $page);
my $settings = {
	read_only => 0,
	icon_file => xdg_data_file(0, qw/pixmaps zim.png/),
};

while ($ARGV[0] =~ /^-/) {
	$_ = shift @ARGV;
	
	if (/^(--version|-v)$/) {
		print $Zim::LONG_VERSION;
		exit;
	}
	elsif (/^--read-?only$/) { $$settings{read_only} = 1 }
	elsif (/^--doc$/) {
		$root = xdg_data_file(1, qw/zim doc/);
		@{$settings}{qw/read_only home/} = (1, 'Help');
	}
	elsif (/^--export$/) {
		$$settings{export} = shift @ARGV;
	}
	elsif (/^--?\w/) { exit_usage() } # include --help etc.
	
	last if /^--?$/;
}

# Initialize main objects
my $zim = Zim->new($settings);

$root = shift @ARGV unless defined $root; # for example: zim --doc pagename
$root = $$settings{default_root} unless defined $root;
unless ($root) {
	$root = $zim->prompt_root_dialog();
	$$settings{default_root} = $root;
}
exit_usage() unless defined $root;

$page = shift @ARGV;
exit_usage() if @ARGV;

my $rep = Zim::Repository->new('', $root);
$zim->set_repository($rep);
$$settings{root_dir} = $root;

if (exists $$settings{export}) {
	my %opts = map split('=', $_, 2), split ',', $$settings{export};
	$opts{verbose} = 1 unless defined $opts{verbose};
	$page = ':' unless length $page;
	$rep->export($page, %opts);
	exit 0;
}

# Initialize application
$zim->gui_init;

if ($page) {
	$page = $rep->resolve_page($page);
	$zim->load_page($page);
}

$zim->main_loop;

exit;


sub xdg_data_file {
	my $is_dir = shift;

	for (
		File::BaseDir->xdg_data_home,
		File::BaseDir->xdg_data_dirs
	) {
		my $file = $is_dir
			? File::Spec->catdir($_, @_)
			: File::Spec->catfile($_, @_) ;
		#print STDERR "Checking $file\n";
		return $file if $is_dir ? -d $file  : -f $file and -r $file;
	}
	
	die 'Could not find '.join('/', @_) .
		"\nis XDG_DATA_DIRS set correctly ?\n";
}

sub exit_usage {
	print << "EOT";
Usage: $0 ROOT_DIR [PAGE]

  ROOT_DIR is the directory to store all docs, for example ~/zim/
  PAGE     is the page you want to open, this argument is optional

  To view the manual try "$0 --doc"
EOT
	exit;
}

1;

__END__

=head1 NAME

zim - A desktop wiki

=head1 SYNOPSIS

zim [options] root_dir [page_name]

=head1 DESCRIPTION

Try to execute C<zim --doc> to view the user manual.

=head1 OPTIONS

... FIXME ...

=head1 AUTHOR

Jaap Karssenberg E<lt>pardus@cpan.orgE<gt>

Copyright (c) 2005 Jaap G Karssenberg and RL Zwart. All rights
reserved. This program is free software; you can redistribute it and/or
modify it under the same terms as Perl.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MER-
CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU
General Public License or the Artistic License for more details.

=head1 SEE ALSO

L<Zim>(3)

=cut
