#!/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}"

if [ ! -d "${perlinstall}" ]
then
    mkdir -p "${perlinstall}"
fi

# You can use a list should you need to force precedence
#filenames="IPC-Run-0.90 Proc-Daemon-0.14 Sort-Key-1.32 Bit-Vector-7.2"

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
  version="${version:0:(${#version} - 7)}"
  # - 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 INC="${perlinstall}" INSTALLDIRS=site 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 ..

