#============================================================================
# Rules for creating cs-config compile help script
#============================================================================

SubDir TOP scripts cs-config ;

rule csConfigGen
{
  LOCATE on cs-config.temppre cs-config.temppost = $(SUBDIR) ;
  Depends $(<) : Jamconfig cs-config.temppre cs-config.temppost 
  		 [ ConcatDirs $(TOP) scripts cs-config Jamfile ] ;

  local l d e s libdep libdepstr ;
  
  for l in $(INSTALLEDLIBS)
  {
    libaddstrings += "    $(l)) 	\
	addlib \"-l$(l)\" 	\
	;;			\
" ;
    libdep = ;
    for d in $($(l)_depends)
    {
      libdep += -l$(d) ;
    }
    for e in $($(l).EXTERNALLIBS)
    {
      libdep += $($(e).LFLAGS) ;
    }
    libdepstr = "" ;
    for s in $(libdep)
    {
      libdepstr = "$(libdepstr) $(s)" ;
    }
    libstrings += "       -l$(l)) DEPS=\"$(libdepstr)\" ;;\
" ;
  }

  liblist = "       $(INSTALLEDLIBS) \
" ;

  # Hack to produce a makevars output similar to the 'make' build system.
  switch $(TARGET.OS)
  {
  case WIN32 :
    MAKEVARS = makevar.win32 ;
  case MACOS_X :
    MAKEVARS = makevar.macosx ;
  case * :
    MAKEVARS = makevar.unix ;
  }
  MAKEVARS = $(MAKEVARS:R=$(SUBDIR)) ;
  Depends $(<) : $(MAKEVARS) ;
}

actions csConfigGen
{
  cat $(SUBDIR)/cs-config.temppre > $(<)

  cat >> $(<) << __END__
CRYSTAL="\${CRYSTAL-$(prefix)}"
prefix="\${CRYSTAL}"
exec_prefix="\${prefix}"
makeout="$(LOCATE.OBJECTS)"
version="$(PACKAGE_VERSION)"
longversion="$(PACKAGE_STRING)"
# try to determine if we're in a source or installed CS version
if [ -r \${prefix}/include/cssysdef.h ] ; then
  includedir="\${prefix}/include"
else
  includedir="$(includedir)"
fi
syslibs=" $(LINKLIBS) "
common_cflags=" $(COMPILER.CFLAGS.MANDATORY) "
common_cxxflags=" $(COMPILER.CFLAGS.MANDATORY) $(COMPILER.C++FLAGS.MANDATORY) "

makevars()
{
cat << "__EOF__"
__END__

  cat $(MAKEVARS) >> $(<)

  echo "__EOF__" >> $(<) 
  echo "}" >> $(<)

  cat >> $(<) << __END__
# dependencies of CS
depends()
{
    case \$1 in
 $(libstrings)
	*)
	    CEXFILE=`echo "\$1.cex" | sed -e "s/\-l//"`
	    findcexfile "\$CEXFILE"
	    if test -r "\$CEXFILE"; then
		DEPS=\`/bin/sh \$CEXFILE --deps\`
	    else
		DEPS=''
	    fi
	    ;;                                        	    
    esac
}

checklibname()
{
    case \$1 in
 $(libaddstrings)
    *)
	findcexfile "\$1"
	if test -z "\$CEXFILE"; then
	    echo "Unknown lib: \$1" 1>&2
            usage 1>&2
	    exit 1
	fi
	
	addexlib "\$CEXFILE"
        ;;
    esac
}

liblist="\
 $(liblist)
"
__END__

  cat $(SUBDIR)/cs-config.temppost >> $(<)
}

ShellScript cs-config : csConfigGen ;
