# --                -- #
#                      #
# main Screws Makefile #
#                      #
# --                -- #

include CONFIG

BUILDTREE=src/Server src/Hsml src/Utils src/Modules

all:	configure build
	@echo Build is done

configure: 
	@if [ ! -e ".config" ]; then \
	if [ "${PREFIX}" = "" ]; then \
		echo "Please edit the CONFIG file or setenv the PREFIX."; \
		exit 1; \
	fi ;\
	echo "<ConfigOptions>" ;\
	cat CONFIG | grep -v \# | grep -v VERSION ;\
	echo "</ConfigOptions>" ;\
	echo "<!-- sleeping 1 second... -->" ;\
	sleep 1 ;\
	echo "CONFIG DONE" > .config ; \
	fi

build:
	@if [ ! -e ".build" ]; then \
	for A in ${BUILDTREE}; do \
		cd $$A && \
		${MAKE} all && \
		cd ../.. ; \
	done ; \
	echo "BUILD DONE" > .build ; \
	fi

clean:
	@echo ${CFGFILE}
	@echo Cleaning...
	@rm -f .build .config
	@find . -name core | xargs rm -f
	@for A in ${BUILDTREE}; do \
		cd $$A && \
		${MAKE} clean && \
		cd ../.. ; \
	done
	
install: all
	@echo "==> Creating install directories"
	install -d -m 0755 ${PREFIX}
	install -d -m 0755 ${PREFIX}/sbin ${PREFIX}/bin
	install -d -m 0755 ${PREFIX}/share/doc/screws/examples
	install -d -m 0755 ${PREFIX}/share/examples/screws
	install -d -m 0755 ${MODULE_PATH}
	#install -d -m 0755 ${PREFIX}/libexec/screws/modules
	install -d -m 0755 ${PREFIX}/man/man1 ${PREFIX}/man/man5 \
		${PREFIX}/man/man8 ${PREFIX}/etc ${PREFIX}/var/log
	@echo "==> Installing binaries"
	install -c -m 0755 src/Utils/screwsctl ${PREFIX}/sbin/
	install -c -m 0755 src/Server/screwsd  ${PREFIX}/sbin/
	install -c -m 0755 src/Hsml/hsml       ${PREFIX}/bin/
	@echo "==> Installing standard includes for HSML"
	mkdir -p ${DIP}
	cp -rf ./src/Hsml/hsml-inc/* ${DIP}/
	cd src/Exec && $(MAKE) install
	cd src/Hsml && $(MAKE) modules_install
	echo "env HSML_LANG_MOD ${MODULE_PATH}"
	#$(MAKE) -C src/Exec/ install
	@echo "==> Installing modules"
	@for A in src/Modules/*.so ; do \
	   echo ... $$A ; \
	   install -m 0644 $$A ${MODULE_PATH}/ ; \
	done
	@echo "==> Examples Documentation and Example files"
	@for A in doc/server/* ; do \
	echo ... $$A ; \
	install -c -m 0644 $$A ${PREFIX}/share/doc/screws/ ; \
	done
	install -c -m 0644 doc/images/screws.png ${PREFIX}/share/doc/screws/
	install -c -m 0644 doc/manpage/screwsd.8 ${PREFIX}/man/man8/screwsd.8
	install -c -m 0644 doc/manpage/screwsctl.1 ${PREFIX}/man/man1/screwsctl.1
	install -c -m 0644 doc/manpage/screws.conf.5 ${PREFIX}/man/man5/screws.conf.5
	install -c -m 0644 conf/*.conf ${PREFIX}/share/examples/screws/
	install -c -m 0644 src/Utils/mime.types ${PREFIX}/share/screws/
	if [ ! -e "${CFGFILE}" ] ; then \
	install -c -m 0644 conf/default.conf ${CFGFILE} ; \
	fi
	@echo ; \
	echo "***************************************" \
	     "***************************************"; \
	echo; \
	echo "Example configuration file: " \
	     "${PREFIX}/share/examples/screws/screws.conf"; \
	echo; \
	echo "***************************************" \
	     "***************************************"
	@echo
	@echo "Use these paths:"
	@echo "** Modules : ${MODULE_PATH}"
	@echo "** Config  : ${CFGFILE}"
	@echo "** Scripts : ${PREFIX}/libexec/"

deinstall:
	rm -rf ${PREFIX}/sbin/screwsd \
	       ${PREFIX}/sbin/screwsctl \
	       ${PREFIX}/bin/hsml \
	       ${PREFIX}/share/doc/screws/ \
	       ${PREFIX}/share/screws/ \
	       ${PREFIX}/man/man8/screwsd.8 \
	       ${PREFIX}/libexec/screws/
	@echo "** Deinstall DONE **"

dist:	clean
	@echo Disting...
	cd .. && \
	mv screws screws-${VERSION} ;\
	tar czf screws-${VERSION}.tar.gz screws-${VERSION} ;\
	mv screws-${VERSION} screws
	@echo DONE

