#!/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;
	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 $icon = ($^O eq 'MSWin32')
	? xdg_data_file(0, qw/pixmaps zim zim.png/) # No svg support on windows
	: xdg_data_file(0, qw/pixmaps zim.svg/)     ;
my $settings = {
	read_only => 0,
	icon_file => $icon,
};
my $name;

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{read_only} = 1;
	}
	elsif (/^--export$/) { $$settings{export} = shift @ARGV }
	elsif (/^--name$/)   { $name = shift @ARGV              }
	elsif (/^--?\w/) { exit_usage() } # include --help etc.
	
	last if /^--?$/;
}

# Initialize main objects
my $zim = Zim->new($settings);
$zim->{name} = $name if defined $name;

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

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

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

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