##
# MegaZeux Utils Makefile fragment
##

#
# FIXME: This whole file is a giant wart on the new build system
#        and needs some love (and refactoring) to fix it
#

.PHONY: utils utils.debug utils_clean

utils_src = src/utils
utils_obj = src/utils/.build

utils_cflags := ${LIBPNG_CFLAGS} -Icontrib/uthash/

checkres := ${utils_src}/checkres${BINEXT}
checkres_objs := ${utils_obj}/checkres.o ${core_obj}/fsafeopen.o \
                 ${core_obj}/util.o ${core_obj}/zip.o
checkres_ldflags := ${ZLIB_LDFLAGS}

downver := ${utils_src}/downver${BINEXT}
downver_objs := ${utils_obj}/downver.o ${core_obj}/util.o ${core_obj}/zip.o
downver_ldflags := ${ZLIB_LDFLAGS}

hlp2txt := ${utils_src}/hlp2txt${BINEXT}
hlp2txt_objs := ${utils_obj}/hlp2txt.o

txt2hlp := ${utils_src}/txt2hlp${BINEXT}
txt2hlp_objs := ${utils_obj}/txt2hlp.o

png2smzx := ${utils_src}/png2smzx${BINEXT}
png2smzx_objs := ${utils_obj}/png2smzx.o ${utils_obj}/smzxconv.o
png2smzx_objs += ${core_obj}/util.o ${core_obj}/pngops.o
png2smzx_ldflags := ${LIBPNG_LDFLAGS}

ccv := ${utils_src}/ccv${BINEXT}
ccv_objs := ${utils_obj}/ccv.o

${utils_obj}/%.o: ${utils_src}/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${CFLAGS} ${utils_cflags} -I${core_src} \
	  ${ZLIB_CFLAGS} -c $< -o $@

-include $(checkres_objs:.o=.d)
-include $(downver_objs:.o=.d)
-include $(hlp2txt_objs:.o=.d)
-include $(txt2hlp_objs:.o=.d)
-include $(png2smzx_objs:.o=.d)

${checkres}: ${checkres_objs}
	$(if ${V},,@echo "  LINK    " ${checkres})
	${CC} ${checkres_objs} -o ${checkres} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS} ${checkres_ldflags}

${downver}: ${downver_objs}
	$(if ${V},,@echo "  LINK    " ${downver})
	${CC} ${downver_objs} -o ${downver} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS} ${downver_ldflags}

${hlp2txt}: ${hlp2txt_objs}
	$(if ${V},,@echo "  LINK    " ${hlp2txt})
	${CC} ${hlp2txt_objs} -o ${hlp2txt} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS}

${txt2hlp}: ${txt2hlp_objs}
	$(if ${V},,@echo "  LINK    " ${txt2hlp})
	${CC} ${txt2hlp_objs} -o ${txt2hlp} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS}

${png2smzx}: ${png2smzx_objs}
	$(if ${V},,@echo "  LINK    " ${png2smzx})
	${CC} ${png2smzx_objs} -o ${png2smzx} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS} ${png2smzx_ldflags}

${ccv}: ${ccv_objs}
	$(if ${V},,@echo "  LINK    " ${ccv})
	${CC} ${ccv_objs} -o ${ccv} \
	  ${ARCH_EXE_LDFLAGS} ${LDFLAGS}

utils: $(filter-out $(wildcard ${utils_obj}), ${utils_obj})

utils: ${checkres} ${downver} ${hlp2txt} ${txt2hlp} ${ccv}

ifeq (${LIBPNG},1)
utils: ${png2smzx}
utils.debug: ${png2smzx}.debug
endif

utils.debug: ${checkres}.debug ${downver}.debug
utils.debug: ${hlp2txt}.debug ${txt2hlp}.debug

utils_clean:
	$(if ${V},,@echo "  RM      " ${utils_obj})
	${RM} -r ${utils_obj}
	$(if ${V},,@echo "  RM      " ${checkres} ${checkres}.debug)
	${RM} ${checkres} ${checkres}.debug
	$(if ${V},,@echo "  RM      " ${downver} ${downver}.debug)
	${RM} ${downver} ${downver}.debug
	$(if ${V},,@echo "  RM      " ${hlp2txt} ${hlp2txt}.debug)
	${RM} ${hlp2txt} ${hlp2txt}.debug
	$(if ${V},,@echo "  RM      " ${txt2hlp} ${txt2hlp}.debug)
	${RM} ${txt2hlp} ${txt2hlp}.debug
	$(if ${V},,@echo "  RM      " ${png2smzx} ${png2smzx}.debug)
	${RM} ${png2smzx} ${png2smzx}.debug
	$(if ${V},,@echo "  RM      " ${ccv} ${ccv}.debug)
	${RM} ${ccv} ${ccv}.debug
