#!/usr/bin/perl

use strict;
use File::Spec;
use File::BaseDir;
use Gtk2 '-init';
use Zim;
use Zim::Repository;

my ($root, $page);
my $data_dir = find_data_dir();
my $settings = {
	read_only => 0,
	data_dir  => File::Spec->catdir($data_dir, 'zim'),
	icon_file => File::Spec->catfile($data_dir, 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 = File::Spec->catdir($data_dir, 'zim', 'doc');
		@{$settings}{qw/read_only home/} = (1, 'Help');
	}
	elsif (/^--export$/) {
		$$settings{export} = shift @ARGV;
	}
	elsif (/^--?\w/) { exit_usage() } # include --help etc.
	
	last if /^--?$/;
}


my $zim = Zim->new($settings);

$root = shift @ARGV unless defined $root;
$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;
}

$zim->gui_init;

$zim->load_page($page) if $page;

$zim->main_loop;

exit;

sub find_data_dir {
	for (File::BaseDir->xdg_data_home, File::BaseDir->xdg_data_dirs) {
		my $dir = File::Spec->catdir($_, 'zim');
		return $_ if -d $dir && -r _;
	}
	my @dirs = File::Spec->splitdir($0);
	pop @dirs; pop @dirs;
	my $dir = File::Spec->catdir(@dirs, 'share', 'zim');
	return File::Spec->catdir(@dirs, 'share')
		if -d $dir && -r _;
	
	print << 'EOT';
Could not find data files.
Typically these should be found in a dir like 
    /usr/share/zim/         or
    /usr/local/share/zim/

If you have found the directory try adding the base
part of the directory ('/usr/share/' for '/usr/share/zim') 
to the XDG_DATA_DIRS variable.
EOT
	exit 1;
}

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
