#!/bin/bash
#===============================================================================
#
#          FILE:  projectGenerator.sh
# 
#         USAGE:  ./projectGenerator.sh 
# 
#   DESCRIPTION:  Creates a project.inc Makefile fragment for a given directory
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  John Ryland (jryland@xiaofrog.com)
#       COMPANY:  InvertedLogic
#       VERSION:  1.0
#       CREATED:  19/07/2008 23:55:13 CST
#      REVISION:  ---
#===============================================================================
SUBDIR=$1
TOPMAKE="Makefile"
if [ ! -f $1/Makefile ]
then
    while [ $SUBDIR != "." ]
    do
        SUBDIR=`dirname $SUBDIR`
        TOPMAKE="../$TOPMAKE"
    done
    # echo "$1 -> $TOPMAKE" 1>&2
    cd $1 > /dev/null
    ln -s $TOPMAKE ./Makefile > /dev/null
    cd - > /dev/null
fi

TYPE=subdirs
# I think some of these finds could be done with something like $(wildcard $(dir)/*) function of make
MMSOURCES=`find ./$1 -name "*.mm" -depth 1 | tr '\n' ' '`
CSOURCES=`find ./$1 -name "*.c" -depth 1 | tr '\n' ' '`
SOURCES=`find ./$1 -name "*.cpp" -depth 1 | tr '\n' ' '`
HEADERS=`find ./$1 -name "*.h"   -depth 1 | tr '\n' ' '`
TESTS=`if [ -d ./$1/tests ]; then find ./$1/tests -name '*.cpp' -depth 1 | tr '\n' ' '; fi`
FILES=`echo "$MMSOURCES $CSOURCES $SOURCES $HEADERS" | wc -w`
if [ $FILES != 0 ] ; then
  grep -c main $MMSOURCES $CSOURCES $SOURCES > /dev/null
  if [ "$?" = "1" ] ; then
    TYPE=lib
    TARGET="\$(BASE)/$1/lib`basename $1`.a"
  else
    TYPE=app
    TARGET="\$(BASE)/$1/`basename $1`"
  fi
fi

echo "###################################################################"
echo "# Autogenerated file. DO NOT EDIT."
echo "# Created on `date`"
echo "# using `uname -nrs`"
echo "# by $0"
echo "# from $PWD/Makefile."
echo "###################################################################"
echo
echo "$1_TEMPLATE  = $TYPE"
if [ "$TYPE" != "subdirs" ] ; then
echo "$1_TESTS     = $TESTS"
echo "$1_SOURCES   = $SOURCES"
echo "$1_CSOURCES  = $CSOURCES"
echo "$1_MMSOURCES = $MMSOURCES"
echo "$1_HEADERS   = $HEADERS"
echo "$1_TARGET    = $TARGET"
# echo "$1_DEPENDS   := "`cat $1/depends 2> /dev/null`
echo "$1_DEPENDS   := "`cat $1/depends 2> /dev/null`
echo
echo "$1_OBJECTS   = \$(patsubst ./%.cpp, \$(OBJSDIR)/%.o, \$($1_SOURCES))"
echo "$1_OBJECTS  += \$(patsubst ./%.c,   \$(OBJSDIR)/%.o, \$($1_CSOURCES))"
echo "$1_OBJECTS  += \$(patsubst ./%.mm,  \$(OBJSDIR)/%.o, \$($1_MMSOURCES))"
echo "$1_INCLUDES  = \$(patsubst %,       -I\$(BASE)/%,    \$($1_DEPENDS)) -I\$(BASE)/$1"
echo "$1_LIBS      = \$(foreach lib,\$($1_DEPENDS),-L\$(BASE)/\$(lib)  -l\$(lastword \$(subst /, ,\$(lib))) )"
echo "$1_DEPS      = \$(foreach lib,\$($1_DEPENDS),  \$(BASE)/\$(lib)/lib\$(lastword \$(subst /, ,\$(lib))).a )"
echo
echo "TESTS        += \$($1_TESTS)"
echo "SOURCES      += \$($1_SOURCES)"
echo "HEADERS      += \$($1_HEADERS)"
echo "TARGETS      += \$($1_TARGET)"
echo "INCLUDES     += \$($1_INCLUDES)"
echo
# echo "\$($1_OBJECTS): \$($1_DEPS)"
# echo -e "\t\$(CXX) \$(CXXFLAGS) \$($1_INCLUDES) -c \$< -o \$@"
echo
echo "$TARGET: \$($1_OBJECTS) \$(LINK_DEPS) \$($1_DEPS)"
if [ "$TYPE" = "lib" ] ; then
  echo -e "\t\$(AR) \$@ \$($1_OBJECTS)"
else
  echo -e "\t\$(LINK) \$(LFLAGS) \$($1_LIBS) -o \$@ \$($1_OBJECTS)"
  echo -e "\t\$(STRIP) -S \$@"
fi
fi
echo

