#!/bin/sh

# 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=$(ls *.c 2> /dev/null)
cxx_files=$(ls *.cpp 2> /dev/null)

num_c_files=$(ls *.c 2> /dev/null | wc -w)
echo "There were found $num_c_files C files."

num_cxx_files=$(ls *.cpp 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 "Library to link flags:"
echo $libs
echo

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

echo "# Makefile created with create_makefile.sh" > $mk
echo "# by Miguel Colom, 2011" >> $mk
echo "# miguel.colom@cmla.ens-cachan.fr" >> $mk
echo "#" >> $mk
echo "# GNU GLP v2 license" >> $mk
f=`date +%d/%m/%Y`
echo "# Makefile created $f" >> $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 "CPPFLAGS  = " >> $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`
  objects="$objects $base.o"
  if [ -f $base.h ]
  then
    echo "$base.o: $base.c $base.h" >> $mk
  else
    echo "$base.o: $base.c" >> $mk
  fi

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

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

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

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
