#!/bin/bash
#
# Parameters:
# 1: Module name with colons, not a dir name with hyphens
# 2: Version #

# Check parameters.

if [ "$1" == "" ]; then
	echo Parameters: Module::Name Version

	exit
fi

if [ "$2" == "" ]; then
	echo Parameters: Module::Name Version

	exit
fi

MODULE=$1
VERSION=$2
MODULE_DIR=/home/ron/perl.modules
HOMEPAGE_DIR=/home/ron/savage.net.au/Perl-modules
DIR_NAME=`echo $MODULE | sed 's!::!-!g'`
TAR_NAME=$DIR_NAME-$VERSION
DIR_PATH=`echo $DIR_NAME | sed 's!-!/!g'`
DIR_PATH=lib/$DIR_PATH.pm

echo MODULE=$MODULE
echo VERSION=$VERSION
echo MODULE_DIR=$MODULE_DIR
echo HOMEPAGE_DIR=$HOMEPAGE_DIR
echo DIR_NAME=$DIR_NAME
echo DIR_PATH=$DIR_PATH
echo TAR_NAME=$TAR_NAME

# Move into the module's workdir.

cd $MODULE_DIR/$DIR_NAME

X=`pwd`

if [ "$X" != "$MODULE_DIR/$DIR_NAME" ]; then
	echo cd into module dir failed. Current dir is $X

	exit
fi

if [ -e MANIFEST ]; then
	echo Remove Build.PL MANIFEST *META.*. Move t/pod* to xt/author/

	exit
fi

git clean -dfX

# Check .gitignore.

grep ^/$DIR_NAME- .gitignore > /dev/null

if [ "$?" == "1" ]; then
	echo Put /$DIR_NAME- into .gitignore

	exit
fi

# Here, the trailing $ excludes MANIFEST.SKIP.

grep ^/MANIFEST$ .gitignore > /dev/null

if [ "$?" == "1" ]; then
	echo Put /MANIFEST into .gitignore

	exit
fi

# Check MANIFEST.SKIP.

grep .gitignore MANIFEST.SKIP > /dev/null

if [ "$?" == "1" ]; then
	echo Put .gitignore into MANIFEST.SKIP

	exit
fi

# Here, I omit the * to skip it being expanded by bash.

grep ^^$DIR_NAME-. MANIFEST.SKIP > /dev/null

if [ "$?" == "1" ]; then
	echo Put ^$DIR_NAME-.* into MANIFEST.SKIP

	exit
fi

# Check LICENSE.

head -1 LICENSE | grep 'Terms of Perl' > /dev/null

if [ "$?" == "1" ]; then
	echo Put Perl into LICENSE

	exit
fi

# Check Makefile.PL.

grep TEST_REQUIRES Makefile.PL > /dev/null

if [ "$?" == "1" ]; then
	echo Put TEST_REQUIRES into Makefile.PL

	exit
fi

if [ "${MODULE:0:5}" != "Local" ]; then
	grep -A 4 repository Makefile.PL | grep $DIR_NAME > /dev/null

	if [ "$?" == "1" ]; then
		echo Put repository into Makefile.PL

		exit
	fi
fi

grep LICENSE Makefile.PL | grep perl > /dev/null

if [ "$?" == "1" ]; then
	echo Put LICENSE perl into Makefile.PL

	exit
fi

grep META_MERGE Makefile.PL > /dev/null

if [ "$?" == "1" ]; then
	echo Put META_MERGE into Makefile.PL

	exit
fi

grep -A 2 meta-spec Makefile.PL | grep 2 > /dev/null

if [ "$?" == "1" ]; then
	echo Put meta-spec V 2 into Makefile.PL

	exit
fi

# Check the source for Repository.

if [ "${MODULE:0:5}" != "Local" ]; then
	grep -i '^=head1 Repository' $DIR_PATH > /dev/null

	if [ "$?" == "1" ]; then
		echo Put repository into $DIR_PATH

		exit
	fi
fi

# Check source for Hash::FieldHash.

ack Hash::FieldHash lib > /dev/null

if [ "$?" == "0" ]; then
	echo Replace Hash::FieldHash with Moo

	exit
fi

# Check t/pod.t

if [ -e t/pod.t ]; then
	echo Move t/pod.t to xt/author/

	exit
fi

# Check Changes. This depends on the format I use for Changes.
# Here are lines 1 .. 3 (ignore the '# ' prefix of course):
# Revision history for Perl extension Tree::Simple.
#
# 1.28  2016-04-27T17:35:00

VER=`head -3 Changes | tail -1 | gawk -- '{print $1}'`

if [ "$VERSION" != "$VER" ]; then
	echo Version number mismatch

	exit
fi

# Here, test for my obsolete Changes format, where [A-Z] starts a day name:
# Revision history for Perl extension WWW::Scraper::Typo3.
#
# 1.01  Thu Aug 26 17:45:00 2010

grep "^$VERSION\s\{1,2\}[A-Z]" Changes > /dev/null

if [ "$?" == "0" ]; then
	echo Fix date format in Changes with fix.dates.pl

	exit
fi

# Check Mojo log.

if [ -e log ]; then
	cp /dev/null log/development.log
fi

# Convert Changes into Changelog.ini.
# ini.report.pl ships with Module::Metadata::Changes.

ini.report.pl -c

git commit -m"Update Changelog.ini from Changes" Changelog.ini > /dev/null

# Check the repo.

git status | grep Untracked > /dev/null

if [ "$?" == "0" ]; then
	echo Remove excess files

	exit
fi

git status | grep 'Changes not staged' > /dev/null

if [ "$?" == "0" ]; then
	echo Run git commit on patched files

	exit
fi

# Update the version #.

update.version.pl -d . -v $VERSION

git commit -am"Update version # to $VERSION" > /dev/null

# Build the distro.

perl Makefile.PL
make manifest
make test

if [ "$?" == "1" ]; then
	echo Test failure

	exit
fi

make dist

if [ ! -e "$TAR_NAME.tar.gz" ]; then
	echo Dist not built

	exit
fi

make install

# Tag the repo.

T=`printf '%(%a %b %e %T %Y)T'`

git tag -d $VERSION
git tag -afm "V $VERSION @ $T" $VERSION

# Move the dist to the homepage dir and clean up.

mv $TAR_NAME.tar.gz $HOMEPAGE_DIR/$TAR_NAME.tgz
git clean -dfX
list.tags.sh

# Update the 'modules' database.

perl $MODULE_DIR/Local-Modules/scripts/update.database.pl -m $MODULE -v $VERSION

# Copy the docs to the homepage dir, where they get picked up
# by the website builder and converted into HTML.

cp -r lib/* $HOMEPAGE_DIR/pod/

# Warn if there are multiple distros in the homepage dir. This is not an error.

X=`dir $HOMEPAGE_DIR/$DIR_NAME-*.tgz | wc -l`

if [ "$X" != "1" ]; then
	echo There are multiple distros in the homepage dir
fi

# Check pre-reqs in Makefile.PL against code.

echo Wait ... running validate.build.make.pl

validate.build.make.pl $MODULE

# What state did we finish in?

git status
dir $HOMEPAGE_DIR/$TAR_NAME.tgz

echo Finished building $MODULE V $VERSION

# Prompt to push to github.

if [ "${MODULE:0:5}" != "Local" ]; then
	echo Run git push now
fi
