#!/bin/sh

# Copyright (c) 1999,2000,2002,2003,2004,2005,2006
# Utrecht University (The Netherlands),
# ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
# INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
# (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
# and Tel-Aviv University (Israel).  All rights reserved.
#
# This file is part of CGAL (www.cgal.org); you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; version 2.1 of the License.
# See the file LICENSE.LGPL distributed with CGAL.
#
# Licensees holding a valid commercial license may use this file in
# accordance with the commercial license agreement provided with the software.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Scripts/scripts/cgal_create_makefile $
# $Id: cgal_create_makefile 32105 2006-06-28 09:00:47Z afabri $
#
# Author(s)     : various

# This script creates a CGAL makefile with entries for all .C and .cpp files in the
# current directory.
#
# Usage: cgal_create_makefile [-options] [outputfile]
#
# -d   create a default CGAL makefile
# -t   create a makefile for the test suite
# -w   create a makefile with flags for LEDA windows
# -g   create a makefile with flags for GEOWIN
# -q   create a makefile with support for QT


#VERSION=2.0


header()
{
  echo "#---------------------------------------------------------------------#"
  echo "#                    $1"
  echo "#---------------------------------------------------------------------#"
}


create_makefile_entry_cpp()
{
#  local CFILE MOC_FROM MOCFILE
  CFILE=$1.cpp
  MOCFILE=""
  if [ -n "$QT" ]; then
    if MOC_FROM=`awk 'BEGIN{ FS=":"; COUNT=0 } \
            /\/\/ *moc_source_file *:/ {print $2; COUNT++} \
            END{ exit COUNT==0 }' $CFILE`; then
      MOCFILE=$1.moc
      echo "$MOCFILE: $MOC_FROM"
      echo "	\$(QT_MOC) -o $MOCFILE $MOC_FROM"
      echo
      echo "$1\$(OBJ_EXT): $MOCFILE"
      echo
    fi
  fi

  # Print target for $1 object file
  echo "$1\$(OBJ_EXT): $1.cpp"
  echo "	\$(CGAL_CXX) \$(CXXFLAGS) \$(OBJ_OPT) $1.cpp"
  echo

  # Print target for $1 executable if $CFILE contains a function "main()"
  egrep '\bmain[ \t]*\(' $CFILE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "$1\$(EXE_EXT): $1\$(OBJ_EXT)"
    echo "	\$(CGAL_CXX) \$(LIBPATH) \$(EXE_OPT)$1 $1\$(OBJ_EXT) \$(LDFLAGS)"
    echo
  fi;
}

create_makefile_entry_C()
{
#  local CFILE MOC_FROM MOCFILE
  CFILE=$1.C
  MOCFILE=""
  if [ -n "$QT" ]; then
    if MOC_FROM=`awk 'BEGIN{ FS=":"; COUNT=0 } \
            /\/\/ *moc_source_file *:/ {print $2; COUNT++} \
            END{ if (COUNT==0) exit 1; else exit 0 }' $CFILE`; then
      MOCFILE=$1.moc
      echo "$MOCFILE: $MOC_FROM"
      echo "	\$(QT_MOC) -o $MOCFILE $MOC_FROM"
      echo
      echo "$1\$(OBJ_EXT): $MOCFILE"
      echo
    fi
  fi

  # Target for $1 object file is global

  # Print target for $1 executable if $CFILE contains a function "main()"
  egrep '\bmain[ \t]*\(' $CFILE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "$1\$(EXE_EXT): $1\$(OBJ_EXT)"
    echo "	\$(CGAL_CXX) \$(LIBPATH) \$(EXE_OPT)$1 $1\$(OBJ_EXT) \$(LDFLAGS)"
    echo
  fi;
}


