#!/u/njancesk/progs/perl/bin/perl

# $Log: symlinkTree.pl,v $
# Revision 1.3  2002/01/02 22:16:52  njancesk
# forgot to add the remove for the link.error file
#
# Revision 1.2  2002/01/02 17:57:10  njancesk
# revised version of symlinkTree.pl (with a faster algorithm
#

## Packages used
use File::Find;

### GLOBAL VARIABLES



############################################
##################  MAIN  ##################
############################################
$real = shift;
$link = shift;
$client = lc shift;
chomp($pwd=`pwd`);
if ($real !~ /^\// && $real){ $real = "$pwd/$real" }
if ($link !~ /^\// && $link){ $link = "$pwd/$link" }

$real =~ s/\/?\\?$//;
$link =~ s/\/?\\?$//;

die "\nUsage: $0 <reference directory> <directory to contain links> [<client>]
For rules: tail -12 $0\n\n"
	unless -d $real && -d $link;

## open error file
open(ERROR, ">$link\/link.error");
select ERROR;

find {
	preprocess => \&ignore,
	wanted => \&linkup,
	follow => 1,
	follow_skip => 2
	}, $real;  ## populates @all

unlink "$link\/link.error" unless $error;

################  MAIN END  ##################



############################################
###############  FUNCTIONS  ################
############################################




sub ignore {
#
#  NOTE!!!  This is a special function exclusively used with File::Find::find
#
#  Inputs:	array of filenames
#
#  Globals:	$client, @dirs, $File::Find::dir, $link
#
#  Subs called:	NONE
#
#  Purpose:	returns array of directories/files with special rules
#
#  Returns:	see Purpose (array)
#

## don't link back other clients in 'client' dir 
if( $File::Find::dir =~ /client$/ && defined $client ){
	@_ = ($client);
	push @_, "Imakefile" if( -e "$File::Find::dir/Imakefile" );
	}


## get relative dir path
(my $dir = $File::Find::dir ) =~ s/$real//o;

## if it exists in the linked area, then forgettaboutit.
my @list = @_;
for( my $n = 0; $n <= $#list; $n++){
	delete $list[$n] if -e "$link/$dir/$list[$n]" && not -d "$link/$dir/$list[$n]";
	} 


## grep out bad things
return	grep !/^[A-Z]+$|		# ignore CLIENT dirs
		^Templates\.DB$|
		\.err$|	# anything ending in .err .QA .out
		\.QA$|
		\.out$|
		^README(?:\.txt|\.build_env)?$|	# ignore readme
		^Makefile$|	# ignore Makefile
		\/\.\.?(?:\/|$)/x, 	# ignore . and ..
		@list;


} ################################################## END ignore




sub linkup {
#
#  Inputs:	NONE
#
#  Globals:	$real, $link
#
#  Subs called:	NONE
#
#  Purpose:	makes the link
#
#  Returns:	NONE
#
my $file = $File::Find::name;
(my $lfile = $file) =~ s/$real/$link/o;

if( $file ne $real && not -e $lfile){
	symlink($file, $lfile) || &error("can't link $file to $lfile: $!\n");
	}

} ################################################## END linkup




sub error {
#
#  Inputs:	scalar of error to print
#
#  Globals:	$error
#
#  Subs called:	NONE
#
#  Purpose:	print error
#
#  Returns:	NONE
#
print "Error: ". shift;
print "\n";
$error++;

} ################################################## END error




__END__

created by: Nikola Janceski (Nicky J. from da' Bronx)

Purpose: Creates a symlink of directory tree.

RULES:	will ignore all files/dirs ending with: .err	.QA	.out

	ignores the following files/dirs: README README.txt README.build_env
					Template.DB Makefile

	ignores all files/dirs with ALL CAPS and all letters only
		(ignores AMBA but not AMBA.TXT)

