#!/bin/sh

################################################################################
## This file is part of CodeLite IDE and is released
## under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
################################################################################

createRevisionFile()
{
	chmod +x ./le_makerevision.sh
	./le_makerevision.sh
}

createVariables()
{
	## set the compiler name
	#  Note the lack of -fPIC. Only use this for .so libs; strange and random bugs occur otherwise
	compiler_name="g++ "
	c_compiler_name="gcc "
	exe_name="Runtime/codelite"
	exe_name1="codelite"
	is_so_lib=$1
	additional_include_path=$2
	additional_links=$3
	
	if [ "$os_name" = "Darwin" ]; then
		if [ "$is_64bit" = "yes" ]; then
			compiler_name=${compiler_name}" -m64 "
			c_compiler_name=${c_compiler_name}" -m64 "
		else
			compiler_name=${compiler_name}" -m32 "
			c_compiler_name=${c_compiler_name}" -m32 "
		fi
	fi

	if [ "$is_64bit" = "yes" ]; then
		compiler_name=${compiler_name}" -DON_64_BIT "
		c_compiler_name=${c_compiler_name}" -DON_64_BIT "
	fi

	## If the non-plugin .so libs aren't installed with the plugin ones, add their dir to the rpath
	rpaths="-Wl,-rpath,$plugins_dir"
	if [ "$non_plugins_dir" != "$plugins_dir" ]; then
		rpaths="$rpaths -Wl,-rpath,$non_plugins_dir"
	fi

	## set the executable name
	if [ "$enable_debug" = "yes" ]; then
		objects_dir="Debug_gcc_unicode"
		configuration="Debug_gcc_unicode"
		debug_flag=" -g "
		ext=ud
		optimization=" -DREGEXP_UNICODE "
		wxcfg="--unicode=yes --debug=yes"
	else
		objects_dir="Release_gcc_unicode"
		configuration="Release_gcc_unicode"
		ext=u
		debug_flag=""
		optimization=" -O3 -DREGEXP_UNICODE "
		wxcfg="--unicode=yes --debug=no"
	fi

	echo "OPT=${optimization}" >> Makefile
	echo "DEBUG=${debug_flag}" >> Makefile
	echo "CMP=${compiler_name} \$(DEBUG) \$(OPT) " >> Makefile
	echo "GCC=${c_compiler_name}" >> Makefile
	echo "EXE_NAME=${exe_name}" >> Makefile
	echo "EXE_NAME_NO_PATH=${exe_name1}" >> Makefile
	echo "OBJ_DIR=${objects_dir}" >> Makefile
	echo "EXT=${ext}" >> Makefile
	echo "OUTPUT_DIR=lib" >> Makefile
	echo "WXCFG=${wxcfg}" >> Makefile
	echo "DESTDIR=" >> Makefile
	echo "" >> Makefile
	echo "CCFLAGS= -Wall \$(shell wx-config --cxxflags \$(WXCFG)) -fno-strict-aliasing -DASTYLE_LIB -DYY_NEVER_INTERACTIVE=1 -DGTK -DSCI_LEXER -DLINK_LEXERS -D__WX__ -DINSTALL_DIR=\\\"${prefix}/share/codelite\\\" -DPLUGINS_DIR=\\\"${plugins_dir}\\\"" >> Makefile
	echo "LINK_FLAGS =  -L${base_dir}/\$(OUTPUT_DIR) ${rpaths} -lplugin\$(EXT) -lcodelite\$(EXT) -lwxsqlite3\$(EXT) -lwxscintilla\$(EXT) -L${base_dir}/sdk/sqlite3/lib \$(shell ${wxconfiglibs} \$(WXCFG))" >> Makefile
	echo "PLUGIN_LINK_FLAGS =  -L${base_dir}/\$(OUTPUT_DIR) \$(shell ${wxconfiglibs} \$(WXCFG)) ${additional_links} " >> Makefile
	echo "INCLUDES := \$(INCLUDES) ${additional_include_path} " >> Makefile
	echo "" >> Makefile
	if [ "$os_name" != "Darwin" ] ; then
		echo "INCLUDES := \$(INCLUDES) \$(shell pkg-config --cflags gtk+-2.0)" >> Makefile
		echo "LINK_FLAGS := \$(LINK_FLAGS) \$(shell pkg-config --libs gtk+-2.0)" >> Makefile
		echo "PLUGIN_LINK_FLAGS := \$(PLUGIN_LINK_FLAGS) \$(shell pkg-config --libs gtk+-2.0)" >> Makefile
	else
		echo "CCFLAGS := \$(CCFLAGS) -D__APPLE__ -DMACOSX" >> Makefile
	fi
	if [ "$os_name" = "FreeBSD" ]; then
		echo "LINK_FLAGS += -lkvm" >> Makefile
	fi
	echo "INCLUDES := \$(INCLUDES) -I. -I${base_dir}/sdk/codelite_indexer/network -I${base_dir}/sdk/wxsqlite3/include -I${base_dir}/sdk/wxsqlite3/sqlite3/include -I${base_dir}/sdk/wxscintilla/include -I${base_dir}/sdk/wxscintilla/src/scintilla/include -I${base_dir}/sdk/wxscintilla/src/scintilla/src -I${base_dir}/Interfaces -I${base_dir}/Debugger -I${base_dir}/Plugin -I${base_dir}/CodeLite " >> Makefile
	echo "" >> Makefile
	echo "" >> Makefile
}

