The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
#Copyright (c) 2011, 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 Sys::Config::Manage;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

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

#print help
sub main::HELP_MESSAGE {
        print "\n".
		      "Switches...\n".
			  "-c <config directory>\n".
			  "-b <base directory>\n".
		      "\n".
			  "\n".
			  "Enviromental Variables...\n".
			  "SysConfigManage_addCommand - The add command to use.\n".
			  "SysConfigManage_baseDir - The base directory.\n".
			  "SysConfigManage_selectionMethod - Selection method.\n".
			  "SysConfigManage_autoCreateConfigDir - Automatically create a config dir if it does not exist.\n".
			  "\n".
			  "\n".
			  "Default...\n".
			  "SysConfigManage_addCommand - undef\n".
			  "SysConfigManage_baseDir - undef\n".
			  "SysConfigManage_selectionMethod - hostname\n".
			  "SysConfigManage_autoCreateConfigDir - 0\n";
}

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

#
if ( ! defined( $ARGV[0] ) ) {
	warn('scm-add: No files to add specified');
	exit 254;
}

#real in the settings
my %args;
if (defined( $ENV{'SysConfigManage_addCommand'} )) {
	$args{'addCommand'}=$ENV{'SysConfigManage_addCommand'};
}
if (defined( $ENV{'SysConfigManage_baseDir'} )) {
	$args{'baseDir'}=$ENV{'SysConfigManage_baseDir'};
}
if (defined( $ENV{'SysConfigManage_selectionMethod'} )) {
	$args{'selectionMethod'}=$ENV{'SysConfigManage_selectionMethod'};
}
if (defined( $ENV{'SysConfigManage_autoCreateConfigDir'} )) {
	$args{'autoCreateConfigDir'}=$ENV{'SysConfigManage_autoCreateConfigDir'};
}

#sets the base directory if requested command line
if (defined( $opts{b} )) {
	$args{baseDir}=$opts{b}
}

#initializes it
my $scm=Sys::Config::Manage->new(\%args);
if ( $scm->error ) {
	warn('scm-add: Failed to initiate the module');
	exit 254;
}

#adds each one one
my $int=0;
my $error=0;
while (defined( $ARGV[$int] )) {
	#adds it
	$scm->add( $ARGV[$int], $opts{c} );

	#sets the error if there is a problem
	if ( $scm->error ) {
		$error=1;
	}

	$int++;
}

#warns of errors upon exit
if ( $error ) {
	warn( 'scm-add: Some errors were encountered' );
	exit 253;
}

#if we get here, it has worked
exit 0;

=head1 NAME

scm-add - This adds a new file to a coniguration directory.

=head1 SYNOPSIS

scm-add [B<-b> <base directory>] [B<-c> <configuration directory] <file0> [<file1>]...

=head1 DESCRIPTION

This copies files from the file system up to the configuration directory.

More than one may be added at a time.

The add method will be invoked for each, regardless of the success of the previous.

=head1 SWITCHES

=head2 -b <base directory>

The -b switch can be used to to specify a base directory.

If not specified via switch, the enviromental variable will be used.

=head2 -c <configuration directory>

This is the configuration directory to use.

If not specified, one will automatically be choosen.

=head1 ENVIROMENTAL VARIABLES

=head2 SysConfigManage_addCommand

This command will be invoked on the file under the configuration directory
after it has been added.

'%%%file%%%' will be replaced with the escaped full path to the new file.

=head2 SysConfigManage_autoCreateConfigDir

Wether or not the configuration directory should automatically be
created if it does not exist.

=head2 SysConfigManage_baseDir

This is the base directory to use.

=head2 SysConfigManage_selectionMethod

This is the election method to use.

If not specified, the default will be used.

=head1 EXIT CODES

Any error codes not specified below are Sys::Config::Manage error codes.

=head2 253

Failed to copy one or more files to the configuration directory.

Check std err for any error output from Sys::Config::Manage.

=head2 254

Something script specific.

=head1 AUTHOR

Copyright (c) 2011, 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 README

scm-up - This copies files from the file system up to the configuration directory.

=cut