#!/bin/tcsh -f

### This script can be used to set up multiple installations of the
### CCP4 suite which all derive from the same source tree. It should be
### run after unpacking the distribution, but before running configure.
### Note that if you only wish to create a single installation, then
### you do _not_ need to run this script.
### 
### This script was kindly donated by Ian Tickle, who gives the following
### detailed instructions:

# Hi, while installing v3.3 here on multiple machines (SG, HP, Alpha, Linux) I 
# recalled discussions on this topic in WG2 meetings on several occasions, and
# I finally decided to do something about it!  I thought the script I wrote to
# do the job might be of general interest.

# Actually it's very simple, and I wish I'd done it before!  All that's
# required is to set up multiple directory trees with the same structure as the
# original source tree, and with links to all the source files.  Then do the
# configure and make for each machine type in its own build tree exactly as
# normal.

# The tcsh script "duptree" below duplicates a source tree with links, and
# works in 2 stages: first it takes as argument the name of the top directory
# of the source tree (e.g. ~/xtal/ccp4 or just ccp4 if the current directory
# is ~/xtal) and creates another script "duptree_ccp4" in the current
# directory.  This script is then run once for each machine type; it takes as
# argument the name of the top directory of the tree to be created (e.g.
# ~/xtal/ccp4.sg or again just ccp4.sg if the current directory is ~/xtal).

# There are two ways of setting up the build tree, depending on the presence
# or absence of the "-a" flag.  If the -a flag is absent (i.e. the default),
# all pathnames in the generated script are relative and the build tree is
# set up in a directory parallel to the source tree.  Hard links are used so
# the build tree will look exactly like the source tree.

# If the "-a" flag is present, the build tree can be created anywhere in the
# directory structure, it doesn't have to be parallel with the source tree.  In
# this case soft links are used instead of hard ones.

# The advantage of using relative pathnames, is that the generated script can
# be given to someone else for use on another system, in order to circumvent
# the restriction of having to run this script on an unadulterated source
# tree.  You obviously can't do this if you have used absolute pathnames.

# For each script the "-f" flag deletes an existing output script or directory 
# tree (careful!) before creating a new one.

# Note that the duptree script MUST be run on the unadulterated source tree,
# i.e. in the condition immediately after unpacking the compressed sources.  If
# you run a make in the original source tree the files created will also be
# shared between machines, and it will defeat the whole object!  However once
# you've created the duptree_ccp4 script, you can do what you like in the
# original source tree.
# 
# The same strategy can be used with other packages (I've done it with TNT).
# However note that some packages (such as TNT) modify some of the source files
# in place.  This will cause problems, and you have to modify the makefile so
# that for each such file a copy of a master file is made first.

setenv PATH `echo $PATH|/bin/sed 's/\.://g'`
if ("$1" == \-a) then
  set a
  shift
endif

if ("$1" == \-f) then
  set f
  shift
endif

if ("$1" == \-a) then
  set a
  shift
endif

if ($#argv != 1) then
  echo 'usage: ./duptree [-a] [-f] <dir>'
  exit
endif

if (! -d $1 || ! -r $1) then
  echo $1 is not a readable directory.
  exit(1)
endif

set r=$1
if ($r =~ */) set r=$r:h
set r=$r:t
set s=duptree_$r
if (-e $s) then
  if ($?f) then
    rm -f $s
  else
    echo $s already exists - use -f option if you don\'t want it.
    exit(1)
  endif
endif

cat<<EOF>$s
#!/bin/tcsh -f
# This script creates a build tree with links to the original source tree, for
# use at sites where multiple installations of the suite for different
# operating systems are desired.

setenv PATH \`echo \$PATH|/bin/sed 's/\\.://g'\`
if ("\$1" == \\-f) then
  set f
  shift
endif
if (\$#argv != 1) then
  echo 'usage: ./$s [-f] <dir>'
  exit
endif
if (-e \$1) then
  if (! -d \$1) then
    echo \$1 is not a directory - delete it if you don\\'t want it.
    exit(1)
  endif
  if (\$?f) then
    echo -n "About to recursively delete directory \$1, type y to confirm: "
    if ("\$<" == y) then
      rm -rf \$1
    else
      exit
    endif
  else
    echo \$1 already exists - use -f option if you don\\'t want it.
    exit(1)
  endif
endif
mkdir \$1
cd \$1
EOF

set t=$PWD/$s
cd $1

set p=".?* *"
set p=`echo $p|sed 's/^\.\. //'|sed 's/ \.\. / /'|sed 's/ \.\.$//'`
if ("$p" != ..) then
  find $p -type d -exec echo mkdir {} \;>>$t
  if ($?a) then
    set f="-s $PWD"
  else
    set f=../$r
  endif
  find $p \( -type f -o -type l \) -exec echo ln $f/ {} {} \;|\
  sed 's/\/ /\//'|sed 's/\#/\\\#/g'>>$t
endif

chmod +x $t
echo Now do \"./$s \<dir\>\" for each new build tree to be created.
if (! $?a) then
cat<<EOF
The pathname you specify must point to a directory, which must not already
exist, to be created parallel to the source directory.

EOF
endif