createMainTargets()
{
	echo "## build targets" >> Makefile

	## default
	target_all="all :  pre_build SQLite Scintilla CodeLite SDK codelite_indexer codelite_cppcheck Plugins LiteEditor "

	## if we are on Linux, print the post message
	if [ "$os_name" = "Linux" ]; then
		target_all="$target_all"
	fi

	if [ "$os_name" = "Darwin" ]; then
		target_all="$target_all Plugins mac_bundle"
	fi

	## add the "all" target to the makefile
	echo "${target_all}" >> Makefile
	echo "" >> Makefile

	echo "codelite_indexer:" >> Makefile
	echo "	@cd sdk/codelite_indexer/ && \$(MAKE)" >> Makefile
	echo "	cp -fr sdk/codelite_indexer/codelite_indexer Runtime/." >> Makefile
	echo "" >> Makefile

	echo "codelite_cppcheck:" >> Makefile
	echo "	@cd sdk/codelite_cppcheck/ && \$(MAKE)" >> Makefile
	echo "	cp -fr sdk/codelite_cppcheck/codelite_cppcheck Runtime/." >> Makefile
	echo "" >> Makefile

	echo "pre_build:" >> Makefile
	echo "	./le_makerevision.sh" >> Makefile
	echo "	@test -d  \$(OBJ_DIR) || mkdir \$(OBJ_DIR)" >> Makefile
	echo "	@test -d  \$(OUTPUT_DIR) || mkdir \$(OUTPUT_DIR)" >> Makefile
	echo "" >> Makefile

	echo "clean: clean_plugins" >> Makefile
	echo "	@cd sdk/codelite_indexer && \$(MAKE) clean" >> Makefile
	echo "	@cd sdk/codelite_cppcheck && \$(MAKE) clean" >> Makefile
	echo "	\$(RM) \$(OUTPUT_DIR)/*.a \$(OUTPUT_DIR)/*.so" >> Makefile
	echo "	\$(RM) -fr \$(OBJ_DIR)" >> Makefile
	echo "" >> Makefile

	if [ "$os_name" = "Darwin" ]; then
		echo "mac_bundle: " >> Makefile
		echo "	@echo Creating Mac bundle..." >> Makefile
		echo "	@cd Runtime && ./make_mac_bundle.sh" >> Makefile
		echo "	@echo done" >> Makefile
		echo "" >> Makefile
	fi
}

createObjectList ()
{
	files_list=$1
	object_list_name=$2

	echo "${object_list_name} = \\"  >> Makefile
	for a_file in $files_list
	do
		a_file_no_ext=`basename $a_file .cxx`
		a_file_no_ext=`basename $a_file_no_ext .cpp`
		a_file_no_ext=`basename $a_file_no_ext .c`
		echo "	${configuration}/${a_file_no_ext}.o\\" >> Makefile
	done
	echo >> Makefile
}

