#!/bin/sh

## usage dumpChapter [-S|d] [files ...]

## dump all the S objects in the chapter.  First, in dumpForObjects form,
## including the meta data, to all.Sdata and meta.Sdata.
## Then superfluously (for human viewing) all the functions and language objects
## to all.S.  The option -S suppresses this.
##
## Make DUMP_FILES listing the above files, plus any files arguments to
## dumpChapter.
## This script does not actually produce a dump file, leaving the choice
## of tar, cpio, etc. to the user.

SDUMP='cat("all.S ... "); invisible(dump(objects(w=1), "all.S", test = function(obj) is.function(obj) || is.language(obj)))'

DODUMP="y"
case $1 in
-S) ## suppress the all.S file for data-only chapters
  SDUMP=""
  shift ;;
-d) ## suppress all dumps
  DODUMP="n"
esac

if [ "$DODUMP" = "y" ]
then

S <<!
if(exists("last.dump")) remove("last.dump") else invisible(NULL)
## .Last.value will be there, but trivial
cat("all.Sdata ... ")
dumpForObjects(as.character(objects(w=1)), "all.Sdata", aliases=T)
cat("meta.Sdata ... ")
dumpForObjects(as.character(objects(w=1, meta=1)), "meta.Sdata", meta=1, aliases=T)
$SDUMP
cat("done\n")
q()
!

fi

## construct the list of all the files dumped, to be used when moving, tar'ing, etc.

cat > DUMP_FILES <<!
all.Sdata
meta.Sdata
!

## now any arguments; these will be the SRC= line from S_MAKE_FLAGS
## when called from the usual S chapter makefile
for i
do
  echo $i >> DUMP_FILES
done

## now some files that may be there and may not.  Dump (incl. 0 length)
for i in all.S makefile S_MAKE_FLAGS SOURCE_FILES S_MAKE_FLAGS S_LOCAL_FILES
  do
    if [ -r $i ]
    then echo $i >> DUMP_FILES
    fi
  done

if [ -s SOURCE_FILES ]
then cat SOURCE_FILES >> DUMP_FILES
fi

sort -o DUMP_FILES -u DUMP_FILES
echo "DUMP_FILES created with list of files to dump via tar, cpio, etc."
