#!/bin/bash
# Create the MIME::Types module.
# This script can be used as base for your own implementation, as long
# as you treat it smartly.  It will call mkdoc to produce documentation
# using the OODoc module.

# This scripts produces a module (sources) as can be transfered to CPAN,
# and an archive (raw) for the files where the manuals are created from:
# the raw input file.  The latter requires a MANIFEST.extra file which
# contain the additional filenames.

VERBOSE=$1

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

#### Other constants
SOURCES=public_html/polygon/source
RAW=public_html/polygon/raw
MANIFESTS="MANIFEST MANIFEST.extra"

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

#
# Produce the manual pages using OODoc
#

./mkdoc $VERBOSE || exit $?

#
# Create a distribution
#

( cd $DIST              || exit $?

  [ -n "$VERBOSE" ] && echo "* Preparing test"
  perl Makefile.PL      || exit $?
  make clean >/dev/null || exit $?
  mv Makefile.old Makefile

  [ -n "$VERBOSE" ] && echo "* Running make"
  make >/dev/null       || exit $?

  [ -n "$VERBOSE" ] && echo "* Running tests"
  make test             || exit $?

  [ -n "$VERBOSE" ] && echo "* Building distribution"
  make dist >/dev/null  || exit $?
)

#
# Publish distribution on the private website
#

DISTNAME=$(cd $DIST; ls -rt *.tar.gz | tail -1)
[ -n "$VERBOSE" ] && echo "* Publishing $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

( cd $SOURCES
  [ -n "$VERBOSE" ] && ls -l $DISTNAME
  ln -sf $DISTNAME source-current.tar.gz || exit $?
)

#
# Publish raw module data (including the raw manual information) to
# the website
#

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

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

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

( cd $RAW
  [ -n "$VERBOSE" ] && ls -l $RAWNAME
  ln -sf $RAWNAME raw-current.tar.bz2 || exit $?
)

exit 0