createCodeLiteCppcheckMakefile ()
{
	files_list=$1
	echo "Generating sdk/codelite_cppcheck/Makefile..."
	current_wd=`pwd`

	cd sdk/codelite_cppcheck/

	## remove old Makefile
	rm -fr Makefile

	compiler_name="g++ "
	c_compiler_name="gcc "
	if [ "$is_64bit" = "yes" ]; then
		compiler_name=${compiler_name}" -DON_64_BIT "
		c_compiler_name=${c_compiler_name}" -DON_64_BIT "
	fi
	## set the executable name
	if [ "$enable_debug" = "yes" ]; then
		debug_flag=" -g "
		configuration="Debug"
		optimization=" -O0 "
	else
		configuration="Release"
		debug_flag=""
		optimization=" -O2 "
	fi

	echo "OPT=${optimization}" >> Makefile
	echo "DEBUG=${debug_flag}" >> Makefile
	echo "CMP=${compiler_name} \$(DEBUG) \$(OPT) " >> Makefile
	echo "GCC=${c_compiler_name} \$(DEBUG) \$(OPT) " >> Makefile
	echo "" >> Makefile
	echo "all: codelite_cppcheck" >> Makefile

	createObjectList "cli/*.cpp lib/*.cpp" "codelite_cppcheck_objects"

	for a_file in $files_list
	do
		a_file_with_ext=`basename $a_file`
		a_file_no_ext=`basename $a_file .cxx`
		a_file_no_ext=`basename $a_file_no_ext .cpp`
		a_file_no_ext=`basename $a_file_no_ext .c`
		file_extension=`basename $a_file | cut -d. -f2`

		if [ "$file_extension" = "c" ]; then
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(GCC) -c ${a_file} -I.  -o ${configuration}/${a_file_no_ext}.o -Ilib" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(GCC) -I.  -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file} -Ilib" >> Makefile
		else
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(CMP)  -c ${a_file} -I. -o ${configuration}/${a_file_no_ext}.o -Ilib" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(CMP) -I. -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file} -Ilib" >> Makefile
		fi

	done
	echo "-include ${configuration}/*.d" >> Makefile
	echo >> Makefile

	echo "codelite_cppcheck: make_dirs \$(codelite_cppcheck_objects)" >> Makefile

	## On FreeBSD, -ldl is part og libc
	if [ "$os_name" = "FreeBSD" ]; then
		echo "	\$(CMP) -o codelite_cppcheck \$(codelite_cppcheck_objects) -lpthread " >> Makefile
	else
		echo "	\$(CMP) -o codelite_cppcheck \$(codelite_cppcheck_objects) -lpthread -ldl " >> Makefile
	fi

	echo >> Makefile

	echo "make_dirs:" >> Makefile
	echo "	@echo" >> Makefile
	echo "	@echo \"----------Building project:[ codelite_cppcheck - ${configuration} ]----------\"" >> Makefile
	echo "	@echo" >> Makefile
	echo "	test -d ${configuration} || mkdir ${configuration}" >> Makefile
	echo >> Makefile

	echo "clean: " >> Makefile
	echo "	\$(RM) -fr ${configuration}/*.o" >> Makefile
	echo "	\$(RM) -fr ${configuration}/*.o.d" >> Makefile
	echo "	\$(RM) -fr codelite_cppcheck" >> Makefile

	## restore the path
	cd ${current_wd}
}

createCodeLiteIndexerMakefile ()
{
	files_list=$1
	echo "Generating sdk/codelite_indexer/Makefile..."
	current_wd=`pwd`

	cd sdk/codelite_indexer/

	## remove old Makefile
	rm -fr Makefile

	compiler_name="g++ "
	c_compiler_name="gcc "
	if [ "$is_64bit" = "yes" ]; then
		compiler_name=${compiler_name}" -DON_64_BIT "
		c_compiler_name=${c_compiler_name}" -DON_64_BIT "
	fi
	## set the executable name
	if [ "$enable_debug" = "yes" ]; then
		debug_flag=" -g "
		configuration="Debug"
		optimization=" -O0 "
	else
		configuration="Release"
		debug_flag=""
		optimization=" -O2 "
	fi

	echo "OPT=${optimization}" >> Makefile
	echo "DEBUG=${debug_flag}" >> Makefile
	echo "CMP=${compiler_name} \$(DEBUG) \$(OPT) " >> Makefile
	echo "GCC=${c_compiler_name} \$(DEBUG) \$(OPT) " >> Makefile
	echo "" >> Makefile
	echo "all: codelite_indexer" >> Makefile

	createObjectList "network/*.cpp libctags/*.c *.cpp" "codelite_indexer_objects"

	for a_file in $files_list
	do
		a_file_with_ext=`basename $a_file`
		a_file_no_ext=`basename $a_file .cxx`
		a_file_no_ext=`basename $a_file_no_ext .cpp`
		a_file_no_ext=`basename $a_file_no_ext .c`
		file_extension=`basename $a_file | cut -d. -f2`

		if [ "$file_extension" = "c" ]; then
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(GCC) -c ${a_file} -DHAVE_CONFIG_H -I. -Ilibctags  -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(GCC) -DHAVE_CONFIG_H -I. -Ilibctags  -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
		else
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(CMP)  -c ${a_file} -DHAVE_CONFIG_H -I. -Ilibctags -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(CMP) -DHAVE_CONFIG_H -I. -Ilibctags  -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
		fi

	done
	echo "-include ${configuration}/*.d" >> Makefile
	echo >> Makefile

	echo "codelite_indexer: make_dirs \$(codelite_indexer_objects)" >> Makefile

	## On FreeBSD, -ldl is part og libc
	if [ "$os_name" = "FreeBSD" ]; then
		echo "	\$(CMP) -o codelite_indexer \$(codelite_indexer_objects) -lpthread " >> Makefile
	else
		echo "	\$(CMP) -o codelite_indexer \$(codelite_indexer_objects) -lpthread -ldl " >> Makefile
	fi

	echo >> Makefile

	echo "make_dirs:" >> Makefile
	echo "	@echo" >> Makefile
	echo "	@echo \"----------Building project:[ codelite_indexer - ${configuration} ]----------\"" >> Makefile
	echo "	@echo" >> Makefile
	echo "	test -d ${configuration} || mkdir ${configuration}" >> Makefile
	echo >> Makefile

	echo "clean: " >> Makefile
	echo "	\$(RM) -fr ${configuration}/*.o" >> Makefile
	echo "	\$(RM) -fr ${configuration}/*.o.d" >> Makefile
	echo "	\$(RM) -fr codelite_indexer" >> Makefile

	## restore the path
	cd ${current_wd}
}

