#!/bin/sh
#

# SCCSID = "@(#)flashfilesys	1.1 9/9/93"


# flashfilesys
#
# This script generates a single file for use with the HDS shell "flashfiles"
# command.
#
# Command syntax:
#	flashfilesys <file_list_file> <output_file>
#
# The <file_list_file> is a file containing the list of files to be stored in
# Flash.  Each line of the file specifies the real file name and its
# corresponding Flash filesystem file name.  When a directory is specified,
# all files within a directory are included.  Subdirectories are not included.
#
# The <output_file> is the file that is created by this shell script.  This
# file name is given when using the HDS shell "flash filesys" command to program
# the Flash memory.
#
#
# Edit history:
#
# 10/30/92 MSH, Created.
#
##############################################################################

# trap signals 0, 1, 2, 3, 15 - remove temporary files
trap "rm -f hds.temp.data hds.temp.dir" 0 1 2 3 15

# error messages and exit values
MUSAGE="usage: flashfilesys <file_list_file> <output_file>"
EUSAGE=1
EINFILE=2
ENOEXIST=3

# two arguments are required
if test "$#" -lt 2
then
    echo $MUSAGE 1>&2
    exit $EUSAGE
fi

# test existence of the input file
INFILE=$1
if test ! -r "$INFILE"
then
    echo "Error: Input_file \"$INFILE\" does not exist" 1>&2
    exit $EINFILE
fi

# prompt user to overwrite output file if it already exists
OUTFILE=$2
if test -r "$OUTFILE"
then
    echo -n "$OUTFILE already exists, overwrite? "
    read OVWR
    if [ "$OVWR" != y -a "$OVWR" != Y ]
    then
	exit 0
    fi
fi

# create output file (clear file if it already exists)
cat < /dev/null > $OUTFILE

# create temporary files
FILEDATA="hds.temp.data"
DIRINFO="hds.temp.dir"
HDR=$OUTFILE
cat < /dev/null > $FILEDATA
cat < /dev/null > $DIRINFO

# make the input file be stdin
exec 3< $INFILE
exec 0<&3

# make the output file be stdout
exec 4> $DIRINFO
exec 1<&4


# Header format:
#
# short int	header_len;
# unsigned int	magic;
# short int	num_files;
# int		dir_len;
# int		data_len;
# char		unused[8];
#

# initialize variables
OFFSET=0
# HDR_LEN is the number of lines that follow that are part of the header
# We write out magic, num_files, dir_len, and data_len
HDR_LEN=4
# the Flash filesystem magic number is 0x01122334
MAGIC=17965876
NUM_FILES=0
# DIR_LEN contains length of directory info after the server code converts
# the ascii values to binary
DIR_LEN=0
DATA_LEN=0

while read REALNAME FLASHNAME
do
#   skip blank lines
    if test ! -n "$REALNAME"
    then
	continue
    fi

#   skip lines with no second entry
    if test ! -n "$FLASHNAME"
    then
	continue
    fi

#   skip line if it begins with "#"
    EXP_REALNAME=`echo $REALNAME | sed 's/./& /g'`
    set $EXP_REALNAME
    if [ "$1" = "#" ]
    then
	continue
    fi

#   make sure file exists
    if test ! -r "$REALNAME"
    then
	echo "Error: File \"$REALNAME\" does not exist" >&2
	rm $FILEDATA
	rm $HDR
	rm $OUTFILE
	exit $ENOEXIST
    fi

#   prepend '/' to Flash file name if not already present
    EXP_FLASHNAME=`echo $FLASHNAME | sed 's/./& /g'`
    set $EXP_FLASHNAME
    if [ "$1" != / ]
    then
	FLASHNAME="/"$FLASHNAME
    fi

#   Is it a file or a directory ?
    if test -f "$REALNAME"
    then
	echo "   $REALNAME" >&2

#	output Flash file name
	echo $FLASHNAME

#	output the file offset
	echo $OFFSET

#	determine size of REALNAME file in bytes
	FILELEN=`wc -c $REALNAME | awk '{ print $1 }' -`
	DATA_LEN=`expr $DATA_LEN + $FILELEN`

#	output the file length
	echo $FILELEN

#	Update the directory info length (DIR_LEN
#	determine length of FLASHNAME string
	FNAME_LEN=`expr $FLASHNAME : '.*'`
	DIR_LEN=`expr $DIR_LEN + $FNAME_LEN + 1 + 4 + 4`

	OFFSET=`expr $OFFSET + $FILELEN`
	NUM_FILES=`expr $NUM_FILES + 1`

#	Append the file data to the temporary file data file
	cat $REALNAME >> $FILEDATA

    else
#	Must be a directory

#	Make sure Flash file name has trailing '/'
	EXP_FLASHNAME=`echo $FLASHNAME | sed 's/./& /g'`
	set $EXP_FLASHNAME
	LAST=`eval echo \\\$$#`
	if [ "$LAST" != / ]
	then
	    FLASHNAME=$FLASHNAME"/"
	fi

#	Get the directory contents
	DIR=`cd $REALNAME;/bin/ls`
	set $DIR
	while test -n "$1"
	do
	    FNAME=$REALNAME"/"$1
	    if test -f "$FNAME"
	    then
		echo "   $FNAME" >&2

#		output concatenated Flash file name
		C_FLASHNAME=$FLASHNAME$1
		echo $C_FLASHNAME

#		output the file offset
		echo $OFFSET

#		determine size of FNAME file in bytes
		FILELEN=`wc -c $FNAME | awk '{ print $1 }' -`
		DATA_LEN=`expr $DATA_LEN + $FILELEN`

#		output the file length
		echo $FILELEN

#		Update the directory info length (DIR_LEN
#		determine length of C_FLASHNAME string
		FNAME_LEN=`expr $C_FLASHNAME : '.*'`
		DIR_LEN=`expr $DIR_LEN + $FNAME_LEN + 1 + 4 + 4`

		OFFSET=`expr $OFFSET + $FILELEN`
		NUM_FILES=`expr $NUM_FILES + 1`

#		Append the file data to the temporary file data file
		cat $FNAME >> $FILEDATA

	    fi
#	    Get next file name in $1
	    shift
	done
    fi
done

# Add in the length of the zero triplet - the end of directory info marker.
# The zero triplet is not actually written out since we can't ouput a Null.
DIR_LEN=`expr $DIR_LEN + 1 + 4 + 4`

# close stdin
#exec <&-

# close stdout
exec >&-

# make the header file be stdout
exec 5> $HDR
exec 1<&5

echo $HDR_LEN
echo $MAGIC
echo $NUM_FILES
echo $DIR_LEN
echo $DATA_LEN

# close stdout
exec >&-

cat $DIRINFO >> $HDR
cat $FILEDATA >> $HDR
