#! /bin/sh
#
############################################################################
#                                                                          #
# The contents of this file are subject to the WebStone Public License     #
# Version 1.0 (the "License"); you may not use this file except in         #
# compliance with the License. You may obtain a copy of the License        #
# at http://www.mindcraft.com/webstone/license10.html                      #
#                                                                          #
# Software distributed under the License is distributed on an "AS IS"      #
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See      #
# the License for the specific language governing rights and limitations   #
# under the License.                                                       #
#                                                                          #
# The Original Code is WebStone 2.5.                                       #
#                                                                          #
# The Initial Developer of the Original Code is Silicon Graphics, Inc.     #
# and Mindcraft, Inc.. Portions created by Silicon Graphics. and           #
# Mindcraft. are Copyright (C) 1995#1998 Silicon Graphics, Inc. and        #
# Mindcraft, Inc. All Rights Reserved.                                     #
#                                                                          #
# Contributor(s): ______________________________________.                  #
#                                                                          #
# %M% %I%
#**************************************************************************#

# get configuration
. $WEBSTONEROOT/conf/testbed

[ -z "$GENFILES_DIR" ] && GENFILES_DIR=$WEBSTONEROOT/DataFiles
if [ ! -d $GENFILES_DIR ]
then
	if mkdir $GENFILES_DIR
	then
		echo "Creating directory '$GENFILES_DIR'."
	else
		echo "Unable to create directory '$GENFILES_DIR'."
		exit 1
	fi
fi

# leaving the DEBUG variable unset or setting it to 0 turns off debugging
[ "$DEBUG" = '0' ] && DEBUG=''
[ -n "$DEBUG" ] && set -x

COPY=yes

if [ -z "$RCP" ] || [ -z "$SERVER" ] || [ -z "$WEBDOCDIR" ]
then
	echo "$0: Leaving data files in $GENFILES_DIR." 1>&2
	COPY="no"
fi


if [ $SERVER = "localhost" ] || [ $SERVER = `hostname` ]
then
	do_local_copy="true"
else
	do_local_copy="false"
fi


while read filesize filename
do
	echo "$GENFILES_DIR/$filename $filesize"
	$WEBSTONEROOT/bin/genrand $filesize $GENFILES_DIR/$filename
	[ "$COPY" = "no" ] && continue

	if [ $do_local_copy ] 
	then
		if cp $GENFILES_DIR/$filename $WEBDOCDIR
		then
			rm -f $filename
		else
			echo "genfiles: Error copying $GENFILES_DIR/$filename to $WEBDOCDIR" 1>&2
		fi
	else
		if $RCP $GENFILES_DIR/$filename $SERVER:$WEBDOCDIR
		then
			rm -f $filename
		else
			echo "genfiles: Error copying $GENFILES_DIR/$filename to $SERVER:$WEBDOCDIR" 1>&2
		fi
	fi
done < $WEBSTONEROOT/conf/fileset


if [ "$COPY" = "no" ]
then
	cat <<EOF
Done!

Now copy or move the files from the directory named '$GENFILES_DIR'
to the root of your web server document tree.
EOF
fi