## The rules created here all contain -fPIC. Any other shared-lib-creating functions *must* do this too
createRules_sharedObject ()
{
	files_list=$1
	object_list_name=$2
	rule_name=$3
	libname=$4
	# createRules_sharedObject() creates both plugin and non-plugin libs
	# For plugins, we want a cp -f line at the end to copy them to Runtime/plugins.
	# For non-plugins we don't: they're installed direct from ./lib
	dont_copy=$5
	# Only use any additional link options on the Mac:
	# they're not needed for Linux, and (on Linux at least) they break -j2
	additional_link_libs=""
	if [ "$os_name" = "Darwin" ];then
	  additional_link_libs=$6
	fi

	for a_file in $files_list
	do

		a_file_with_ext=`basename $a_file`
		a_file_no_ext=`basename $a_file .cxx`
		a_file_no_ext=`basename $a_file_no_ext .cpp`
		a_file_no_ext=`basename $a_file_no_ext .c`
		file_extension=`basename $a_file | cut -d. -f2`

		if [ "$file_extension" = "c" ]; then
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(GCC) -c ${a_file} -fPIC \$(CCFLAGS) \$(INCLUDES) -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(GCC) -fPIC \$(CCFLAGS) \$(INCLUDES) -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.i:  >> Makefile
			echo "	\$(GCC) -fPIC \$(CCFLAGS) \$(INCLUDES) -E -o ${configuration}/${a_file_no_ext}.o.i ${a_file}" >> Makefile
		else
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(CMP) -c ${a_file} -fPIC \$(CCFLAGS) \$(INCLUDES) -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(CMP) -fPIC \$(CCFLAGS) \$(INCLUDES) -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.i:  >> Makefile
			echo "	\$(CMP) -fPIC \$(CCFLAGS) \$(INCLUDES) -E -o ${configuration}/${a_file_no_ext}.o.i ${a_file}" >> Makefile
		fi
	done
	echo >> Makefile

	shared_obj_switch=" -shared "
	if [ "$os_name" = "Darwin" ]; then
		shared_obj_switch=" -dynamiclib "
	fi
	
	if [ "$rule_name" = "SDK" ]; then
		echo "${rule_name}: \$(${object_list_name}) CodeLite " >> Makefile
	else 
		echo "${rule_name}: \$(${object_list_name})" >> Makefile
	fi
	
	echo "	\$(CMP) -fPIC ${shared_obj_switch} -o ${base_dir}/\$(OUTPUT_DIR)/${libname}.so \$(${object_list_name}) \$(PLUGIN_LINK_FLAGS) ${additional_link_libs}" >> Makefile
	if [ $dont_copy != "dont_copy" ]; then
		echo "	cp -f ${base_dir}/\$(OUTPUT_DIR)/${libname}.so ${runtime_path}/." >> Makefile
	fi
	echo >> Makefile
	echo "-include ${configuration}/*.o.d" >> Makefile
	echo >> Makefile
}

createRules_exe ()
{
	files_list=$1
	object_list_name=$2
	rule_name=$3
	exename=$4
	for a_file in $files_list
	do

		a_file_with_ext=`basename $a_file`
		a_file_no_ext=`basename $a_file .cxx`
		a_file_no_ext=`basename $a_file_no_ext .cpp`
		a_file_no_ext=`basename $a_file_no_ext .c`
		file_extension=`basename $a_file | cut -d. -f2`

		if [ "$file_extension" = "c" ]; then
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(GCC) -c ${a_file} \$(CCFLAGS) \$(INCLUDES) -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(GCC) \$(CCFLAGS) \$(INCLUDES) -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.i:  >> Makefile
			echo "	\$(GCC) \$(CCFLAGS) \$(INCLUDES) -E -o ${configuration}/${a_file_no_ext}.o.i ${a_file}" >> Makefile
		else
			echo ${configuration}/${a_file_no_ext}.o: ${a_file} ${configuration}/${a_file_no_ext}.o.d >> Makefile
			echo "	\$(CMP) -c ${a_file} \$(CCFLAGS) \$(INCLUDES) -o ${configuration}/${a_file_no_ext}.o" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.d:  >> Makefile
			echo "	\$(CMP) \$(CCFLAGS) \$(INCLUDES) -MT${configuration}/${a_file_no_ext}.o -MF${configuration}/${a_file_no_ext}.o.d  -MM ${a_file}" >> Makefile
			echo ${configuration}/${a_file_no_ext}.o.i:  >> Makefile
			echo "	\$(CMP) \$(CCFLAGS) \$(INCLUDES) -E -o ${configuration}/${a_file_no_ext}.o.i ${a_file}" >> Makefile
		fi
	done
	echo >> Makefile

	echo "${rule_name}: \$(${object_list_name}) SDK" >> Makefile
	echo "	\$(CMP) -o ${exename} \$(${object_list_name}) \$(LINK_FLAGS)" >> Makefile
	echo "	@echo ==========================================================" >> Makefile
	echo "	@echo Done, please run \"sudo \$(MAKE) install\"." >> Makefile
	echo "	@echo ==========================================================" >> Makefile
	echo >> Makefile
}

