# Makefile for JX distribution

JX_ROOT  := ..
MAKE_DIR := ${shell pwd}

DISTR_PROGS := ../programs/makemake \
               ../programs/jxlayout

include ${JX_ROOT}/include/make/jx_constants
include ${JX_ROOT}/include/make/jx_config

#
# build all Makefiles
#

PHONY : Makefiles
Makefiles:
	@${foreach dir, \
          ${wildcard ../lib?*/. ./util/*/. ../programs/*/.} \
          ../ACE/test/ ../tutorial/, \
       cd ${dir}; makemake; ${MAKE} Makefiles; cd ${MAKE_DIR};}

#
# install binaries
#

.PHONY : install
install:
	@cd ../ACE; ${MAKE} jxinstall
	@cd ../programs/makemake; ${MAKE} -f Makefile.port jxinstall
	@${MAKE} Makefiles
	@${foreach dir, \
          ${wildcard ../lib?*/.}, \
       cd ${dir}; ${MAKE}; cd ${MAKE_DIR};}
	@${foreach dir, \
          ${wildcard ./util/*/. ../programs/*/.}, \
       cd ${dir}; ${MAKE} jxinstall; cd ${MAKE_DIR};}

#
# source distribution
#

SRC_TAR_FILE := JX_source-${JX_VERSION}.tar

.PHONY : jxsource
jxsource:
	@cp README.source ${JX_ROOT}/README
	@cd ${J_DISTR_TAR_DIR}; \
     tar -cf ${SRC_TAR_FILE} ${JX}/README ${JX}/FAQ ${JX}/LICENSE ${JX}/CHANGES \
                             ${JX}/Makefile
	@${RM} ${JX_ROOT}/README

	@${foreach dir, \
          ../libjcore/ ../libjx/ ../ACE/ ../tutorial/ ../misc/ ${DISTR_PROGS} \
          ${wildcard ./util/*/.}, \
       cd ${dir}; \
       ${MAKE} DISTR_TAR_FILE=${SRC_TAR_FILE} jxsource; \
       cd ${MAKE_DIR}; }

	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${SRC_TAR_FILE} ${filter-out %~, \
                               ${shell cd ${J_DISTR_TAR_DIR}; echo \
             ${JX}/doc \
             ${JX}/include/missing_proto/* \
             ${JX}/include/make/jx_constants \
             ${JX}/include/make/jx_config_common \
             ${JX}/include/make/jx_targets \
             ${JX}/include/make/default_lib_target \
             ${JX}/include/make/sys/* \
             ${addprefix ${JX}/lib/, \
               Makefile README.* rpm/*} }}; \
     gzip ${SRC_TAR_FILE}

.PHONY : allsource
allsource: jxsource
	@${foreach dir, \
          ${filter-out ../libjcore/. ../libjx/. \
                       ../programs/makemake/. ../programs/jxlayout/., \
            ${wildcard ../lib?*/. ../programs/*/.}}, \
       cd ${dir}; ${MAKE} source; cd ${MAKE_DIR};}

#
# shared
#

SHARED_TAR_FILE := JX_shared-${JX_VERSION}.tar

.PHONY : jxshared
jxshared:
  ifdef RH_ISV
	@echo '***** RedHat ISV version *****'
	@cp README.ISV ${JX_ROOT}/README
  else
	@cp README.shared ${JX_ROOT}/README
  endif
	@cd ${J_DISTR_TAR_DIR}; \
     tar -cf ${SHARED_TAR_FILE} ${JX}/README ${JX}/FAQ ${JX}/LICENSE ${JX}/CHANGES
	@${RM} ${JX_ROOT}/README

	@${foreach dir, \
          ../libjcore/ ../libjx/ ../ACE/ ../tutorial/ ${DISTR_PROGS}, \
       cd ${dir}; \
       ${MAKE} DISTR_TAR_FILE=${SHARED_TAR_FILE} jxshared; \
       cd ${MAKE_DIR}; }

	@cd ${J_DISTR_TAR_DIR}; \
     tar -rhf ${SHARED_TAR_FILE} \
              ${JX}/doc \
              ${addprefix ${JX}/include/make/, \
                          jx_constants jx_config jx_config_common \
                          jx_targets default_lib_target}

#
# RPM
#

RPM_SRC_DIR  := /usr/src/redhat/SOURCES
RPM_SPEC_DIR := /usr/src/redhat/SPECS
RPM_BIN_DIR  := /usr/src/redhat/RPMS/i386
RPM_SRPM_DIR := /usr/src/redhat/SRPMS

RPM_TAR_FILE  := ${RPM_SRC_DIR}/JX-${JX_VERSION}.tar
RPM_SPEC_FILE := JX-${JX_VERSION}-1.spec

.PHONY : jxrpm
jxrpm:
	@chmod a+x rpm/cleanjx; ./rpm/cleanjx

	@cp ${J_DISTR_TAR_DIR}/${SHARED_TAR_FILE} ${RPM_TAR_FILE}
  ifdef RH_ISV
	@echo '***** RedHat ISV version *****'
  else
	@cd ${RPM_SRC_DIR}; tar -xf ${RPM_TAR_FILE}; ${RM} ${RPM_TAR_FILE}
	@cp README.rpm ${RPM_SRC_DIR}/${JX}/README
	@cd ${RPM_SRC_DIR}; tar -cf ${RPM_TAR_FILE} ${JX}; ${RM} -r ${JX}
  endif

	@cp rpm/${RPM_SPEC_FILE} ${RPM_SPEC_DIR}
	@cd ${RPM_SPEC_DIR}; ${RPM} ${RPM_SPEC_FILE}

	@mv ${RPM_BIN_DIR}/JX-${JX_VERSION}-1.i386.rpm ${J_DISTR_TAR_DIR}/; \
     chown ${USER}. ${J_DISTR_TAR_DIR}/*.rpm; \
     ${RM} ${RPM_SRPM_DIR}/JX-${JX_VERSION}-1.src.rpm
	@./rpm/cleanjx

#
# clean up
#

.PHONY : tidy
tidy:
	@${foreach dir, \
          ${wildcard ../lib?*/. ../programs/*/. ./util/*/.} \
          ../ACE/ ../misc/ ../tutorial/, \
       cd ${dir}; ${MAKE} tidy; cd ${MAKE_DIR};}

.PHONY : clean
clean:
	@${foreach dir, \
          ${wildcard ../lib?*/. ../programs/*/. ./util/*/.} \
          ../ACE/ ../misc/ ../tutorial/, \
       cd ${dir}; ${MAKE} clean; cd ${MAKE_DIR};}

.PHONY : uninstall
uninstall:
	@${foreach dir, \
          ${wildcard ../lib?*/. ../programs/*/. ./util/*/.} \
          ../ACE/, \
       cd ${dir}; ${MAKE} jxuninstall; cd ${MAKE_DIR};}
