#!/bin/csh -f

# Make or update a shadow directory of the specified Starlab directory.
# Usage:
#	    mkshadow  source-directory  shadow-directory
#
# If source-directory is not specified, the default is starlab.
# If shadow-directory is not specified, the default is source_directory_s.
#
# Make pointers to source code (*.[hcCfF]), Makefile.am, ([A-Z]*),
# the sbin and local directories, and certain other top-level files.
# Do not copy or link the usr directory or any autoconfigured files.
#
# Updated to reflect Starlab's new configure/make structure by Steve (9/04).
# Best to run this script before Starlab has been configured or built,
# then work in the shadow directory.
#
# Note that the update option will add new links if they are needed, but
# will NOT remove links to files that no longer exist.

if ($#argv > 0 && ("x$1" == "x-h" || "x$1" == "x--help")) then
    echo Usage: mkshadow source-directory shadow-directory
    exit
endif

set src = starlab
if ($#argv > 0) set src = $1
if (! -d $src) then
    echo Directory $src does not exist
    exit
endif

set dst = {$src}_s
if ($#argv > 1) set dst = $2
set bad = 0
if (-d $dst) then
    echo Shadow directory $dst already exists.
    set bad = 1
else if (-e $dst) then
    echo Non-directory $dst already exists.
    set bad = 2
endif

set update = 0
if ($bad != 0) then
    if ($bad == 1) then
	echo -n "Delete (d) or update (u) $dst? [quit] "
    else
	echo -n "Delete (d) $dst? [quit] "
    endif
    set resp = $<
    if ("x$resp" == "xd") then
	/bin/rm -rf $dst
    else if ("x$resp" == "xu" && $bad == 1) then
	set update = 1
    else
	exit
    endif
endif

if ($update == 0) then
    echo Shadowing directory $src with $dst
else
    echo Updating shadow directory $dst \(shadowing $src\)
endif

#--------------------------------------------------------------------------

# We are ready to start linking dst to src.  Start by listing the
# directories and files to shadow.  Don't include CVS, and go to some
# trouble to avoid other local files needed by or created by the
# configure/make build process.

set curr = $PWD
cd $src

# Explicit list of directories to exclude: any CVS, local, config, .deps,
# .libs, top-level autom4te.cache, usr, anything below src/packages.

set excl = '/CVS$|/local$|/config$|\....s$|^./tmp|^./autom4|^./usr|/packages/'

set p = ""
set p = "-print"	# apparently unnecessary

set dirs = (`find . -type d $p | egrep -v "$excl"`)

# The list of files to link excludes local and config, which are always
# copied.  It includes all source files and Makefile.am, together with
# essential top-level files.  We also exclude usr, everything below
# src/packages, any generated source files (e.g. moc_pp3_widget.C),
# anything in CVS, and executables in sbin.

# Some definitions to limit the lengths of some following lines.

set x = config.h
set sbin_ex = "`(chdir sbin; make -s skip)`"
alias gr egrep

# (Not clear just how likely we are to run into a limit here in the length
#  of of a word or a wordlist...)

set files = ( \
  `find include src -name packages -prune -o -name '*.[hcfF]' $p | gr -v $x` \
  `find include src -name '*.C' $p | gr -v moc_pp3_widget` \
  `find src -name '*.[ly][ly]' $p` \
  `/bin/ls -d [A-U,W-Z]* *.ac install-* v*.m4 *.in | gr -v 'Make|CVS|HOST'` \
  `find doc etc scripts -name CVS -prune -o -type f $p` \
  `find src/packages/* -type d -prune -o -type f $p | gr -v '\.log$'` \
  `find . -name packages -prune -o -name Makefile.am $p ` \
  `find sbin -name CVS -prune -o -type f $p | gr -v $sbin_ex` \
  `/bin/ls include/stamp-h.in` \
)

echo Found $#dirs directories and $#files files in directory $src
cd $curr

#--------------------------------------------------------------------------

if (! -d $dst) then
    if ($update == 1) then
	echo Can\'t find shadow directory $dst		# can't happen?
	exit
    else
	mkdir $dst
    endif
endif

echo Making shadow directories in $dst \(1 dot per directory\):
set newdir = 0

foreach dir ($dirs)
    echo -n "."
    if (! -d $dst/$dir) then
	mkdir -p $dst/$dir
	if ($update == 1) then
	    @ newdir++
	endif
    endif
end
echo ""
if ($update == 1) echo Created $newdir new directories

echo Creating links from $dst to $src \(1 dot per 10 links\):
set newlink = 0

# Don't use foreach here since the number of entries in files may be large.

@ i = 0
@ n = 0
while ($i < $#files)
    @ i++
    set file = $files[$i]
    @ n++
    if ($n == 10) then
	echo -n "."
	@ n = 0
    endif
    if (! -e $cwd/$dst/$file) then
	ln -s $cwd/$src/$file $cwd/$dst/$file >& /dev/null  # avoid errors on
							    # repeated file name
	@ newlink++
    endif
end
echo ""
if ($update == 1) echo Created $newlink new links
echo ""

# Explicitly copy the "local" and "config" directories (excluding CVS)
# from src to dst.

foreach dir (local config)
    echo Copying $dir
    set dstdir = $dst/$dir
    if (-e $dstdir && $update == 0) then
	rm -r -f $dstdir
    endif
    if (! -e $dstdir) then
	mkdir $dstdir
    endif
    foreach file (`find $src/$dir -name CVS -prune -o -type f $p`)
	cp -p $file $dstdir
    end
end

echo ""
echo The shadow directory $dst now mirrors directory $src.
echo All source and Makefiles in $dst are symbolic links into $src.
echo Run install-starlab to complete the build.
echo ""
