#! /bin/csh
# This script copies the CORE part of tools only.
# makecore [ toolsdir ] [ destdir ] [-package SOLVERS | COMM | ALL ]
set PACKAGE  = "ALL"
set DESTDIR  = tools.core
set TOOLSDIR = `pwd`
set SETSCCS  = 1
set POSITIONCNT = 0
#
if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 [ toolsdir ] [ destdir ]"
	echo "   [-package SOLVERS | COMM | CHAMELEON | ALL ] "
	echo "          [ -nosccs ]"
        echo "Makes a copy of the core of tools, from toolsdir to destdir."
        echo "If destdir not given, tools.core is used."
        exit 1
    endif
endif
while ($#argv > 0) 
    switch ($1)
    case -package:
	shift
	set PACKAGE = $1
	shift
	breaksw
    case -nosccs:
	shift
	set SETSCCS = 0
	breaksw
    default:
	if ($POSITIONCNT == 0) then
	    set TOOLSDIR = $1
            set POSITIONCNT = 1
	else if ($POSITIONCNT == 1) then
	    set DESTDIR  = $1
	    set POSITIONCNT = 2
        else
	    echo "Only 2 positional arguments allowed"
	    exit
        endif
        shift
        breaksw
    endsw
end
#
echo "Copying $PACKAGE from $TOOLSDIR to $DESTDIR..."
#
# Select which package.  By default, do everything
# BUG: vectors/pvec* needs comm.h
set BASEDIRS  = "bin c2fort doc docs inline lint set system testing tex"
set BASEFILES = "readme tools.h petsccfg.h"
if ($PACKAGE == "SOLVERS") then
    set DIRS = "$BASEDIRS blas fd ilu iter lapack nonlin solvers sparse vectors xtools"
    set FILES = "$BASEFILES"
    set EXCLUDEDIRS = "blas/old docs/misc docs/ref docs/blkcm iter/cgfac iter/qmr sparse/skyline sparse/block sparse/eagle sparse/complex sparse/order/rqt sparse/order.old xtools/papps solvers/fortran solvers/examples/history"
    set EXCLUDEFILES = "bin/upshot bin/xhist bin/xedge doc/doctext doc/doc2lt lint/blinth docs/tutorial/parallel.* system/sn system/sn860 system/time/ttest vectors/pvec.c vectors/pvec.h vectors/pzvec.c"

else if ($PACKAGE == "COMM") then
    set DIRS = "$BASEDIRS blkcm blog comm" # upshot
    set FILES = "$BASEFILES"
    set EXCLUDEDIRS = "blkcm/examples.old blog/old blog/save docs/misc docs/ref"
    set EXCLUDEFILES = "bin/upshot bin/xhist bin/xedge comm/htable/p4pgm doc/doctext doc/doc2lt lint/blinth system/sn system/sn860 system/time/ttest blog/btest comm/examples/angst/tcomm comm/examples/angst/twin"

else if ($PACKAGE == "CHAMELEON") then
    # This is a special distribution that excludes the non-point-to-point
    set DIRS = "$BASEDIRS blog comm"
    set FILES = "$BASEFILES"
    set EXCLUDEDIRS = "docs/blkcm docs/misc docs/ref docs/talk blog/save blog/old"
    set EXCLUDEFILES = "bin/upshot bin/xhist bin/xedge comm/htable/p4pgm doc/doctext doc/doc2lt docs/tutorial/solvers.* lint/blinth system/sn system/sn860 system/time/ttest blog/btest comm/examples/angst/tcomm comm/examples/angst/twin"

else 
    # Everything
    set DIRS = "$BASEDIRS blas blkcm blog comm fd ilu iter lapack nonlin solvers sparse vectors xtools" # upshot
    set FILES = "$BASEFILES"
    set EXCLUDEDIRS = "blas/old blkcm/examples.old docs/misc docs/ref iter/cgfac iter/qmr sparse/eagle sparse/skyline sparse/block sparse/complex sparse/order/rqt sparse/order.old solvers/fortran solvers/examples/history"
    set EXCLUDEFILES = "bin/upshot bin/xhist bin/xedge comm/htable/p4pgm doc/doctext doc/doc2lt lint/blinth system/sn system/sn860 system/time/ttest blog/btest comm/examples/angst/tcomm comm/examples/angst/twin"
endif
#
if (! -e "$DESTDIR") mkdir $DESTDIR
if (! -e "$DESTDIR") then
    echo "Could not create directory $DESTDIR; please create it and rerun"
    echo "$0."
    exit 1
endif
#
# We use tar for bmake, since we do NOT want to copy the links
# in bmake
echo "Copying directory bmake"
(cd $TOOLSDIR ; tar cf $DESTDIR/.foo.tar bmake )
(cd $DESTDIR ; tar xf $DESTDIR/.foo.tar ; /bin/rm $DESTDIR/.foo.tar )
# Remove the symbolic links
find $DESTDIR/bmake -type l -exec /bin/rm \{\} \;
#
# Just copy the remaining directories
foreach DIR ($DIRS)
    echo "Copying directory $DIR"
    cp -pr $TOOLSDIR/$DIR $DESTDIR
end
# Trim some directories
foreach DIR ($EXCLUDEDIRS)
    /bin/rm -rf $DESTDIR/$DIR
end
# The lint library directories
foreach DIR ($DESTDIR/lint/*)
    if (-d $DIR) /bin/rm -rf $DIR
end
# Trim some files
foreach FILE ($EXCLUDEFILES)
    /bin/rm -f $DESTDIR/$FILE
end
# add the last files
foreach FILE ($FILES)
    echo "Copying file $FILE"
    cp -p $TOOLSDIR/$FILE $DESTDIR
end
# clean the upshot directories
if (-e $DESTDIR/upshot) then
    ( cd $DESTDIR/upshot ; make clean )
endif
# trim example programs
( cd $DESTDIR ; bin/delex )
# trim lots of random stuff:
echo "Remove old/unneeded files..."
( cd $DESTDIR ; find . \( -name '*~' -o -name '*%' -o -name '*.*~' -o \
			  -name '.*~' -o -name core -o -name '*.dvi' -o \
		          -name 'rsum*.tex' -o -name '*.aux' -o \
	                  -name '*.o' -o -name '*.log' \) \
		-exec /bin/rm \{\} \; )
#
# Go through and patch the SCCSid lines.  There doesn't seem to be a
# simple way to modify a file in place (I can't pass multiple commands
# to ed/ex/vi, and sed wants to act as a stream editor)
if ($SETSCCS) then
    echo "Set the SCCS dates"
    set DATE = "`date +%m/%d/%y`"
    (cd $DESTDIR ; find . -name '*.[ch]' -exec $TOOLSDIR/bin/udcore \{\} $DATE \; )
endif
#
# If NOT including the parallel version, modify the file vectors/makefile
if ($PACKAGE == "SOLVERS") then
    ( cd $DESTDIR/vectors ; \
    sed 's/pvec.. pzvec..//g' makefile >! tmp ; \
    /bin/mv -f tmp makefile )
endif
# If NOT including the parallel solvers, modify the papps to exclude the 
# parallel matrix display
if (! -e $DESTDIR/psolvers) then 
    if (-e $DESTDIR/xtools/papps) then
	( cd $DESTDIR/xtools/papps ; \
    	sed 's/qpmatrix..//g' makefile >! tmp ; \
    	/bin/mv -f tmp makefile ; /bin/rm -f qpmatrix.c )
    endif
endif

exit 0
