#!/bin/bash
# Create the Object::Realize::Later module.

VERBOSE=$1

#### Synchronize these variables with those defined in mkdoc
DIST=/tmp/ORL    # where the module's distribution is being built

#### Other constants
DIST_OODOC="../OODoc/examples/orl.tar.gz"
SOURCES=distributed
RAW=raw
MANIFESTS="MANIFEST MANIFEST.extra"

[ -d $DIST ] || mkdir $DIST || exit $?

#
# Produce oodoc example
#

[ -n "$VERBOSE" ] && echo "* Packaging ORL setup as example for OODoc";
tar -czf $DIST_OODOC README.oodoc MANIFEST.extra mkdist mkdoc || exit $?

#
# Produce the manual pages using OODoc
#

./mkdoc $VERBOSE || exit $?

#
# Create a distribution
#

[ -n "$VERBOSE" ] && echo "* Run make to see if everything works"

( cd $DIST              || exit $?

  perl Makefile.PL      || exit $?
  make >/dev/null       || exit $?
  make test             || exit $?
  make dist >/dev/null  || exit $?
)

# Saving distributed source

DISTNAME=$(cd $DIST; ls -rt *.tar.gz | tail -1)
[ -n "$VERBOSE" ] && echo "* Saving $DISTNAME"

[ -d $SOURCES ] || mkdir $SOURCES || exit $?

if ! cp $DIST/$DISTNAME $SOURCES/
then ERROR=$?
     echo "Could not copy $DISTNAME from $DIST to $SOURCES: $ERROR" >&2
     exit $ERROR
fi

[ -n "$VERBOSE" ] && ls -l $SOURCES/$DISTNAME

# Saving raw module data

[ -d $RAW ] || mkdir $RAW || exit $?

RAWNAME=$(echo "$DISTNAME" | sed 's/\.tar\.gz/-raw.tar.bz2/')
[ -n "$VERBOSE" ] && echo "* Saving $RAWNAME"

if ! tar -cjf $RAW/$RAWNAME $(cat $MANIFESTS)
then ERROR=$?
     echo "Could not create $RAWNAME in $RAW: $ERROR" >&2
     exit $ERROR
fi

[ -n "$VERBOSE" ] && ls -l $RAW/$RAWNAME
exit 0