createPluginMakefile()
{
	plugin_path=$1
	plugin_name=$2
	install_path=$3
	post_install_path=$4

	echo "Generating ${plugin_path}/Makefile..."
	current_wd=`pwd`
	cd ${plugin_path}

	## remove old Makefile
	rm -fr Makefile

	## set the plugin path relative to the main path
	base_dir=".."


	if [ "$plugin_name" = "CppCheck" ] ; then
	  createVariables "yes" "-I../sdk/codelite_cppcheck/"
	else
	  createVariables "yes"
	fi

	createObjectList "*.cpp" "${plugin_name}Objects"
	
	createPluginTargets "${plugin_name}" "${post_install_path}"
	if [ "$plugin_name" = "Subversion2" ] ; then
	  createRules_sharedObject "*.cpp" "${plugin_name}Objects" "${plugin_name}" "${plugin_name}" "copy" "-L../lib -lplugin\$(EXT) -lcodelite\$(EXT) -lwxscintilla\$(EXT)"
	else 
	  createRules_sharedObject "*.cpp" "${plugin_name}Objects" "${plugin_name}" "${plugin_name}" "copy" "-L../lib -lplugin\$(EXT) -lcodelite\$(EXT) -lwxscintilla\$(EXT) -lwxsqlite3\$(EXT)"
	fi

	## restore the path
	cd ${current_wd}

	echo ".PHONY: ${plugin_name} ${plugin_name}_clean ${plugin_name}_install" >> Makefile
	echo "${plugin_name}:" >> Makefile
	echo "	@\$(MAKE) -C ${plugin_path} -f Makefile" >> Makefile
	echo "" >> Makefile

	echo "${plugin_name}_clean:" >> Makefile
	echo "	@\$(MAKE) -C ${plugin_path} -f Makefile clean" >> Makefile
	echo "" >> Makefile

	echo "${plugin_name}_install:" >> Makefile
	echo "	@mkdir -p ${install_path}" >> Makefile
	echo "	cp \$(OUTPUT_DIR)/${plugin_name}.so ${install_path}" >> Makefile
	echo "" >> Makefile
}

createPluginTargets()
{
	plugin_name=$1
	runtime_path=$2

	echo "## build targets" >> Makefile
	echo "all :  pre_build ${plugin_name} post_build" >> Makefile
	echo "" >> Makefile
	echo "pre_build:" >> Makefile
    echo "	@echo" >> Makefile
	echo "	@echo \"----------Building project:[ ${plugin_name} - ${configuration} ]----------\"" >> Makefile
	echo "	@echo" >> Makefile
	echo "	@test -d  \$(OBJ_DIR) || mkdir \$(OBJ_DIR)" >> Makefile
	echo "	@test -d  ${base_dir}/\$(OUTPUT_DIR) || mkdir ${base_dir}/\$(OUTPUT_DIR)" >> Makefile
	echo "" >> Makefile
	echo "clean:" >> Makefile
	echo "	\$(RM) ${base_dir}/\$(OUTPUT_DIR)/${plugin_name}.so" >> Makefile
	echo "	\$(RM) -fr \$(OBJ_DIR)" >> Makefile
	echo "	\$(RM) -fr ${runtime_path}/${plugin_name}.so" >> Makefile
	echo "" >> Makefile

	echo "post_build:" >> Makefile
	# Don't copy to ${runtime_path} here, do it in the target; otherwise it breaks 'make -j2'
	echo "" >> Makefile

}

