#!/bin/sh

# mktargets -- configure top-level library targets		-*- sh -*-
# 
#   Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
#                              listed elsewhere in this file.
#   All rights reserved.
#   
#   This file is part of Unix Squeak.
# 
#      You are NOT ALLOWED to distribute modified versions of this file
#      under its original name.  If you modify this file then you MUST
#      rename it before making your modifications available publicly.
# 
#   This file is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.
#   
#   You may use and/or distribute this file ONLY as part of Squeak, under
#   the terms of the Squeak License as described in `LICENSE' in the base of
#   this distribution, subject to the following additional restrictions:
# 
#   1. The origin of this software must not be misrepresented; you must not
#      claim that you wrote the original software.  If you use this software
#      in a product, an acknowledgment to the original author(s) (and any
#      other contributors mentioned herein) in the product documentation
#      would be appreciated but is not required.
# 
#   2. You must not distribute (or make publicly available by any
#      means) a modified copy of this file unless you first rename it.
# 
#   3. This notice must not be removed or altered in any source distribution.
# 
#   Using (or modifying this file for use) in any context other than Squeak
#   changes these copyright conditions.  Read the file `COPYING' in the
#   directory `platforms/unix/doc' before proceeding with any such use.
# 
# Author: ian.piumarta@inria.fr
# 
# Last edited: 2003-08-23 19:33:52 by piumarta on emilia.inria.fr


. ./config.sh

no_exports="disabledPlugins"
make_targets="make.targets"

la='${la}'
a='$a'
o='$o'

plugins=""
if test "${ext_modules}${ext_plugins}" != ""; then
  for p in ${ext_modules} ${ext_plugins}; do
    plugins="${plugins} ${p}${la}"
  done
fi

echo "" > ${make_targets}

targets=""
plibs=""

if test "${int_modules}${int_plugins}" != ""; then
  for p in ${int_modules} ${int_plugins}; do
    targets="${targets} ${p}/${p}$a"
    echo							  >> ${make_targets}
    echo "${p}/${p}$a : .force"					  >> ${make_targets}
    echo '	@$(SHELL) -ec '"'cd ${p}; "'$(MAKE) '"${p}$a'"	  >> ${make_targets}
    test -f ${p}.lib && plibs="${plibs} `cat ${p}.lib`"
  done
fi

if test "${ext_modules}${ext_plugins}" != ""; then
  for p in ${ext_modules} ${ext_plugins}; do
    echo							  >> ${make_targets}
    echo "${p}${la} :"						  >> ${make_targets}
    echo '	@$(SHELL) -ec '"'cd ${p}; "'$(MAKE) '"${p}${la}'" >> ${make_targets}
  done
fi

if test "${exc_plugins}" != ""; then
  targets="${targets} ${no_exports}$o"
  cat > ${no_exports}.c <<EOF
/* this should be in a header file, but it isn't.  ho hum. */
typedef struct {
  char *pluginName;
  char *primitiveName;
  void *primitiveAddress;
} sqExport;
`for p in ${exc_plugins}; do echo "sqExport ${p}_exports[] = { 0, 0, 0 };"; done | sed 's/-/_/g'`
EOF
  cat >> ${make_targets} <<EOF

${no_exports}$o : ${no_exports}.c
	\$(COMPILE) ${no_exports}$o ${no_exports}.c
EOF
fi

mv Makefile Makefile.tmp

sed "s%\[targets\]%${targets}%g
s%\[plugins\]%${plugins}%g
s%\[plibs\]%${plibs}%g
/\[make_targets\]/r ${make_targets}
s%\[make_targets\]%%g" < Makefile.tmp > Makefile

rm Makefile.tmp