create_makefile()
{
  # print makefile header
  echo "# Created by the script cgal_create_makefile"
  echo "# This is the makefile for compiling a CGAL application."
  echo
  header "include platform specific settings"
  echo    "# Choose the right include file from the <cgalroot>/make directory."
  echo
  echo    "# CGAL_MAKEFILE = ENTER_YOUR_INCLUDE_MAKEFILE_HERE"
  echo    "include \$(CGAL_MAKEFILE)"

  # print compiler flags
  echo
  header "compiler flags"
  echo
  echo    "CXXFLAGS = \\"
  if [ -d include ] ; then
    echo  "           -Iinclude \\"
  fi
  echo    "           -I../../include \\"
  if [ ! -z "$TESTSUITE" ] ; then
    echo    "           \$(TESTSUITE_CXXFLAGS) \\"
    echo    "           \$(EXTRA_FLAGS) \\"
  fi
  echo    "           \$(CGAL_CXXFLAGS) \\"
  echo    "           \$(LONG_NAME_PROBLEM_CXXFLAGS)"

  # print linker flags
  echo
  header "linker flags"
  echo
  echo    "LIBPATH = \\"
  if [ ! -z "$TESTSUITE" ] ; then
    echo    "          \$(TESTSUITE_LIBPATH) \\"
  fi
  if [ -z "$LEDAWINDOWS" ] ; then
    echo    "          \$(CGAL_LIBPATH)"
  else
    echo    "          \$(CGAL_WINDOW_LIBPATH)"
  fi
  echo
  echo    "LDFLAGS = \\"
  if [ ! -z "$TESTSUITE" ] ; then
    echo    "          \$(TESTSUITE_LDFLAGS) \\"
  fi
  echo    "          \$(LONG_NAME_PROBLEM_LDFLAGS) \\"
  if [ -n "$QT" ] ; then
    echo    "          \$(CGAL_QT_LDFLAGS)"
  elif [ -n "$GEOWIN" ] ; then
    echo    "          \$(CGAL_GEOWIN_LDFLAGS)"
  elif [ -z "$LEDAWINDOWS" ] ; then
    echo    "          \$(CGAL_LDFLAGS)"
  else
    echo    "          \$(CGAL_WINDOW_LDFLAGS)"
  fi

  echo
  header "target entries"

  # print 'all' target
  echo
  printf "all:            "
  for file in `ls *.C 2>/dev/null | sort` ; do
    # Add $file's executable to "all" target
    # if $file contains a function "main()"
    egrep '\bmain[ \t]*\(' $file >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      printf "\\\\\n                `basename $file .C`\$(EXE_EXT) "
    fi
  done
  for file in `ls *.cpp 2>/dev/null | sort` ; do
    # Add $file's executable to "all" target
    # if $file contains a function "main()"
    egrep '\bmain[ \t]*\(' $file >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      printf "\\\\\n                `basename $file .cpp`\$(EXE_EXT) "
    fi
  done
  echo

  # print rules for each .C and .cpp file
  echo
  for file in `ls *.C 2>/dev/null | sort` ; do
    base=`basename $file .C`
    create_makefile_entry_C $base
  done
  for file in `ls *.cpp 2>/dev/null | sort` ; do
    base=`basename $file .cpp`
    create_makefile_entry_cpp $base
  done

  # print 'clean' target
  printf "clean: "
  for file in `ls *.C 2>/dev/null | sort` ; do
      printf "\\\\\n                   `basename $file .C`.clean "
  done
  for file in `ls *.cpp 2>/dev/null | sort` ; do
      printf "\\\\\n                   `basename $file .cpp`.clean "
  done
  printf    "\n\n"

  # print "suffix rules"
  header "suffix rules"
  echo
  echo    ".C\$(OBJ_EXT):"
  echo    "	\$(CGAL_CXX) \$(CXXFLAGS) \$(OBJ_OPT) \$<"
  echo
  echo
  echo    ".cpp\$(OBJ_EXT):"
  echo    "	\$(CGAL_CXX) \$(CXXFLAGS) \$(OBJ_OPT) \$<"
  echo

}


usage()
{
     echo "Usage: cgal_create_makefile [-options] [outputfile]"
     echo
     echo "-d   create a default CGAL makefile"
     echo "-t   create a makefile for the test suite"
     echo "-w   create a makefile with flags for LEDA windows"
     echo "-g   create a makefile with flags for GEOWIN"
     echo "-q   create a makefile with flags for QT"
}


case $# in
  0) usage
     exit 1
esac


for i do
  case $i in
    -d) ;;
    -t) TESTSUITE='y';;
    -w) LEDAWINDOWS='y';;
    -g) GEOWIN='y';;
    -q) QT='y';;
    -*) usage
        exit 1;;
    *) if [ -z "$OUTPUTFILE" ] ; then
         OUTPUTFILE=$i
       else
         usage
         exit 1
       fi;;
  esac
done


if [ -z "$OUTPUTFILE" ] ; then
  OUTPUTFILE=makefile
fi


if [ -f ${OUTPUTFILE} ] ; then
  echo "moving $OUTPUTFILE to ${OUTPUTFILE}.bak ..."
  mv -f $OUTPUTFILE ${OUTPUTFILE}.bak
fi
create_makefile > $OUTPUTFILE
echo "created $OUTPUTFILE ..."