createInstallTarget()
{
	echo "install: uninstall core_install plugins_install" >> Makefile
	echo "" >> Makefile

	echo "core_install:" >> Makefile
	echo "	@echo installing CodeLite" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/CodeLite" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/share/codelite" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/bin" >> Makefile
	echo "	@cp \$(EXE_NAME) \$(DESTDIR)$prefix/bin/" >> Makefile
	if [ "$make_symlink" = "yes" ] ; then
	  echo "	@ln -s \$(EXE_NAME_NO_PATH) \$(DESTDIR)$prefix/bin/CodeLite" >> Makefile
	  echo "	@chmod +x \$(DESTDIR)$prefix/bin/CodeLite" >> Makefile
	fi
	echo "	@cp Runtime/codelite_fix_files \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@cp Runtime/codelite_exec \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@cp Runtime/codelite_kill_children \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@cp Runtime/codelite_xterm \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@chmod +x \$(DESTDIR)$prefix/bin/codelite_fix_files" >> Makefile
	echo "	@chmod +x \$(DESTDIR)$prefix/bin/codelite_exec" >> Makefile
	echo "	@chmod +x \$(DESTDIR)$prefix/bin/codelite_kill_children" >> Makefile
	echo "	@chmod +x \$(DESTDIR)$prefix/bin/codelite_xterm" >> Makefile
	echo "	@cp sdk/codelite_indexer/codelite_indexer \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@cp sdk/codelite_cppcheck/codelite_cppcheck \$(DESTDIR)$prefix/bin/" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/applications" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/config" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/templates" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$plugins_dir/" >> Makefile
	# In case the non-plugin.so libs aren't to go into the same dir as plugins...
	echo "	@mkdir -p \$(DESTDIR)$non_plugins_dir/" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$plugins_dir/debuggers" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$plugins_dir/resources" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/images" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/rc" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/lexers" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/lexers/Default" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/lexers/BlackTheme" >> Makefile
	echo "	@mkdir -p \$(DESTDIR)$prefix/share/codelite/src/" >> Makefile
	echo "	@cp -fr Runtime/plugins/resources/*.* \$(DESTDIR)$plugins_dir/resources/" >> Makefile
	echo "	@cp -fr Runtime/config/build_settings.xml.default \$(DESTDIR)$prefix/share/codelite/config/" >> Makefile
	echo "	@cp -fr Runtime/config/plugins.xml.default \$(DESTDIR)$prefix/share/codelite/config/" >> Makefile
	echo "	@cp -fr Runtime/config/debuggers.xml.gtk \$(DESTDIR)$prefix/share/codelite/config/debuggers.xml.default" >> Makefile
	echo "	@cp -fr Runtime/config/codelite.xml.default.gtk \$(DESTDIR)$prefix/share/codelite/config/codelite.xml.default" >> Makefile
	echo "	@cp -fr Runtime/codelite.desktop \$(DESTDIR)$prefix/share/applications/codelite.desktop; " >> Makefile
	echo "	@cp -fpr Runtime/rc/*.xrc \$(DESTDIR)$prefix/share/codelite/rc/" >> Makefile
	echo "	@cp -fpr Runtime/templates/* \$(DESTDIR)$prefix/share/codelite/templates/" >> Makefile
	echo "	@chmod -R +w \$(DESTDIR)$prefix/share/codelite/templates/* " >> Makefile
	echo "	@cp -fpr Runtime/lexers/Default/*.xml \$(DESTDIR)$prefix/share/codelite/lexers/Default/" >> Makefile
	echo "	@cp -fpr Runtime/lexers/BlackTheme/*.xml \$(DESTDIR)$prefix/share/codelite/lexers/BlackTheme/" >> Makefile
	echo "	@cp -fpr lib/lib*${ext}.so \$(DESTDIR)$non_plugins_dir/" >> Makefile
	echo "	@chmod 0644 \$(DESTDIR)$non_plugins_dir/*.so" >> Makefile
	echo "	@cp -fpr lib/Debugger.so \$(DESTDIR)$plugins_dir/debuggers/" >> Makefile
	echo "	@chmod 0644 \$(DESTDIR)$plugins_dir/debuggers/*.so" >> Makefile
	echo "	@cp -fpr Runtime/index.html \$(DESTDIR)$prefix/share/codelite/" >> Makefile
	echo "	@cp -fpr Runtime/astyle.sample \$(DESTDIR)$prefix/share/codelite/" >> Makefile
	echo "	@cp -fpr Runtime/svnreport.html \$(DESTDIR)$prefix/share/codelite/" >> Makefile
	echo "	@cp -fpr Runtime/images/*.png \$(DESTDIR)$prefix/share/codelite/images/" >> Makefile
	echo "	@cp -fpr Runtime/config/accelerators.conf.default \$(DESTDIR)$prefix/share/codelite/config/" >> Makefile
	echo "	@cp -fpr Runtime/src/*.gz \$(DESTDIR)$prefix/share/codelite/src/" >> Makefile
	if [ "$add_desktop_icon" = "yes" ] ; then
	  if test -d $HOME/Desktop/ ; then
	    echo "	@cp -fpr Runtime/codelite.desktop $HOME/Desktop/" >> Makefile
		echo "	@chmod +x $HOME/Desktop/codelite.desktop" >> Makefile
	  fi
	fi
	echo "" >> Makefile
}

createUnInstallTarget()
{
	echo "uninstall: " >> Makefile
	echo "	@echo Uninstalling CodeLite" >> Makefile
	if [ "$make_symlink" = "yes" ] ; then
	  echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/CodeLite" >> Makefile
	fi
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_fix_files" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_exec" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_kill_children" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_xterm" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_indexer" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite_cppcheck" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/CodeLiteUR" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/CodeLiteUD" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/bin/codelite" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/include/UnitTest++/" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/lib/libUnitTest++.a" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$prefix/share/codelite" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$non_plugins_dir/" >> Makefile
	echo "	@\$(RM) -fr  \$(DESTDIR)$plugins_dir/" >> Makefile
    # Check write permission for /usr/share/applications, in case we're using a 'local' prefix and not doing sudo
	echo "	@if test -w \$(DESTDIR)$prefix/share/applications ; then \\" >> Makefile
	    echo "	  \$(RM) -fr \$(DESTDIR)$prefix/share/applications/codelite.desktop; \\" >> Makefile
	echo "	fi" >> Makefile
	echo "	@\$(RM) -fr ~/Desktop/codelite.desktop" >> Makefile
	echo "" >> Makefile
}

