#!/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()
{
  printf "#---------------------------------------------------------------------#\n"
  printf "#                    $1\n"
  printf "#---------------------------------------------------------------------#\n"
}


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
      printf "$MOCFILE: $MOC_FROM\n"
      printf "\t\$(QT_MOC) -o $MOCFILE $MOC_FROM\n"
      printf "\n"
      printf "$BASE\$(OBJ_EXT): $MOCFILE\n"
      printf "\n"
    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
    printf "$BASE\$(EXE_EXT): $BASE\$(OBJ_EXT)\n"
    printf "\t\$(CGAL_CXX) \$(LIBPATH) \$(EXE_OPT)$BASE $BASE\$(OBJ_EXT) \$(LDFLAGS)\n"
    printf "\n"
  fi;
}


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

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

  # print linker flags
  printf "\n"
  header "linker flags"
  printf "\n"
  printf "LIBPATH = \\\\\n"
  if [ ! -z "$TESTSUITE" ] ; then
    printf "          \$(TESTSUITE_LIBPATH) \\\\\n"
  fi
  printf "          \$(CGAL_LIBPATH)\n"
  printf "\n"
  printf "LDFLAGS = \\\\\n"
  if [ ! -z "$TESTSUITE" ] ; then
    printf "          \$(TESTSUITE_LDFLAGS) \\\\\n"
  fi
  printf "          \$(LONG_NAME_PROBLEM_LDFLAGS) \\\\\n"
  printf "          \$(CGAL_LDFLAGS)\n"

  printf "\n"
  header "target entries"

  # print 'all' target
  printf "\n"
  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
  printf "\n"

  # print rules for each .C and .cpp file
  printf "\n"
  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"
  printf "\n"
  printf ".C\$(OBJ_EXT):\n"
  printf "\t\$(CGAL_CXX) \$(CXXFLAGS) \$(OBJ_OPT) \$<\n"
  printf "\n"
  printf "\n"
  printf ".cpp\$(OBJ_EXT):\n"
  printf "\t\$(CGAL_CXX) \$(CXXFLAGS) \$(OBJ_OPT) \$<\n"
  printf "\n"
}


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 ..."
