#!/usr/bin/perl
#Copyright (c) 2012, Zane C. Bowers-Hadley
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use warnings;
use Getopt::Std;
use Cwd;
use Toader;
use Toader::Templates;
use Toader::Render::CSS;
use Term::CallEditor qw/solicit/;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

#version function
sub main::VERSION_MESSAGE {
	print "toader-css 0.0.0\n";
}

#print help
sub main::HELP_MESSAGE {
	print "\n".
		"Switches:\n".
		"-a <action>   The action to perform.\n".
		"\n".
		"Actions:\n".
		"edit - Edits the CSS template.\n".
		"get - Prints out the specified template.\n".
        "getd - Prints out the specified default template.\n".
		"render - Renders the CSS\n";
}

#gets the options
my %opts=();
getopts('a:', \%opts);

if ( ! defined( $opts{a} ) ){
	warn('toader-css: Nothing specified for -a');
	exit 254;
}

#get the current directory
my $dir=getcwd;

#initialize Toader as a bit of a test and it may be used later
my $toader=Toader->new({ dir=>$dir });
if ( $toader->error){
	warn('toader-css: Failed to initialize Toader. error="'.$toader->error.
		 '" errorString="'.$toader->errorString.'"');
	exit $toader->error;
}

#always opperate in the root directory
my $tt=Toader::Templates->new;
$tt->dirSet( $toader->getRootDir );
if ( $tt->error ){
	warn('toader-temp:"'.$tt->error.'" Failed to set the directory for Toader::Template');
	exit $tt->error;
}

if ( $opts{a} eq 'edit' ){
    my $template=$tt->getTemplate( 'css' );
    if ( $tt->error ){
        warn('toader-temp:'.$tt->error.': Failed to fetch the specified template');
        exit $tt->error;
    }
	my $fh=solicit($template);
	$template=join( '', <$fh> );
	$tt->setTemplate('css', $template);
}

if ( $opts{a} eq 'get' ){
	my $template=$tt->getTemplate( 'css' );
	if ( $tt->error ){
		warn('toader-temp:'.$tt->error.': Failed to fetch the specified template');
		exit $tt->error;
	}
	print $template;
	exit 0;
}

if ( $opts{a} eq 'getd' ){
    my $template=$tt->getTemplateDefault( 'css' );
    if ( $tt->error ){
        warn('toader-css:'.$tt->error.': Failed to fetch the specified template');
        exit $tt->error;
    }
    print $template;
    exit 0;
}

if ( $opts{a} eq 'render' ){
	my $cr=Toader::Render::CSS->new($toader);
	if ( $cr->error ){
		warn('toader-css: Failed to intialize Toader::Render:CSS');
		exit $cr->error;
	}
	$cr->render;
	if ( $cr->error ){
		warn('toader-css: Toader::Render::CSS->render errored');
		exit $cr->error;
	}
    exit 0;
}

warn('toader-css: Nothing matched for -a');
exit 254;

=head1 NAME

toader-css - A tool for working the Toader CSS stuff.

=head1 SYNOPSIS

toader-css -a edit

toader-css -a get

toader-css -a getd

toader-css -a render

=head1 SWITCHES

=head2 -a <action>

The specified action to perform.

=head2 -t <template>

The template to operate on.

=head1 ACTIONS

=head2 edit

This edits it.

=head2 get

Prints out the specified template.

=head2 getd

Prints out the specified default template.

=head2 render

This renders it.

=head1 AUTHOR

Copyright (c) 2012, Zame C. Bowers-Hadley <vvelox@vvelox.net>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS` OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=head1 OSNAMES

unix

=head1 README

toader-css - A tool for working the Toader CSS stuff.

=cut
