#!/usr/bin/perl
#Copyright (c) 2013, 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 Toader;
use Cwd;
use Toader::findToaderDirs;
use File::Copy;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

#version function
sub main::VERSION_MESSAGE {
	print "toader-autodoc 0.0.0\n".
		"\n".
		"Switches:\n".
		"-a <action>   The action to perform.\n".
		"\n".
		"Actions:\n".
		"entry1.0 - Performs the entry migration for the 1.0 version.\n";
}

#print help
sub main::HELP_MESSAGE {

}

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

	#gets the VCS object
	my $vcs=$toader->vcs;
	if ( $toader->error ){
		warn('toader-migrate: Failed to get the VCS object. error="'.$toader->error.
			 '" errorString="'.$toader->errorString.'"');
		exit $toader->error;		
	}

	#checks if VCS is usable
	my $vcsUsable=$vcs->usable;
	if ( $vcs->error ){
		warn('toader-migrate: Failed to check if VCS is usable or not. error="'.
			 $vcs->error.'" errorString="'.$vcs->errorString.'"');
		exit $vcs->error;
	}

	#gets the dir object
	my $dirObj=$toader->getDirObj;
	if ( $toader->error ){
		warn('toader-migrate: Failed to get the directory object. error="'.$toader->error.
			 '" errorString="'.$toader->errorString.'"');
		exit $toader->error;
	}

	#finds the Toader dirs
	my $ftd=Toader::findToaderDirs->new;
	my @toaderDirs=$ftd->findToaderDirs( $dir );
	if ( $toader->error ){
		warn('toader-migrate: Failed to list the Toader dirs. error="'.$ftd->error.
			 '" errorString="'.$ftd->errorString.'"');
		exit $ftd->error;
	}

	#process each directory
	my $tdint=0;
	while ( defined( $toaderDirs[$tdint] ) ){
		my $entryDir=$toaderDirs[$tdint].'/.toader/entries/';

		#if the entry dir exists, process it
		if ( -d $entryDir ){
			my $dh;
			if ( ! opendir( $dh, $entryDir ) ){
				warn('toader-migrate: Failed to open the dir "'.$entryDir.'"');
				exit 254;
			}
			my @entries=readdir( $dh );
			closedir( $dh );

			#process each entry
			my $eint=0;
			while ( defined( $entries[$eint] ) ){
				my $oldEntry=$entryDir.$entries[$eint];
				my $newEntry=$entries[$eint];
				$newEntry=~s/\://g;
				$newEntry=$entryDir.$newEntry;
				
				#rename the entry
				if ( ! move( $oldEntry, $newEntry ) ){
					warn('toader-migrate: Unable to move "'.
						 $oldEntry.'" to "'.$newEntry.'"');
					exit 254;
				}

				#handle the VCS stuff if it is usable
				if ( $vcsUsable ){
					#add the new file
					$vcs->add( $newEntry );
					if ( $vcs->error ){
						warn('toader-migrate: Unable to add "'.$newEntry.
							 '" to VCS. error="'.$vcs->error.
							 '" errorString="'.$vcs->errorString.'"');
						exit $vcs->error;
					}

					#remove the old file
					$vcs->add( $oldEntry );
					if ( $vcs->error ){
						warn('toader-migrate: Unable to remove "'.$oldEntry.
							 '" from VCS. error="'.$vcs->error.
							 '" errorString="'.$vcs->errorString.'"');
						exit $vcs->error;
					}
				}
				$eint++;
			}
		}
		$tdint++;
	}

	exit 0;
}

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

if ( $opts{a} eq 'entry1.0' ){
	&entryMigrate;
	exit 0;
}

