#!/bin/bash

progname=$0

source ../cascade-make/lib/cascade-lib.bash GEXTPARALLELBUILDING ../.. $*
source ../cascade-make/lib/cascade-lib-modulebuild.bash GEXTPARALLELBUILDING $*

base=$GEXTPARALLELBUILDING
prefix=$GEXTPARALLELBUILDING_INSTALLED
perlversion=$PERL_VERSION
perlinstall="${prefix}/lib/perl/${perlversion}"

filenames="IPC-Run-0.90 Proc-Daemon-0.14 Sort-Key-1.32 Bit-Vector-7.2 version-0.9902 Perl-OSType-1.003 Module-Metadata-1.000014 Module-Build-0.4005 Test-Requires-0.06 Test-Fatal-0.010 Test-Simple-0.98 Module-Runtime-0.013 Try-Tiny-0.12 Module-Implementation-0.06 Params-Validate-1.07 Params-Util-1.07 DateTime-1.03 DateTime-Locale-0.45 List-MoreUtils-0.33.tar.gz Class-Singleton-1.4 Class-Load-0.20 Data-OptList-0.107 parent-0.225 DateTime-TimeZone-1.59"

cd cpan
# 1. Scan <ext>/packages/cpan for *.tar.gz files
#for filename in *.tar.gz # gah - doesn't support prereqs
for filename in filenames
do
  # 2. For each found
  echo "Processing $filename"
  IFS='-' read -a parts <<< "$filename"
  partslen=${#parts[@]}
  # deal with version first, so we can pop it off array
  version=${parts[${#parts[@]}-1]}
  # - remove .tar.gz and prepend -
  #version="-${version:0:(${#version} - 7)}"
  # - just prepend -
  version="-${version}"
  # - pop!
  unset parts[${#parts[@]}-1]
  # now construct the three other module strings we need
  first="1"
  package=""
  modulecolons=""
  modulefilesp=""
  for i in "${parts[@]}"
  do
    if [ $first == "1" ]
    then
      package=$i
      modulecolons=$i
      modulefilesp=$i
      first="0"
    else
      package="${package}-${i}"
      modulecolons="${modulecolons}::${i}"
      modulefilesp="${modulefilesp}/${i}"
    fi
  done
  echo "Package: $package (aka $modulecolons or $modulefilesp)"
  echo "Version: $version"
  modulepath="${base}/packages/cpan/${package}${version}"
  # a. Unpack if necessary
  opt_run_untar $force_untar $auto_untar $package $version
  # b. Determine the type of Perl build (ModuleBuild vs MakeMaker)
  modulebuild="0"
  if [ -f "${modulepath}/Build.PL" ]
  then
    modulebuild="1"
  fi
  echo "Using ModuleBuild to install? $modulebuild"
  # c. Determine if we need to compile this file
  if [ ! -f "${perlinstall}/${modulefilesp}.pm" ]
  then
    if [ $modulebuild == "1" ]
    then
      # d. Configure
      opt_modulebuild_configure $force_config $auto_config $package $version $prefix $perlversion
      # e. Compile
      opt_modulebuild_make $compile   $package $version
      opt_modulebuild_make $install   $package $version "install"
    else
      # d. Configure
      opt_run_perl_configure $force_config $auto_config $package $version $prefix INSTALLSITEARCH="${perlinstall}" INSTALLSITELIB="${perlinstall}"
      # e. Compile
      opt_run_make $compile   $package $version
      opt_run_make $install   $package $version "install"
    fi
  else
    echo "${modulecolons} Perl module already compiled => no need to recompile"
  fi
  # f. Other Perl builder specific modes
  if [ $modulebuild == "1" ]
  then
    opt_modulebuild_make $clean     $package $version "clean"
    opt_modulebuild_make $distclean $package $version "distclean"
  else
    opt_run_make $clean     $package $version "clean"
    opt_run_make $distclean $package $version "distclean"
  fi
  # g. Finally the general modes
  opt_run_tarclean $tarclean $package $version
done

cd ..

