#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Config::Tiny;
use OpenGuides::Utils;
my ($conffile, $srcdir, $dbtype, $dbname, $dbuser, $dbpass, $dbhost, $help, $preclear);
GetOptions( "conf=s" => \$conffile,
		"src=s"          => \$srcdir,
		"type=s"         => \$dbtype,
		"name=s"         => \$dbname,
		"user=s"         => \$dbuser,
		"pass=s"         => \$dbpass,
		"host=s"         => \$dbhost,
		"help"           => \$help,
		"force-preclear" => \$preclear
);
$conffile ||= 'wiki.conf';
$srcdir   ||= 'src';
my $config = Config::Tiny->read($conffile);
$dbtype ||= $config->{_}{dbtype} || die "No database type specified in $conffile\n";
$dbname ||= $config->{_}{dbname} || die "No database name specified in $conffile\n";
$dbuser ||= $config->{_}{dbuser};
$dbpass ||= $config->{_}{dbpass};
$dbhost ||= $config->{_}{dbhost};
my @args;
@args = ('--type' => $dbtype, '--name'=> $dbname);
if ($dbuser) {
	push @args, ('--user' => $dbuser);
	push @args, ('--pass' => $dbpass) if $dbpass;
}
push @args, ('--host' => $dbhost) if $dbhost;
push @args, '--force-preclear' if $preclear;
system "cgi-wiki-setupdb @args";
my $wiki = OpenGuides::Utils->make_wiki_object( config => $config );
{
for (<$srcdir/*.src>) {
open (SRC, "<$_") or die;
chomp (my $node_name = <SRC>);
if ($wiki->node_exists($node_name)) {
	print "Skipping $node_name\n";
} else {
	print "Writing $node_name\n";
	local $/ = undef;
		my $node_text = <SRC>;
		$wiki->write_node( 
			$node_name, $node_text, undef, {category=> ['Wiki How To']}
			) or die "Couldn't write $node_name\n";
		}
	}
}

=head1 NAME

openguides-setupdb - Set up a database storage backend for OpenGuides

=head1 SYNOPSIS

 # Setup the CGI::Wiki backend using the config information in wiki.conf
 # that was written during installation.  This calls the script 
 # cgi-wiki-setupdb.
 # It will also write all the files distributed in the src directory
 # (containing default template files) to the wiki.

 openguides-setupdb

=head1 MORE OPTIONS

 # You can also set another location for the configuration file
 openguides-setupdb --conf myconf.conf

 # And for the source files
 openguides-setupdb --src examples/mysrc
 
 # Or use the same parameters as cgi-wiki-setupdb
 openguides-setupdb --type postgres
  --name mywiki \
  --user wiki  \
  --pass wiki \
  --host 'db.example.com'

 # Clear out any existing data and set up a fresh backend from scratch.
 openguides-setupdb --force-preclear

=head1 DESCRIPTION

All arguments are optional, but the data should be specified in the wiki.conf
file that was created when you installed OpenGuides.

=over 4

=item conf

The path to the config file.  
You don't need to specify this if you are in the cgi-bin directory where
wiki.conf lives.

=item src

The path to the src directory.  openguides-setupdb assumes that this is in the
src directory off the current directory.  We read each file ending *.src,
the first line is the Node title, the rest is the contents.

=item type

The database type.  Should be one of 'postgres', 'mysql' and 'sqlite'.

=item name

The database name.

=item user

The user that connects to the database. It must have permission
to create and drop tables in the database.

=item pass

The user's database password.

=item host

The hostname of the machine the database server is running on (omit
for local databases).

=back

and one optional flag:

=over 4

=item force-preclear

By default, this script will leave any existing data alone.  To force
that to be cleared out first, pass the C<--force-preclear> flag.

=back

=head1 AUTHOR

 osfameron (openguides@osfameron.abelgratis.co.uk)
 based on cgi-wiki-setupdb by Kake Pugh (kake@earth.li)

=head1 COPYRIGHT

     Copyright (C) 2004 osfameron.  All Rights Reserved.

This code is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 SEE ALSO

L<CGI::Wiki>
L<OpenGuides>

=cut
1;
