#!/usr/bin/perl

=head1 NAME

refdb-ms - RefDB MakeStyle, a bibliography style generator for RefDB

=head1 Summary

A utility to aid the generation of RefDB bibliography styles for RefDB <refdb.sourceforge.net>.

=head1 Requirements

One of the design goals for this utility was to minimise external dependencies.

=over

=item Refdb::Makestyle

A custom module containing methods and attributes used by this script.

Debian package: libperl-refdb-makestyle.

=back

=cut

use strict;
use RefDB::Makestyle;

$ENV{'CLUI_DIR'} = "OFF"; 

my $ui = UI->new();

die "Fatal errors encountered.\n" if not $ui->startup_checks();

=head1 The program

=head2 Style root

The root element of the style, C<CiteStyle>, is a data member of the user-interface (C<ui>) object.  All other elements of the style are children of the C<CiteStyle> element.

=cut

$ui->set_root( CiteStyle->new() );

=head2 Help system

There is a series of help screens.  See C<refdb-ms.pl> library for details.

=cut

print "RefDB MakeStyle -- generate bibliography styles for RefDB.\n";
$ui->help_system() if $ui->_input_confirm( "Do you require help?" );

=head2 Main entry loop

This is the main part of the program -- where the style itself is created.

It is short as most of the "work" is done by the C<refdb-ms.pl> library.

=cut

my ( $current_element , @choice , $child ) =
	( $ui->get_root()->get_last_incomplete() );
$current_element->add_attributes();
$current_element->enter_value();
while ( 1 ) {
	# Get working element
	$current_element = $ui->get_root()->get_last_incomplete();
	last if not defined $current_element;  # style is complete 
	# Add element attributes and content
	$current_element->add_attributes();
	$current_element->enter_value();
	# Provide feedback
	printf "\nProgress report:\n%s\n" , $ui->get_root()->show_progress();
	printf "Current element: %s\n" , $current_element->get_name();
	# Select next element/action
	@choice = $current_element->select_next_child();
	# Delete element if user selected that option
	if ( @choice[1] eq "[DELETE]" ) {
		$ui->delete_selected_element();
		next;
	}
	# If current element complete, skip next part
	next if not defined $choice[1];  # current element is complete
	printf "\n%s" , @choice[0];
	# Add newly selected element to style
	$child = $ui->create_element( @choice[1] );
	$current_element->add_child( $child );
}
print "\nThe style is now complete.\n";

=head2 Write output

The style is written to an C<xml> file.  If the default name, E<lt>I<stylename>-style.xmlE<gt>, already exists, another is created.

=cut

$ui->write_style();

=head2 Write summary

A summary of the style is written to an C<html> file.  It is meant as an aide memoire when entering references.

=cut

$ui->write_summary();

=head2 Upload style

The style can be uploaded to refdb.  If a pre-existing style of the same name exists it is backed up to file before the new style is added.

=cut

$ui->upload_style();

printf "\nStyle \"%s\" has been created.\nIt has been saved to file <%s>.\nA summary of the style has been written to file <%s>.\n\nRefDB Makestyle is finished.\n" , $ui->get_root()->get_style_name() , $ui->get_style_file() , $ui->get_summary_file();
exit 0;

=head1 AUTHOR

David Nebauer, david E<lt>atE<gt> nebauer E<lt>dotE<gt> org

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2004 by David Nebauer

Distributed under the same license and conditions as the C<Refdb> project E<lt>L<http://refdb.sourceforge.net/>E<gt>.

=cut
