# VK project Makefile
# (c) Christoph Pinkel 1999

# This Makefile is designed to be used in one single VK project sub directory;
# You should use one directory for each window and sub window, these directories
# will be autocreated by kwinedit.
# This file should automatically find all *.vk, *.vkm files and the *.vk(t)f
# file and produce the cpp-output (xyz.cpp and xyz.h, where xyz is your classname).
#
# You have not even to copy this file in each sub directory manually, as the
# Makefile in the directory above will do it.
#
# IMPRTANT: You may use this file directly, if you just want the VK sources
#	in THIS directory to be rebuilt, or if you want to clean temporary
#	files in this directory ("make clean"). If you want to _compile_ your
#	project to an executable file, or if you want to rebuild everything,
#	then use "make all", "make VK_files" (rebuild c++ source) or
#	"make KDE" (compile/run) of the Makefile in the directory ../..
#
#	Good luck :-)
#	Christoph

all: VK

VK: find makeconfig makevks makevkms makevktf copycpp
	td=$$(pwd); \
	echo -e "\033[32mFinished\033[0m in $$td\t\t[   \033[32mOK\033[0m   ]"; \
	echo "You may wish to use 'make KDE' now to test your program."


find:
	td=$$(pwd); \
	echo -e "Starting translation in $$td\t[ \033[33mStarted\033[0m ]"; \
	echo "Looking for *.vk, *.vkc and *.vk(t)f files in this directory ..."
	ls *.vk > .vk.makelist || echo "Warning: No code files found!"
	ls *.vk*f > .vktf.makelist || echo "Outch: No *.vk(t)f file!"
	echo $$(sed -e s/.vk/.vkm/ .vk.makelist) > .vkm.makelist
	ChV=$$(sed -n -e 1p .vktf.makelist); \
	if test "$$ChV" = ""; then \
		echo -e "No *.vktf or *.vkf file!\t\t\t\t[  \033[31mError\033[0m  ]"; \
		exit 1; \
	fi
	ChV=$$(sed -n -e 2p .vktf.makelist); \
	if test "$$ChV" != ""; then \
		echo -e "Several *.vktf or *.vkf files!\t\t\t\t[  \033[31mError\033[0m  ]"; \
		exit 2; \
	fi
	if test -f h-template; \
	then \
		echo "Found h-template."; \
	else \
		echo -e "Missing file h-template in actual directory.\t\t[  \033[31mError\033[0m  ]"; \
		exit 3; \
	fi
	if test -f cpp-template; \
	then \
		echo "Found cpp-template."; \
	else \
		echo -e "Missing file cpp-template in actual directory.\t\t[  \033[31mError\033[0m  ]"; \
		exit 4; \
	fi
	ls *.vk*f > .vkc.makelist
	echo $$(sed -e s/.vkf/.vkc/ .vkc.makelist) > .vkc.makelist; \
	echo $$(sed -e s/.vktf/.vkc/ .vkc.makelist) > .vkc.makelist; exit 0
	echo -e "Found all project files.\t\t\t\t[   \033[32mOK\033[0m   ]"

makeconfig:
	vkc=$$(cat .vkc.makelist); \
	vktf=$$(cat .vktf.makelist); \
	vkConfigmaker ../../project.conf $$vktf $$vkc || ( \
	echo -e "Couldn't generate window configuration.\t\t\t[  \033[31mError\033[0m  ]"; \exit 1 ) \
	|| exit 1; \
	echo -e "Configuration file updated\t\t\t[   \033[32mOK\033[0m   ]"; \

makevks:
	fnlist=$$(cat .vk.makelist); \
	vkc=$$(cat .vkc.makelist); \
	for fn in $$fnlist; do \
		echo $$fn > .tmp.makelist; \
		dest=$$(sed -e s/.vk/.vkm/ .tmp.makelist); \
		echo -e "Translating file $$fn ..."; \
		vkPreTranslator $$fn $$vkc $$dest || ( \
		echo -e "Error translating file $$fn.\t\t\t[  \033[31mError\033[0m  ]"; \exit 1 ) \
		|| exit 1; \
		echo -e "File $$fn translated to *.vkm\t\t[   \033[32mOK\033[0m   ]"; \
	done
	

makevkms:
	fnlist=$$(cat .vkm.makelist); \
	vkc=$$(cat .vkc.makelist); \
	for fn in $$fnlist; do \
		echo "Translating $$fn ..."; \
		echo "$$fn" > .tmp.makelist; \
		vkModuleTranslator $$fn \
			$$(sed -e s/.vkm/.cpp-code/ .tmp.makelist ) $$vkc || exit 1; \
		echo -e "File $$fn translated\t\t\t\t[   \033[32mOK\033[0m   ]"; \
	done

makevktf:
	fn=$$(cat .vktf.makelist); \
	vkc=$$(cat .vkc.makelist); \
	vkFormTranslator -c $$fn cpp-template h-template $$vkc || exit 1;\
	echo "Window file translated."

copycpp:
	for cpps in $$(ls *.cpp); do \
	echo "Copy file $$cpps ..."; \
	if !( test -f ../../../program/$$cpps ); then \
		rm ../../../Makefile; \
		tdir=$$(pwd); \
		cd ../..; \
		echo "(File was not installed before)"; \
		vkProjectCreator data $$tdir/$$cpps; \
		cd $$tdir; \
	else \
	cp -f $$cpps ../../../program; \
	fi; \
	done; \
	for hs in $$(ls *.h); do \
	echo "Copy file $$hs ..."; \
	if !( test -f ../../../program/$$hs ); then \
		rm ../../../Makefile; \
	fi; \
	cp -f $$hs ../../../program; \
	done; \

clean:
	rm -f .*.makelist
	rm -f *.vkm
	rm -f *.vkc
	rm -f *.cpp-code
	rm -f *.cpp
	rm -f *.h