Show_Help_and_Exit()
{
	echo "This is the CodeLite configure script"
	echo "Usage: ./configure [OPTION]..."
	echo "Available options:"
	echo "  --prefix=PREFIX         install files into the filepath PREFIX (the default is /usr)"
	echo "  --plugins-dir=PREFIX    install plugins into the filepath PREFIX (the default is /usr/lib/codelite)"
	echo "  --enable-debug          create a debug build (the default is disable)"
	echo "  --help                  show this help"
	exit
}

## main
# checkCURL

## Create variables needed by the makefile
enable_debug="no"
prefix="/usr"
plugins_dir="/usr/lib/codelite"
add_desktop_icon="yes"
make_symlink="no"
os_name=`uname -s`
arch=`uname -m`

## Which wx version is being used for this build?
wxversionnumber=`wx-config --version 2>/dev/null`
if [ -z "$wxversionnumber" ]; then
	# No wx-config, so why continue?
	echo "No functional wx-config script was found in your PATH"
	echo "Is the wxWidgets development package installed?"
	exit
fi
 
wxMAJOR_VERSION=`echo $wxversionnumber | awk -F. '{print $1}'`
wxMINOR_VERSION=`echo $wxversionnumber | awk -F. '{print $2}'`
# Should we ever need it: wxRELEASE_NUMBER=`echo $wxversionnumber | awk -F. '{print $3}'`

## wx versions 2.9 and greater need an extra 'std,aui' in wx-config --libs
 # NB this logic will fail for wx versions < 2 ;)
wxconfiglibs="wx-config --libs std,aui"
if [ "$wxMAJOR_VERSION" -eq 2 ] && [ "$wxMINOR_VERSION" -lt 9 ]; then
	wxconfiglibs="wx-config --libs"
fi

## Replace place holders in the desktop file
rm -fr Runtime/codelite.desktop
sed s@%%PREFIX%%@$prefix@g Runtime/codelite.desktop.template >> Runtime/codelite.desktop

## Determine if we are on 64 bit machine
## AMD / Intel will produce x86_64
## while itanium produces ia64
is_64bit="no"
if [ "$arch" = "x86_64" ] || [ "$arch" = "ia64" ]; then
	is_64bit="yes"
fi

## Process any arguments that were supplied
until [ -z "$1" ]  # Until all parameters used up
do
	if [ "$1" = "--enable-debug" ]; then
		enable_debug="yes"

	   # The next 2 aren't in the Usage, but will prevent undesirable things from happening when e.g. making rpms
		elif [ "$1" = "--disable-desktop_icon" ]; then
			add_desktop_icon="no"

		elif [ "$1" = "--disable-make_symlink" ]; then
			make_symlink="no"

		elif [ "$1" = "--help" ]; then
			Show_Help_and_Exit

		else
			key=`echo $1 | cut -d= -f1`
			value=`echo $1 | cut -d= -f2`

			if [ "$key" = "--prefix" ]; then
				prefix=$value
			elif [ "$key" = "--plugins-dir" ]; then
				plugins_dir=$value
			fi
		fi
	shift
done

## To where shall we install the .so libs which used to be static e.g. libwxscintillau.so
# I'm using plugins_dir for simplicity, but creating its own name makes this easy to change
non_plugins_dir=$plugins_dir

## remove old makefile
makefile_name="Makefile"
base_dir="."
rm -fr ./Makefile

## Create revision file
createRevisionFile
echo "Generating Makefile..."
createVariables "no"

## Create the various object lists
createObjectList "sdk/wxscintilla/src/scintilla/src/*.cxx sdk/wxscintilla/src/*.cpp" "ScintillaObjects"
createObjectList "sdk/codelite_indexer/network/*.cpp CodeLite/*.cpp" "CodeLiteObjects"
createObjectList "LiteEditor/*.cpp" "LiteEditorObjects"
createObjectList "sdk/wxsqlite3/src/*.cpp sqlite3/*.c" "wxSqlite3Objects"
createObjectList "Plugin/*.cpp Plugin/*.c" "SDKObjects"

createMainTargets
echo >> Makefile

## Create the rules
# First the non-plugin .so libs:
createRules_sharedObject  "Plugin/*.cpp Plugin/*.c" "SDKObjects" "SDK" "libplugin\$(EXT)" "dont_copy" "-L./lib -lwxscintilla\$(EXT) -lcodelite\$(EXT)"
createRules_sharedObject  "sdk/wxscintilla/src/scintilla/src/*.cxx sdk/wxscintilla/src/*.cpp" "ScintillaObjects" "Scintilla" "libwxscintilla\$(EXT)" "dont_copy"
createRules_sharedObject  "sdk/codelite_indexer/network/*.cpp CodeLite/*.cpp" "CodeLiteObjects" "CodeLite" "libcodelite\$(EXT)" "dont_copy" "-L./lib -lwxsqlite3\$(EXT)"
createRules_sharedObject  "sdk/wxsqlite3/src/*.cpp sqlite3/*.c" "wxSqlite3Objects" "SQLite" "libwxsqlite3\$(EXT)" "dont_copy" ""

