#!/bin/sh

# Copyright (c) 1999,2000,2002-2007
# 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.3-branch/Scripts/scripts/cgal_create_makefile $
# $Id: cgal_create_makefile 36976 2007-03-09 22:53:24Z reichel $
#
# 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 CGAL makefile for the test suite
# -q   create a CGAL makefile with Qt support


#VERSION=2.0


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


create_makefile_entry()
{
#  local CFILE MOC_FROM MOCFILE
  BASE=`basename $1 .C`
  BASE=`basename $BASE .cpp`
  CFILE=$1
  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=${BASE}.moc
      echo "$MOCFILE: $MOC_FROM"
      printf "\t\$(QT_MOC) -o $MOCFILE $MOC_FROM\n"
      echo
      echo "$BASE\$(OBJ_EXT): $MOCFILE"
      echo
    fi
  fi

  # Print target for $1 executable if $CFILE contains a function "main()"
  egrep '\bmain[ \t]*\(' $CFILE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "$BASE\$(EXE_EXT): $BASE\$(OBJ_EXT)"
    printf "\t\$(CGAL_CXX) \$(LIBPATH) \$(EXE_OPT)$BASE $BASE\$(OBJ_EXT) \$(LDFLAGS)\n"
    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
  echo    "          \$(CGAL_LIBPATH)"
  echo
  echo    "LDFLAGS = \\"
  if [ ! -z "$TESTSUITE" ] ; then
    echo    "          \$(TESTSUITE_LDFLAGS) \\"
  fi
  echo    "          \$(LONG_NAME_PROBLEM_LDFLAGS) \\"
  echo    "          \$(CGAL_LDFLAGS)"

  echo
  header "target entries"

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

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

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

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

}


usage()
{
     echo "Usage: cgal_create_makefile [-options] [outputfile]"
     echo
     echo "-d   create a default CGAL makefile"
     echo "-t   create a CGAL makefile for the test suite"
     echo "-q   create a CGAL makefile with Qt support"
}


case $# in
  0) usage
     exit 1
esac


for i do
  case $i in
    -d) ;;
    -t) TESTSUITE='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 ..."
