#!/bin/sh

#    Create Makefile: creates a Makefiles according to the C/C++ files found.
#
#    Copyright (C) 2011  Miguel Colom, miguel.colom@cmla.ens-cachan.fr
#    Thanks to Nicolas Limare (nicolas.limare@cmla.ens-cachan.fr) for his
#    helpful comments and improvements.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program 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.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Configuration
COPT="-O3"
CFLAGS="\$(COPT)"
CSTRICT="-Wall -Wextra -ansi"
CXXOPT="-O3"
CXXFLAGS="\$(COPT)"
CXXSTRICT="-Wall -Wextra -ansi"
mk='Makefile' # Makefile filename

# Get C and C++ files
c_files=$(find . -name "*.c" 2> /dev/null)
cxx_files=$(find . -name "*.cpp" 2> /dev/null)

num_c_files=$(echo $c_files | wc -w)
echo "There were found $num_c_files C files."

num_cxx_files=$(echo $cxx_files 2> /dev/null | wc -w)
echo "There were found $num_cxx_files C++ files."
echo

# Configure with the user
echo "The following C flags will be used:"
echo $COPT
echo "Type additional flags or type ENTER:"
read more_c_flags
echo "C flags that will be used to compile:"
echo $COPT $more_c_flags
CFLAGS="\$(COPT) $more_c_flags"
echo

echo "The following C++ flags will be used:"
echo $CXXOPT
echo "Type additional flags or type ENTER:"
read more_cxx_flags
echo "C++ flags that will be used to compile:"
echo $CXXOPT $more_cxx_flags
CXXFLAGS="\$(CXXOPT) $more_cxx_flags"
echo

echo "Type library options to use type ENTER. Ex: -lpng -lgomp -lfftw3f"
read libs
echo "Libraries to link flags:"
echo $libs
echo

EXEC=''
while [ ! $EXEC ]
do
  echo "Type the name of the executable name:"
  read EXEC
done

d=`date +%d/%m/%Y`
echo "# Makefile created with Create Makefile.sh $d" > $mk
echo >> $mk

echo "COPT      = $COPT" >> $mk
echo "CFLAGS    = $CFLAGS" >> $mk
echo "CSTRICT   = $CSTRICT" >> $mk
echo "CXXOPT    = $CXXOPT" >> $mk
echo "CXXFLAGS  = $CXXFLAGS" >> $mk
echo "CXXSTRICT = $CXXSTRICT" >> $mk
echo "LDFLAGS   = $libs" >> $mk
echo "EXEC      = $EXEC" >> $mk
echo >> $mk
echo "default: \$(EXEC)" >> $mk
echo "all: \$(EXEC)" >> $mk
echo >> $mk

objects=''

echo "# ------- C files -------" >> $mk
for c_file in $c_files
do
  base=`basename $c_file .c`
  dir_name=`dirname $c_file`
  filename_no_ext="$dir_name/$base"
  objects="$objects $filename_no_ext.o"
  if [ -f $filename_no_ext.h ]
  then
    echo "$filename_no_ext.o: $filename_no_ext.c $filename_no_ext.h" >> $mk
  else
    echo "$filename_no_ext.o: $filename_no_ext.c" >> $mk
  fi

  echo "	\$(CC) \$(CFLAGS) -c $filename_no_ext.c -o $filename_no_ext.o" >> $mk
  echo >> $mk
done

echo "# ------- C++ files -------" >> $mk
for cpp_file in $cxx_files
do
  base=`basename $cpp_file .cpp`
  dir_name=`dirname $cpp_file`
  filename_no_ext="$dir_name/$base"
  objects="$objects $filename_no_ext.o"
  if [ -f $filename_no_ext.h ]
  then
    echo "$filename_no_ext.o: $filename_no_ext.cpp $filename_no_ext.h" >> $mk
  else
    echo "$filename_no_ext.o: $filename_no_ext.cpp" >> $mk
  fi

  echo "	\$(CXX) \$(CXXFLAGS) -c $filename_no_ext.cpp -o $filename_no_ext.o" >> $mk
  echo >> $mk
done

echo
echo "# ------- Main -------" >> $mk
echo "\$(EXEC): $objects" >> $mk
if [ 0 -lt $num_cxx_files ]; then
    echo "	\$(CXX) $objects \$(LDFLAGS) -o \$(EXEC)" >> $mk
else
    echo "	\$(CC) $objects \$(LDFLAGS) -o \$(EXEC)" >> $mk
fi
echo
echo >> $mk

echo "lint: " >> $mk
echo "	\$(MAKE) CFLAGS=\"\$(CFLAGS) \$(CSTRICT)\" CXXFLAGS=\"\$(CXXFLAGS) \$(CXXSTRICT)\"" >> $mk
echo >> $mk

echo "clean: " >> $mk
echo "	rm -f *.o" >> $mk
echo >> $mk

echo "distclean: clean" >> $mk
echo "	rm -f \$(EXEC)" >> $mk
echo >> $mk

