#!/usr/local/bin/perl
#
#	To fix SLS directory structure.
#

if ( ! -f "./.fix_SLS.enable" )
{
	print STDERR "$0: You're not allowed to start fix_SLS here\n";
	print STDERR "$0: enable working with ./fix_SLS.enable\n";
	exit( 0 );
}

@remove_files	= ();
@DirectoryList	= `find . -type d -print`;

foreach (@DirectoryList)
{
	chop;

	if ( $_ eq "." || $_ eq ".." || $_ eq "./.OLD" )
	{
		next;
	}

	$FSP_CONTENT = $_ . "/.FSP_CONTENT";

	if ( -f $FSP_CONTENT )
	{
		@should_be 	= `strings $FSP_CONTENT`;
		chop(@should_be);
		$directory	= $_;
		@files		= `ls $directory`;
		chop(@files);

		foreach (@files)
		{

			$item = $_;
			$found = 0;
			foreach (@should_be)
			{
				if ( /^\+/ )
				{
					next;
				}
#print "compairing file ",$item," to .FSP ",$_,"\n";
				if ( $item eq $_ )
				{
#print "found\n";
					$found = 1;
					last;
				}
			}
			if ( $found == 0 )
			{
				push( @remove_files, $directory . "/" . $_ );
			}
		}
	}
}


foreach ( @remove_files )
{
#	print "should remove: ",$_,"\n";
	if ( ! -d $_ )
	{
		unlink( $_ );
		print "removed: ",$_,"\n";
	}
}
