#!/bin/sh

# mkmf -- configure subdirectory Makefiles			-*- sh -*-
# 
#   Copyright (C) 1996-2003 Ian Piumarta and other authors/contributors
#     as listed elsewhere in this file.
#   All rights reserved.
#   
#     You are NOT ALLOWED to distribute modified versions of this file
#     under its original name.  If you want to modify it and then make
#     your modifications available publicly, rename the file first.
# 
#   This file is part of Unix Squeak.
# 
#   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-02-06 03:14:37 by piumarta on emilia.local.

. ./config.sh

findFiles() {
  suffix=$1
  shift
  while test $# -gt 0; do
    echo $1/*${suffix}
    shift
  done
}

findDirs() {
  prefix=$1
  shift
  dirs=""
  while test $# -gt 0; do
    dirs="${dirs} ${prefix}`dirname $1`"
    shift
  done
  echo ${dirs} | tr ' ' '\012' | sort | uniq | tr '\012' ' '
}

plibs=""

mkmf() {
  default=$1
  shift
  name=$1
  shift
  dirs="$*"
  makefile_in=""
  makefile_inc=""
  make_targets="${name}/make.targets"
  targets=""
  includes=""
  srcs=""
  hdrs=""
  echo "creating ${name}/Makefile"
  test -d ${name} || mkdir ${name}
  echo "" > ${make_targets}
  # collect source and header files
  for x in ${dirs}; do
    d=${x}/${name}
    if test -d ${d}; then
      dd=${d}
      if test -f ${d}/mkmf.subdirs; then
        subdirs="`cat ${d}/mkmf.subdirs | tr '\012' ' '`"
	for sd in ${subdirs}; do dd="${dd} ${topdir}/${sd}"; done
      fi
      srcs="${srcs} `findFiles .c ${dd}`"
      srcs="${srcs} `findFiles .S ${dd}`"
      srcs="${srcs} `findFiles .m ${dd}`"
      hdrs="${hdrs} `findFiles .h ${dd}`"
      # override Makefile.in
      if test -f "${d}/Makefile.in"; then
	makefile_in="${d}/Makefile.in"
      fi
      # include Makefile.inc
      if test -f "${d}/Makefile.inc"; then
	makefile_inc="${makefile_inc} ${d}/Makefile.inc"
      fi
    fi
  done
  srcs="`echo ${srcs} | tr ' ' '\012' | fgrep -v '*'`"
  hdrs="`echo ${hdrs} | tr ' ' '\012' | fgrep -v '*'`"
  # create targets and rules
  if test "${srcs}" != ""; then
    for c in ${srcs}; do
      o=`basename ${c} | sed 's,\.c,$o,;s,\.m,$o,;s,\.S,$o,'`
      targets="${targets} ${o}"
      echo							>> ${make_targets}
      echo "${o} : ${c}"					>> ${make_targets}
      echo '	$(COMPILE) '"${o} ${c}"				>> ${make_targets}
    done
  fi
  # create includes
  if test "${hdrs}" != ""; then
    includes="${includes} `findDirs -I ${hdrs}`"
  fi
  # default Makefile.in if no override
  if test ! -f "${makefile_in}"; then
    makefile_in=${cfgdir}/Makefile.plg.in
  fi
  # include Makefile.inc(s)
  if test "${makefile_inc}" != ""; then
    cp ${makefile_in} makefile.in
    makefile_in="makefile.in"
    for inc in ${makefile_inc}; do
      sed "/\[make_inc\]/r ${inc}" < ${makefile_in} > makefile.out
      mv makefile.out makefile.in
    done
  fi
  # substitutions
  if test -f "${name}.sub"; then
    sed -f ${name}.sub < ${makefile_in} > makefile.sub
    makefile_in="makefile.sub"
  fi
  sed "s%\[make_inc\]%%g
s%\[target\]%${name}"'$a'"%g
s%\[targets\]%${targets}%g
s%\[includes\]%${includes}%g
/\[make_cfg\]/r ${blddir}/make.cfg
s%\[make_cfg\]%%g
/\[make_prg\]/r ${bldir}/make.prg
s%\[make_prg\]%%g
/\[make_plg\]/r ${blddir}/make.${default}
s%\[make_plg\]%%g
s%\[plibs\]%${plibs}%g
/\[make_targets\]/r ${make_targets}
s%\[make_targets\]%%g" < ${makefile_in} > ${name}/Makefile
  /bin/rm -f ${make_targets} makefile.in makefile.sub
}

if test "${int_modules}" != ""; then
  for p in ${int_modules}; do
    mkmf int ${p} ${vmi_dirs}
  done
fi

if test "${ext_modules}" != ""; then
  for p in ${ext_modules}; do
    if test -f ${p}.lib; then
      plibs="`cat ${p}.lib`"
    else
      plibs=""
    fi
    mkmf ext ${p} ${vmi_dirs}
  done
fi

if test "${int_plugins}" != ""; then
  for p in ${int_plugins}; do
    mkmf int ${p} ${int_dirs}
  done
fi

if test "${ext_plugins}" != ""; then
  for p in ${ext_plugins}; do
    if test -f ${p}.lib; then
      plibs="`cat ${p}.lib`"
    else
      plibs=""
    fi
    mkmf ext ${p} ${ext_dirs}
  done
fi
