#!/bin/bash

love_suffix="$1"
love_amsuffix="$(echo "$love_suffix" | sed 's/\-/_/g' | sed 's/\./_/g')"

flags=()
upper()
{
	echo "$@" | tr '[:lower:]' '[:upper:]'
}

sourcefind()
{
	find "$1" $2 -type f \( -iname "*.c" -o -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" -o -iname "*.lch" -o -iname "*.lua" \) | awk "{print \"./$prefix\"\$0\" \\\\\"}" | grep -v -f"$LOVEROOT/platform/unix/exclude" | sort
}

handlemodule()
{
	module="$1"
	DEFINENAME="LOVE_MODULE_$(upper "$module")"
	printf "$DEFINENAME"
}

genmodules()
{
	LOVEROOT="$(pwd)"
	cd ./src

	printf "liblove${love_amsuffix}_la_SOURCES = \\\\\n"
	sourcefind "common" | sed "s/^/    /"
	FILES="$(sourcefind "scripts" | sed "s/^/    /")"
	printf "${FILES:0:${#FILES}-2}\n\n"

	local -a modulelist=()
	local -a liblist=()

	cd ./modules
	prefix="modules/"
	for module in *; do
		flags+=("module-$module")

		FILES="$(sourcefind "$module" | sed "s/^/    /")"
		if [[ "x$FILES" != "x" ]]; then
			printf "liblove_module_$module = \\\\\n"
			printf "${FILES:0:${#FILES}-2}\n\n"
			modulelist+=("$module")
		fi
	done

	cd ../libraries
	prefix="libraries/"
	for library in *; do
		flags+=("library-$library")
		FILES="$(sourcefind "$library" | sed "s/^/    /")"

		if [[ ${#FILES} -gt 2 ]]; then
			printf "liblove_library_$library = \\\\\n"
			printf "${FILES:0:${#FILES}-2}\n\n"
			liblist+=("$library")
		fi
	done

	for module in "${modulelist[@]}"; do
		NAME=$(handlemodule "$module")
		printf "if $NAME\n"
		printf "liblove${love_amsuffix}_la_SOURCES += \$(liblove_module_$module)\n"
		printf "endif\n\n"
	done

	for library in "${liblist[@]}"; do
		NAME="LOVE_LIBRARY_$(upper "$library")"
		printf "if $NAME\n"
		printf "liblove${love_amsuffix}_la_SOURCES += \$(liblove_library_$library)\n"
		printf "endif\n\n"
	done

	cd ../..
}

genflags()
{
	printf > configure-modules-pre.ac
	printf > configure-modules-post.ac
	for flag in "${flags[@]}"; do
		prettyflag="$(echo "$flag" | sed -e 's/-/ love./' -e 's/-/./g')"
		varflag="enable_$(echo "$flag" | sed -e 's/[^a-zA-Z0-9]/_/')"
		defineflag="LOVE_ENABLE_$(upper $(echo $flag | sed -e 's/^[^-]*-//' -e 's/-/_/g'))"
		amflag="$(upper $(echo love-$flag | sed 's/-/_/g'))"

		# Don't generate an --enable rule for libraries
		if [[ "$(echo $flag | sed -e '/^library-/d')" != "" ]]; then
			printf "AC_ARG_ENABLE([$flag], [  --disable-$flag    Turn off $prettyflag], [], [$varflag=yes])\n" >> configure-modules-pre.ac
		fi

		printf "AH_TEMPLATE([$defineflag], [])\n" >> configure-modules-post.ac
		printf "if test x\"\$$varflag\" = xyes; then\n" >> configure-modules-post.ac
		printf "    AC_DEFINE([$defineflag], [])\n" >> configure-modules-post.ac
		printf "fi\n" >> configure-modules-post.ac
		printf "AM_CONDITIONAL([$amflag], [test x\$$varflag = xyes])\n\n" >> configure-modules-post.ac
	done
}

echo Generating src/Makefile.am ...
inc_current='$(srcdir)'
inc_modules="$inc_current/modules"
inc_libraries="$inc_current/libraries"

cat > src/Makefile.am << EOF
AM_CPPFLAGS = -I$inc_current -I$inc_modules -I$inc_libraries -I$inc_libraries/enet/libenet/include -I$inc_libraries/box2d \$(LOVE_INCLUDES) \$(FILE_OFFSET)\
	\$(SDL_CFLAGS) \$(lua_CFLAGS) \$(freetype2_CFLAGS) \$(harfbuzz_CFLAGS)\
	\$(openal_CFLAGS) \$(zlib_CFLAGS) \$(libmodplug_CFLAGS)\
	\$(vorbisfile_CFLAGS) \$(theora_CFLAGS)
AUTOMAKE_OPTIONS = subdir-objects
SUBDIRS =
SUFFIXES = .lua .lua.h

if LOVE_BUILD_EXE
# LÖVE executable
bin_PROGRAMS = love${love_suffix}
#love_LDFLAGS =
love${love_amsuffix}_LDADD = liblove${love_suffix}.la \$(lua_LIBS)
love${love_amsuffix}_SOURCES = love.cpp

if LOVE_TARGET_OSX
love${love_amsuffix}_LIBTOOLFLAGS = --tag=OBJCXX
love${love_amsuffix}_SOURCES += \\
    ./common/macosx.mm
else
love${love_amsuffix}_LIBTOOLFLAGS = --tag=CXX
endif
endif

# Compile scripts
.lua.lua.h:
	cd ./scripts; \
	\$(LUA_EXECUTABLE) auto.lua \$<

# libLÖVE
lib_LTLIBRARIES = liblove${love_suffix}.la
liblove${love_amsuffix}_la_LDFLAGS = -module -export-dynamic \$(LDFLAGS) -release \$(PACKAGE_VERSION)
liblove${love_amsuffix}_la_LIBADD = \
	\$(SDL_LIBS) \$(freetype2_LIBS) \$(harfbuzz_LIBS) \$(lua_LIBS)\
	\$(openal_LIBS) \$(zlib_LIBS) \$(libmodplug_LIBS)\
	\$(vorbisfile_LIBS) \$(theora_LIBS)

EOF

genmodules >> src/Makefile.am

echo "src/Makefile.am is updated! ^.^"

echo "Generating configure-modules.ac"
genflags
cat >> configure-modules-post.ac << EOF
AC_SUBST([LOVE_SUFFIX], [${love_suffix}])
EOF
echo "configure-modules.ac is updated! ^.^"
