#!/bin/csh -f
#
# linktree - copy a directory tree, with symbolic links for source files
#
# synopsis
#    linktree [-d] <dir>
#
# example:
#    cd /dest_dir/stuff
#    linktree /master_dir/stuff/morestuff
#    where morestuff is a directory which gets mkdir'd and its contents linked.
#
# This script uses heuristics to decide what is a source file and
# what is not.  You may want to modify these heuristics for your
# local situation.  For instance, if you are working with X11, as
# I am, Makefiles are not source files.
#
# See linkcommand for a way to run commands in the source tree
# while you are working in the shadow tree.
#
# If -d is specified, creates a link "..." in each directory pointing
# back to the master directory.
#
# Written by Jef Poskanzer.  hacked a bunch by SWT and MRF

# Test for -d specification
set dflag
switch ( "$1" )
    case "-d":
	set dflag = -d
	shift
	breaksw
endsw

if ( $#argv != 1 ) then
    echo "usage: linktree [-d] <dir>"
    exit
endif

set dir=$1
if ( ! ( -d $dir ) ) then
    echo "usage: linktree [-d] <dir>"
    echo "$dir is not a directory."
    exit
endif

umask 2

set ldir=$dir:t
mkdir $ldir
cd $ldir

if ( x$dflag == x-d ) ln -s $dir ...

foreach file ( `echo $dir/*` )
    if ( $file =~ */SCCS || $file =~ */RCS ) then
	echo "ignoring...  : ${file:t}"
    else if ( $file =~ *.bdf ) then
	echo "ignoring... : ${file:t} not useful for PEX"
    else if ( $file =~ *.o || $file =~ *.a || $file =~ *.ln ) then
	echo "ignoring...  : ${file:t} binary"
#   else if ( $file =~ */Makefile ) then
#	echo "ignoring...  : Makefile"
#   else if ( $file =~ */InterViews ) then
#	echo "ignoring...  : InterViews"
    else if ( $file =~ */X.V11R?/contrib ||\
	      $file =~ */X.V11R?/doc || $file =~ */X.V11R?/demos ||\
	      $file =~ */X.V11R?/examples || $file =~ */X.V11R?/man ||\
	      $file =~ */X.V11R?/clients ) then
	echo "ignoring... : ${file:t} not useful for PEX"
    else if ( -d $file ) then
	echo "directory... : ${file}, recursing..."
	$0 $dflag $file		# $0 is linktree
    else
	set lfile=$file:t
	if (-e $lfile) then
		echo "exists...    : $lfile"
	else 
		echo "linking...   : ${file:t}"
		ln -s $file $lfile
	endif
    endif
end





