SubDir TOP plugins cscript csperl5 ;

if $(PERL5.AVAILABLE) = "yes" && ! [ Property build : projgen ]
{
  # @@@ FIXME: This scheme of creating a file which includes
  # csperlxs_fallback.inc fails when it comes to project file creation, since
  # the project file needs to reference an existing file.  To fix, the fallback
  # file should exist at all times, but should be utilized only when needed,
  # such as when ExtUtils is unavailable or when creating a project file.

  rule PerlEmbedGlueFile
  {
    MakeLocate $(<) : $(LOCATE_TARGET) ;
    Clean csperl5clean : $(<) ;
  }

  if $(PERL5.EXTUTILS.EMBED.AVAILABLE) = "yes"
  {
    if $(PERL5.DYNALOADER.AVAILABLE) = "yes"
    {
      actions PerlEmbedGlueFile
      {
        $(CMD.PERL5) -MExtUtils::Embed -e xsinit -- \
        -o $(<) -std DynaLoader cspace
      }
    }
    else
    {
      actions PerlEmbedGlueFile
      {
        $(CMD.PERL5) -MExtUtils::Embed -e xsinit -- -o $(<) -std cspace
      }
    }
  }
  else
  {
    actions PerlEmbedGlueFile
    {
      echo '#include "csutil/csperlxs_fallback.inc"' > $(<)
    }
  }

  local targetdir ;
  local target = [ FGristFiles cswigpl5.inc ] ;
  local pmfile = [ FGristFiles cspace.pm ] ;

  if $(CMD.SWIG)
  {
    targetdir = $(LOCATE_TARGET) ;
    Swig $(target) $(pmfile) :
      [ FGristFiles cspace.i ] :
      perl5 :
      -c++ -shadow -const -module cspace :
      [ ConcatDirs $(TOP) include ] :
      [ ConcatDirs $(TOP) include ivaria ] :
      [ ConcatDirs $(TOP) scripts perl5 ] ;
    Depends csperl5clean : perl5swigclean ;
  }
  else
  {
    targetdir = [ ConcatDirs $(TOP) scripts perl5 ] ;
    SEARCH on $(target) = $(targetdir) ;
  }

  local perlxs = [ FGristFiles csperlxs.c ] ;
  PerlEmbedGlueFile $(perlxs) ;
  
  Description csperl5 : "Perl5 plugin" ;
  Includes [ DoSourceGrist cswigpl5.cpp ] : $(target) ;
  Plugin csperl5 : $(perlxs) csperl5.cpp csperl5.h cswigpl5.cpp ;
  LinkWith csperl5 : cstool csgfx csgeom csutil ;
  ExternalLibs csperl5 : PERL5 ;
  CFlags csperl5 :
    [ FIncludes $(targetdir) ]
    $(COMPILER.C++FLAGS.WARNING.NO_UNUSED)
    $(COMPILER.C++FLAGS.WARNING.NO_UNINITIALIZED) ;

  rule InstallCSPerl5PM
  {
    local SUBDIR = $(2) ; # Temporary override for InstallData.
    InstallData $(1) : $(3) ;
  }
  InstallCSPerl5PM $(pmfile) : $(targetdir) : perl5 ;


  # Note that '__EOF__' is purposely quoted to prevent shell from intrepreting
  # dollar signs in quoted block.
  actions Perl5Cex
  {
    cat > $(<) << '__EOF__'
#! /bin/sh
# This file is generated automatically by csperl5/Jamfile
PERL5_LIBS="$(PERL5.LFLAGS[1])"
PERL5_CFLAGS="$(PERL5.CFLAGS[1])"
PERL5_CXXFLAGS="$(PERL5.CFLAGS[1])"
PERL5_DEPS=""

if test -z "$1"; then
    echo "Do not invoke this sub-component of cs-config directly."
    exit 1
fi

case $1 in
    --libs) echo "$PERL5_LIBS" ;;
    --deps) echo "$PERL5_DEPS" ;;
    --cflags) echo "$PERL5_CFLAGS" ;;
    --cxxflags) echo "$PERL5_CXXFLAGS" ;;
    *) echo "Unknown option"; exit 1 ;;
esac
__EOF__
  }

  local cexfile = perl5.cex ;
  ShellScript $(cexfile) : Perl5Cex ;
  Depends csperl5 : $(cexfile) ;
  Depends csperl5clean : $(cexfile)clean ;
}