# Then the binary, and indexer
createRules_exe "LiteEditor/*.cpp" "LiteEditorObjects" "LiteEditor" ${exe_name}
createCodeLiteIndexerMakefile "sdk/codelite_indexer/*.cpp sdk/codelite_indexer/network/*.cpp sdk/codelite_indexer/libctags/*.c sdk/codelite_indexer/libctags/*.cpp"
createCodeLiteCppcheckMakefile "sdk/codelite_cppcheck/cli/*.cpp sdk/codelite_cppcheck/lib/*.cpp"
createInstallTarget
createUnInstallTarget

echo "-include \$(OBJ_DIR)/*.d" >> Makefile
echo "" >> Makefile

echo "Plugins: SDK \\" >> Makefile

########################################################
##
## Add any plugin targets you need here
## Append these as dependencie for the main project
##
########################################################
echo "Debugger\\" >> Makefile
echo "Gizmos\\" >> Makefile
echo "cscope\\" >> Makefile
echo "Subversion2\\" >> Makefile
echo "CodeFormatter\\" >> Makefile
echo "Copyright\\" >> Makefile
echo "UnitTestCPP\\" >> Makefile
echo "ExternalTools\\" >> Makefile
echo "SnipWiz\\" >> Makefile
echo "wxformbuilder\\" >> Makefile
echo "abbreviation\\" >> Makefile
echo "SymbolView\\" >> Makefile
echo "ContinuousBuild\\" >> Makefile
echo "QmakePlugin\\" >> Makefile
echo "CppCheck" >> Makefile
echo "" >> Makefile

echo "plugins_install:\\" >> Makefile
########################################################
##
## For all plugins you add, you can add an entry here for
## the install target
########################################################
echo "Debugger_install\\" >> Makefile
echo "Gizmos_install\\" >> Makefile
echo "cscope_install\\" >> Makefile
echo "Subversion2_install\\" >> Makefile
echo "CodeFormatter_install\\" >> Makefile
echo "Copyright_install\\" >> Makefile
echo "UnitTestCPP_install\\" >> Makefile
echo "ExternalTools_install\\" >> Makefile
echo "SnipWiz_install\\" >> Makefile
echo "wxformbuilder_install\\" >> Makefile
echo "abbreviation_install\\" >> Makefile
echo "SymbolView_install\\" >> Makefile
echo "ContinuousBuild_install\\" >> Makefile
echo "QmakePlugin_install\\" >> Makefile
echo "CppCheck_install" >> Makefile
echo "	@chmod 0644 \$(DESTDIR)$plugins_dir/*.so" >> Makefile
echo "" >> Makefile

echo "clean_plugins:\\" >> Makefile
########################################################
##
## For all plugins you add, add an entry here for 'clean'
##
########################################################
echo "Debugger_clean\\" >> Makefile
echo "Gizmos_clean\\" >> Makefile
echo "cscope_clean\\" >> Makefile
echo "Subversion2_clean\\" >> Makefile
echo "CodeFormatter_clean\\" >> Makefile
echo "Copyright_clean\\" >> Makefile
echo "UnitTestCPP_clean\\" >> Makefile
echo "ExternalTools_clean\\" >> Makefile
echo "SnipWiz_clean\\" >> Makefile
echo "wxformbuilder_clean\\" >> Makefile
echo "abbreviation_clean\\" >> Makefile
echo "SymbolView_clean\\" >> Makefile
echo "ContinuousBuild_clean\\" >> Makefile
echo "QmakePlugin_clean\\" >> Makefile
echo "CppCheck_clean" >> Makefile
echo "" >> Makefile

########################################################
##
## add plugins here
## createPluginMakefile <plugin path> <plugin name>
##
########################################################

createPluginMakefile "Debugger" "Debugger" "\$(DESTDIR)$plugins_dir/debuggers/" "../Runtime/debuggers"
createPluginMakefile "Gizmos" "Gizmos"  "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "cscope" "cscope" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "Subversion2" "Subversion2" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "CodeFormatter" "CodeFormatter" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "Copyright" "Copyright" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "UnitTestCPP" "UnitTestCPP" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "ExternalTools" "ExternalTools" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "SnipWiz" "SnipWiz" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "wxformbuilder" "wxformbuilder" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "abbreviation" "abbreviation" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "SymbolView" "SymbolView" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "ContinuousBuild" "ContinuousBuild" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "QmakePlugin" "QmakePlugin" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
createPluginMakefile "cppchecker" "CppCheck" "\$(DESTDIR)$plugins_dir/" "../Runtime/plugins"
