# VK project Makefile
# (c) Christoph Pinkel 1999

# This is the top level Makefile of your VK project.
# It translates your VK code to c++ source code and generates all files
# you need for your KDE project.
# Use make all to do all these jobs at one time.
# If you just want to rebuilt the c++ code, run "make VK_files", if you
# want to build the KDE project from the allready-built c++ source, then
# use "make KDE". To clean both the meta-output AND the KDE project use
# "make clean".
# "make clean" does NOT prepare your program for a release archive. If
# you want to release your project, just run "make distclean" and "automake"
# in "./..".

# Christoph

all: VK_files KDE

VK_files:
	echo "Translating VK files."; \
	td=$$(pwd); \
	for subdir in $$(ls VK); do \
	cd $$td; \
	if ( test -d ./VK/$$subdir ); then \
		echo "Translating in $$subdir..."; \
		if !(test -f ./VK/$$subdir); then \
			cp makefile.sub ./VK/$$subdir/Makefile; \
			cp *-template ./VK/$$subdir; \
			cp run.sub ./VK/$$subdir/run; \
			chmod a+x ./VK/$$subdir/run; \
		fi; \
		cd ./VK/$$subdir && make VK -s \
		|| ( echo "Error in ./VK/$$subdir."; exit 1 ) || exit 1; \
	fi; \
	done; \
	echo "All *.cpp/*.h files have been rebuilt."

KDE: data prepare exec

data:
	echo -e "Copying data files\t\t\t\t[ \033[33mStarted\033[0m ]"
	cd pics; \
	for pic in $$(ls *.xpm); do \
		cd ..; \
		if ( test ./pics/$$pic -nt ../program/pics/$$pic ); then \
			vkProjectCreator data ./pics/$$pic; \
			rm -f .project.conf.copy; \
		fi; \
		if !( test -f ../program/pics/$$pic ); then \
			vkProjectCreator data ./pics/$$pic; \
			rm -f .project.conf.copy; \
		fi; \
		cd pics; \
	done

prepare:
	if !( test "$$(cat project.conf)" = "$$(cat .project.conf.copy)" ); then \
		rm -f ../Makefile; \
	fi;
	bash -c "if !( test -f ../Makefile ); then make update; fi"
	echo "KDE project is up to date."

update:
	echo -e "UPDATING THE KDE PROJECT\t\t\t\t[ \033[33mStarted\033[0m ]"; \
	cp project.conf .project.conf.copy; \
	date +"%b %d %Y" > .today; \
	vkProjectCreator base project.conf \
	|| ( echo -e "Error updating the KDE project\t\t\t\t[  \033[31mError\033[0m  ]"; \
	exit 1 ) || exit 1; \
	cd ..; \
	echo "trying to run 'autoconf' in '..'"; \
	autoconf || ( echo -e "\033[31m\"autoconf\" failed. Have you installed it ?\033[0m"; \
	exit 1 ) || exit 1; \
	echo "trying to run 'automake' in '..'"; \
	automake || ( echo -e "\033[31m\"automake\" failed. Have you installed it ?\033[0m"; \
	exit 1 ) || exit 1; \
	echo "trying to run '../configure'"; \
	./configure --enable-debug || \
	( echo -e "\033[31m./configure failed. Try to find the reason in the output above!\033[0m"; \
	exit 1 ) || exit 1; \
	echo -e "\033[32mFinished\033[0m. Run 'make exec' to start your program!\t[   \033[32mOK\033[0m   ]"

exec:
	echo -e "Starting c++ compilation\t\t\t\t[ \033[33mStarted\033[0m ]"
	cd ..; \
	chmod a-x ./program/* ; \
	chmod a+x ./program/pics ; \
	echo "trying to compile (make all) in '..'; now pray!"; \
	make all || ( \
	echo -e "\033[31mEverything's so sad and awful and I feel so sorry )-:"; \
	echo -e "(I.e. you've found a bug in VisKProg)"; \
	echo -e "If you're familiar with c++, you may try to examine the error"; \
	echo -e "output and repare the Makefile or c++-files. If not, you may"; \
	echo -e "destroy our computer or e-mail me a bomb at <de_cp@linuxstart.com>."; \
	echo -e "A bug report should contain the following information:"; \
	echo -e "*Your e-mail address* *Error description* *How to reproduce the error*"; \
	echo -e "\n\033[0m"; \
	exit 1 ) || exit 1; \
	cd program; \
	for tstfile in $$(ls *); do \
		if (test -x $$tstfile); then \
			echo "Running $$tstfile."; \
			./$$tstfile  || (echo -e "\nPROGRAM EXITED ABNORMALLY!\t\t\t[  \033[31mError\033[0m  ]"; exit 1) \
			|| exit 1; \
			exit 0; \
		fi; \
	done


clean:
	if (test -f ../Makefile); then \
		cd ..; \
		make distclean; \
	fi; \
	rm -f ../Makefile; \
	rm -f .project.conf.copy; \
	rm -f .today; \
	td=$$(pwd); \
	for subdir in $$(ls VK); do \
	if ( test -d ./VK/$$subdir ); then \
		echo "Cleaning $$subdir..."; \
		cd ./VK/$$subdir && make clean -s; \
		rm Makefile; \
		rm *-template; \
		cd $$td; \
	fi; \
	done; \
	cd ..; \
	rm *.lsm; \
	cd program; \
	rm *.kdelnk; \
	rm *.xpm; \
	echo "Cleaned."
