#!/usr/bin/perl

use strict;
use File::Spec;

# Library path bootstrap
my @path;
if (defined $ENV{PAR_TEMP}) { # See PAR.pm
	@path = ($ENV{PAR_TEMP}, 'inc');
}
else {
	my $binary = File::Spec->rel2abs($0);
	my ($vol, $dirs, undef) = File::Spec->splitpath($binary);
	@path = ($vol, $dirs, File::Spec->updir);
}
my $libdir = File::Spec->catdir(@path, 'lib');
unshift @INC, $libdir if -d $libdir;

eval q/use File::BaseDir ()/;
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;
}

# Load core modules
eval q/
	use Gtk2 '-init';
	use Zim;
	use Zim::Repository;
/
.($^O eq 'MSWin32' ? 'use Zim::Win32;' : '');
die $@ if $@;

# Parse command line options
my ($root, $page, $name);
my $settings = { read_only => 0 };

while ($ARGV[0] =~ /^-/) {
	$_ = shift @ARGV;
	
	if (/^(--version|-v)$/) {
		print $Zim::LONG_VERSION;
		exit;
	}
	elsif (/^--read-?only$/) { $$settings{read_only} = 1 }
	elsif (/^--doc$/) {
		($root) = grep {-d $_}
			map File::Spec->catdir($_, 'zim', 'doc'),
			File::BaseDir->xdg_data_dirs;
		$$settings{read_only} = 1;
	}
	elsif (/^--export$/) { $$settings{export} = shift @ARGV }
	elsif (/^--name$/)   { $name = shift @ARGV              }
	elsif (/^--?\w/)     { exit_usage() } # include --help etc.
	
	last if /^--?$/;
}

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

unless (defined $root) { # for example: zim --doc pagename
	if (@ARGV) {
		exit_usage() if @ARGV > 2;
		($root, $page) = @ARGV;
	}
	elsif ($$settings{default_root}) {
		$root = $$settings{default_root};
	}
}
else {
	exit_usage() if @ARGV > 1;
	($page) = @ARGV;
}

if (defined $root) { show_window($name, $root) }
else { $zim->prompt_repository_dialog(\&show_window) }

Gtk2->main;

exit;

sub show_window {
	my ($name, $root) = @_;

	$zim->{name} = $name;
	
	my $rep = Zim::Repository->new(undef, ':', $root);
	$rep->{config}{read_only} = $$settings{read_only};
	$zim->set_repository($rep);
	$$settings{home} = $rep->config->{home} if $rep->config->{home};

	if (exists $$settings{export}) { # FIXME this doesn't belong here
		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) {
		eval { # dies when page does not exist in read-only mode
			$page = $rep->resolve_page($page);
			$zim->load_page($page);
		};
	}

	$zim->gui_show;
}

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 and outliner

=head1 SYNOPSIS

zim [options] [directory] [page]

=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
