#! /bin/sh

# distlist: create a list of files to distribute
#
# usage: distlist [ -p prefix ] [ machtype ... ]

# This script generates a list of files obtained from FILES files in a
# directory tree.  Specifically, it lists the files in the file FILES
# from the current directory, then recursively from the subdirectories
# given by the DISTDIRS macro in the Makedefs file, as well as the
# files in certain object directories (see below).  Files are listed
# one per line and are prefixed with the pathname of the containing
# directory, relative to the directory in which the script is first
# invoked.

# The machtype arguments determine which object directories are
# included.  Each machtype argument should be the name of an
# object subdirectory (O.sgi, O.next, etc).  If no object subdirs
# are specified, all those present are included.

# In general the prefix argument is used only by this script when it
# invokes itself recursively.

# We assume the shell var GEOM is set to the root of the geom tree

prefix=.
if [ "$1" = "-p" ] ; then
  prefix=$2 ;
  if [ -n "$VERBOSE" ]; then echo "distlist $prefix" >&2; fi
  shift 2 ;
else
  # If no -p, we're the top-level script, so unique-ify our files.
  # Also filter out embedded ..'s
  $0 -p ./Geomview "$@" | sed -e "/\.\./s:/[^/]*/\.\./:/:g" | sort -u
  exit
fi

machargs="$*"

if [ $# = 0 ] ; then
  machdistdirs="O.*"
  ourtest=: ;
else
  machdistdirs="$*"
  ourtest='! -d $1' ;
  for mach in $machdistdirs ; do
    ourtest='-d $dir/'$mach' -o '"$ourtest" ;
  done ;
  ourtest='set -- $dir/O.*; test '"$ourtest" ;
fi

if [ -f FILES ] ; then
  sed -e "s:^:$prefix/:" FILES
fi

if [ -f Makedefs ] ; then
  distdirs=`make DISTDIRS` ;
  for dir in $distdirs $machdistdirs ; do
    if test -d $dir && eval "$ourtest" ; then
        ( cd $dir ; $GEOM/tools/distlist -p $prefix/$dir $machargs ) ;
    fi
# oh sed god do thy thing!
  done | sed -e 's:/[^/][^/]*/\.\./:/:' ;
fi

# The above sed command replaces srings of the form "/xxx/../"
# with a single "/":
