#! sh

"$MAKE" || exit

make_install() {
   "$MAKE" install UNINST=1 \
      && "$PERL_PREFIX"/bin/SP-patch-postinstall
}

install_success=

if find blib/arch/auto -type f \( -name "*.a" -o -name "*.obj" -o -name "*.lib" \) | grep -q .; then
   echo "Probably a static XS module, rebuilding perl"

   # perl seems to try to link against both installed and blib files at
   # the same time, at least in the case of Encode.
   #
   # To work aorund this, we try to install the module first, which, due
   # to the UNINST=1, should get rid of any duplicates first. We remember
   # whether the installw as successful so we don't re-run it later, but
   # if it fails, we install later again.
   #
   # Uninstall using packlists won't work, as modules embedded in perl
   # (e.g. Encode) do not have a (Separate) .packlist file

   if make_install; then
      install_success=+
   fi

   rm -f Makefile.aperl

   if "$MAKE" all perl; then
      mv perl "$PERL_PREFIX"/bin/perl~ \
         && rm -f "$PERL_PREFIX"/bin/perl \
         && mv "$PERL_PREFIX"/bin/perl~ "$PERL_PREFIX"/bin/perl
      "$MAKE" -f Makefile.aperl map_clean
   else
      "$MAKE" -f Makefile.aperl map_clean
      exit 1
   fi
fi

if [ "$install_success" ]; then
   true
else
   make_install
fi

