#!/bin/sh


function usage () {
    echo 1>&2 "Append a number of fully configured V3 AUs to 'lockss.opt'"
    echo 1>&2 
    echo 1>&2 "usage: $0 -n <number> [-d <depth> -b <branch> -f <filetypes> \\ "
    echo 1>&2 "       -N <numfiles> -B <binsize> -o <output>]"
    echo 1>&2 "Where: "
    echo 1>&2 "   <number>      The number of AUs to create. Required."
    echo 1>&2 "   <depth>       The depth of each AU.  Default 1."
    echo 1>&2 "   <branch>      The number of branches of each AU.  Default 1."
    echo 1>&2 "   <filetypes>   The type of files, 'bin', 'text', 'both'.  Default 'both'."
    echo 1>&2 "   <numfiles>    The number of files per branch.  Default 2."
    echo 1>&2 "   <binsize>     The size (in bytes) of the binary files.  Default 1K."
    echo 1>&2 "   <output>      The file to which to write.  Default 'lockss.opt'."
}

# Defaults
COUNT=
DEPTH=1
BRANCH=1
FILETYPE=17
FILETYPETEXT="both binary and text"
NUMFILES=30
BINSIZE=1024
OUTFILE="lockss.opt"

while getopts hn:d:b:f:N:B:o: opt
do
    case "$opt" in
    n) 
       COUNT=$OPTARG
       ;;
    d) 
       DEPTH=$OPTARG
       ;;
    b) 
       BRANCH=$OPTARG
       ;;
    f) 
       if [ $OPTARG = "bin" ] ; then
           FILETYPE=16
	   FILETYPETEXT="binary"
       elif [ $OPTARG = "text" ] ; then
           FILETYPE=1
	   FILETYPETEXT="text"
       fi
       ;;
    N) 
       NUMFILES=$OPTARG
       ;;
    B) 
       BINSIZE=$OPTARG
       ;;
    o) 
       OUTFILE=$OPTARG
       ;;
    h|?) 
       usage
       exit 127
       ;;
    esac
done

if [ "${COUNT}x" == "x" ] ; then
    usage
    exit 127
fi

echo "Generating ${COUNT} configured mock archival unit(s) of depth ${DEPTH}, with"
echo "${BRANCH} branch(es), consisting of ${FILETYPETEXT} files."

echo -n "OK to continue? [Y/n]: "
read c
if [ "${c}x" == "yx" -o "${c}x" == "Yx" ] ; then
    echo "Continuing..."
else
    echo "Not continuing."
    exit 127
fi


echo -n "Overwrite ${OUTFILE}? [y/N]: "
read overwrite

if [ "${overwrite}x" == "yx" -o "${overwrite}x" == "Yx" ] ; then
    echo "OK, overwriting ${OUTFILE}.  (Backup is ${OUTFILE}~)..."
    if [ -f ${OUTFILE} ] ; then
	mv -f ${OUTFILE} ${OUTFILE}~
    fi
else
    echo "OK, Appending to ${OUTFILE}..."
fi

iter="0"
while [ $iter -lt $COUNT ] ; do
    echo "Adding content ${iter}..."
    cat <<EOF >>${OUTFILE}

# Archival Unit generated by make_content.sh.
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.root=root_${iter}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.branch=${BRANCH}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.depth=${DEPTH}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.fileTypes=${FILETYPE}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.numFiles=${NUMFILES}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.binFileSize=${BINSIZE}
org.lockss.au.org|lockss|plugin|simulated|SimulatedPlugin.root~root_${iter}.protocol_version=3

EOF
    # Increment iter.
    iter=`expr $iter + 1`
    done
exit 0
