THRIFT-4416: fixed up CPAN module creation and automated all steps except for upload to CPAN
Client: perl

This closes #1441
diff --git a/lib/perl/build-cpan-dist.sh b/lib/perl/build-cpan-dist.sh
index 1765e6d..3b80734 100755
--- a/lib/perl/build-cpan-dist.sh
+++ b/lib/perl/build-cpan-dist.sh
@@ -1,9 +1,41 @@
 #!/bin/bash
 #
 # This script is intended to be used after tagging the repository and updating
-# the version files for a release.  It will create a CPAN archive.
+# the version files for a release.  It will create a CPAN archive.  Run this
+# from inside a docker image like ubuntu-xenial.
+#
+
+set -e
+
+rm MANIFEST
+rm -rf Thrift-*
+
+# setup cpan without a prompt
+echo | cpan
+cpan install HTTP::Date
+cpan install CPAN
+cpan install CPAN::Meta ExtUtils::MakeMaker JSON::PP
 
 perl Makefile.PL
+rm MYMETA.yml
 make
 make manifest
 make dist
+
+#
+# We unpack the archive so we can add version metadata for CPAN
+# so that it properly indexes Thrift and remove unnecessary files.
+#
+
+echo '-----------------------------------------------------------'
+set -x
+
+DISTFILE=$(ls Thrift*.gz)
+tar xzf Thrift-*.gz
+rm Thrift-*.gz
+DISTDIR=$(ls -d Thrift*)
+cd $DISTDIR
+perl ../tools/FixupDist.pl
+cd ..
+tar cvzf $DISTFILE $DISTDIR
+rm -r $DISTDIR