# Makefile to compile JX source distribution

J_LIB_DIR := ${shell pwd}/lib

J_LIB_DIR_ON_PATH := $(findstring ${J_LIB_DIR}, ${LD_LIBRARY_PATH})

# If ACE_ROOT is defined, we toss the copy included in the distribution and
# create a symbolic link to the existing directory.

DEFAULT_ACE_ROOT := ${shell cd ACE/ACE_wrappers; pwd}

# This insures that ACE_ROOT is passed to all other Makefiles

JMAKE = ${MAKE} ACE_ROOT=${DEFAULT_ACE_ROOT} \
                LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${J_LIB_DIR}

#
# tell user what options are available
#

.PHONY : default
default:
	@echo

  ifndef ACE_ROOT
	@echo "If you already have a compatible version of ACE on your computer, then"
	@echo "set ACE_ROOT to point to the top level ACE directory.  (This is part of"
	@echo "the standard ACE configuration.)  If you don't have ACE installed, add"
	@echo "the following to your .cshrc file:"
	@echo
	@echo "  setenv ACE_ROOT ${DEFAULT_ACE_ROOT}"
	@echo
  endif

  ifeq ($(shell whoami),root)

  ifndef JX_INSTALL_ROOT
	@echo "To install the binaries in a directory other than /usr/bin, set the"
	@echo "environment variable JX_INSTALL_ROOT to the desired directory."
	@echo
	@echo "If you do this, JX_INSTALL_ROOT must be on your execution path."
  endif
	@echo
  ifndef JX_LIB_ROOT
	@echo "To install the libraries in a directory other than /usr/lib, set the"
	@echo "environment variable JX_LIB_ROOT to the desired directory."
  endif

  else

  ifndef JX_INSTALL_ROOT
	@echo "To install the binaries in a directory other than ~/bin, set the"
	@echo "environment variable JX_INSTALL_ROOT to the desired directory."
	@echo
	@echo "This path (either ~/bin or JX_INSTALL_ROOT), must be on your execution path."
   ifeq ($(strip ${J_LIB_DIR_ON_PATH}),)
	@echo
	@echo "You must also add the following to your LD_LIBRARY_PATH environment variable:"
   endif
  else
   ifeq ($(strip ${J_LIB_DIR_ON_PATH}),)
	@echo "You must add the following to your LD_LIBRARY_PATH environment variable:"
   endif
  endif
  ifeq ($(strip ${J_LIB_DIR_ON_PATH}),)
	@echo
	@echo "  ${J_LIB_DIR}"
  endif

  endif

	@echo
	@echo "Then run one of the following or check the README file:"
	@echo
	@echo "  ${MAKE} aix4.x-g++     for AIX 4.x with g++"
	@echo "  ${MAKE} debian1.x      for Debian 1.x Linux"
	@echo "  ${MAKE} debian2.x      for Debian 2.x Linux"
	@echo "  ${MAKE} freebsd2.x     for FreeBSD 2.x"
	@echo "  ${MAKE} freebsd3.x     for FreeBSD 3.x"
	@echo "  ${MAKE} hpux10-g++     for HP-UX 10 with g++"
	@echo "  ${MAKE} irix5.3-g++    for IRIX 5.3 with g++"
	@echo "  ${MAKE} irix6.x-g++    for IRIX 6.x with g++"
	@echo "  ${MAKE} linuxppc4      for LinuxPPC r4"
	@echo "  ${MAKE} linuxppc5      for LinuxPPC r5"
	@echo "  ${MAKE} mklinux        for MkLinux"
	@echo "  ${MAKE} openlinux2.2   for OpenLinux 2.2"
	@echo "  ${MAKE} osf1_4.x-g++   for Digital Unix (osf1) 4.x with g++"
	@echo "  ${MAKE} redhat4.2      for RedHat 4.2 Linux"
	@echo "  ${MAKE} redhat5.x      for RedHat 5.x & 6.x Linux"
	@echo "  ${MAKE} redhat5.x-64   for RedHat 5.x & 6.x Linux on 64-bit systems"
	@echo "  ${MAKE} redhat5.x-kcc  for RedHat 5.x & 6.x Linux with KAI C++"
	@echo "  ${MAKE} slackware      for Slackware Linux"
	@echo "  ${MAKE} sunos4-g++     for SunOS 4.x and g++"
	@echo "  ${MAKE} sunos5.4-g++   for SunOS 5.4 (Solaris 2.4) with g++"
	@echo "  ${MAKE} sunos5.5-g++   for SunOS 5.5 (Solaris 2.5) with g++"
	@echo "  ${MAKE} sunos5.5-CC    for SunOS 5.5 (Solaris 2.5) with Sun CC"
	@echo "  ${MAKE} sunos5.6-g++   for SunOS 5.6 (Solaris 2.6) with g++"
	@echo "  ${MAKE} sunos5.6-CC    for SunOS 5.6 (Solaris 2.6) with Sun CC"
	@echo "  ${MAKE} sunos5.7-g++   for SunOS 5.7 (Solaris 2.7) with g++"
	@echo "  ${MAKE} suse5.x        for SuSE 5.x Linux"
	@echo
	@echo "If you are not sure which system you have, run \"uname -a\"."
	@echo

	@echo
	@echo "To clean up afterwards, you can use:"
	@echo
	@echo "  ${MAKE} tidy           removes .o files"
	@echo "  ${MAKE} clean          removes .o files and libraries"

  ifeq ($(shell whoami),root)
	@echo "  ${MAKE} uninstall      removes installed binaries and libraries"
  else
	@echo "  ${MAKE} uninstall      removes installed binaries"
	@echo
	@echo "The libraries are not installed in any system directories, so only use"
	@echo "\"clean\" if you link everything statically."
  endif

	@echo

#
# Useful variables
#

INSTALL_CMD := cd lib; ${JMAKE} install

#
# RedHat 4.2
#

.PHONY : redhat4.2
redhat4.2: linux_common
	@ln -s sys/Linux-Intel-RH4.2  include/make/jx_config
	@${INSTALL_CMD}

#
# RedHat 5.x
#

.PHONY : redhat5.x
redhat5.x: linux_common
	@ln -s sys/Linux-Intel-RH5.x  include/make/jx_config
	@${INSTALL_CMD}

#
# 64-bit RedHat 5.x
#

.PHONY : redhat5.x-64
redhat5.x-64: linux_common
	@ln -s sys/Linux-Alpha-RH5.x  include/make/jx_config
	@${INSTALL_CMD}

#
# RedHat 5.x with KAI C++
#

.PHONY : redhat5.x-kcc
redhat5.x-kcc:
	@ln -s sys/Linux-Intel-RH5.x-kcc \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_empty.h \
           include/jcore/jMissingProto.h
	@ln -s config-linux-kcc.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_linux_kcc.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU

	@${INSTALL_CMD}

#
# Debian
#

.PHONY : debian1.x
debian1.x: redhat4.2

.PHONY : debian2.x
debian2.x: redhat5.x

#
# SuSE
#

.PHONY : suse5.x
suse5.x: linux_common
	@ln -s sys/Linux-Intel-SuSE5.x  include/make/jx_config
	@${INSTALL_CMD}

#
# Slackware
#

.PHONY : slackware
slackware: suse5.x

#
# OpenLinux
#

.PHONY : openlinux2.2
openlinux2.2: linux_common
	@ln -s sys/Linux-Intel-OpenLinux2.2  include/make/jx_config
	@${INSTALL_CMD}

#
# LinuxPPC r4
#

.PHONY : linuxppc4
linuxppc4: linux_common
	@ln -s sys/LinuxPPC_4  include/make/jx_config

	@${RM} ACE/ACE_wrappers/ace/config.h
	@ln -s config-linuxppc4.h \
           ACE/ACE_wrappers/ace/config.h

	@${INSTALL_CMD}

#
# LinuxPPC r5
#

.PHONY : linuxppc5
linuxppc5: linux_common
	@ln -s sys/LinuxPPC_5  include/make/jx_config
	@${INSTALL_CMD}

#
# MkLinux
#

.PHONY : mklinux
mklinux: linux_common
	@ln -s sys/MkLinux_g++  include/make/jx_config

	@${RM} ACE/ACE_wrappers/ace/config.h
	@ln -s config-mklinux.h \
           ACE/ACE_wrappers/ace/config.h

	@${INSTALL_CMD}

#
# Linux shared code
#

.PHONY : linux_common
linux_common: prep
	@ln -s ../../include/missing_proto/jMissingProto_empty.h \
           include/jcore/jMissingProto.h
	@ln -s config-linux.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_linux.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU

#
# FreeBSD
#

.PHONY : freebsd2.x
freebsd2.x: prep
	@ln -s sys/FreeBSD-2.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_empty.h \
           include/jcore/jMissingProto.h
	@ln -s config-freebsd.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_freebsd.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

.PHONY : freebsd3.x
freebsd3.x: prep
	@ln -s sys/FreeBSD-3.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_empty.h \
           include/jcore/jMissingProto.h
	@ln -s config-freebsd-pthread.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_freebsd_pthread.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# SunOS 4.x
#

.PHONY : sunos4-g++
sunos4-g++: prep
	@ln -s sys/SunOS-4_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS4.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos4-g++.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos4_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# SunOS 5.4 (Solaris 2.4)
#

.PHONY : sunos5.4-g++
sunos5.4-g++: prep
	@ln -s sys/SunOS-5.4_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos5.4-g++.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos5_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# SunOS 5.5 (Solaris 2.5)
#

.PHONY : sunos5.5-g++
sunos5.5-g++: prep
	@ln -s sys/SunOS-5.5_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos5.5.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos5_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

.PHONY : sunos5.5-CC
sunos5.5-CC: prep
	@ln -s sys/SunOS-5.5_SunCC \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos5.5.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos5_sunc++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# SunOS 5.6 (Solaris 2.6)
#

.PHONY : sunos5.6-g++
sunos5.6-g++: prep
	@ln -s sys/SunOS-5.6_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos5.6.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos5_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

.PHONY : sunos5.6-CC
sunos5.6-CC: prep
	@ln -s sys/SunOS-5.6_SunCC \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_SunOS5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-sunos5.6.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_sunos5_sunc++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# SunOS 5.7 (Solaris 2.7)
#

.PHONY : sunos5.7-g++
sunos5.7-g++: sunos5.6-g++

#
# HP-UX 10
#

.PHONY : hpux10-g++
hpux10-g++: prep
	@ln -s sys/HP-UX10_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_HPUX10.h \
           include/jcore/jMissingProto.h
	@ln -s config-hpux-10.x-g++.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_hpux_gcc.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# IRIX 5.3
#

.PHONY : irix5.3-g++
irix5.3-g++: prep
	@ln -s sys/IRIX-5.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_IRIX5.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-irix5.3-g++.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_irix5.3_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# IRIX 6.x
#

.PHONY : irix6.x-g++
irix6.x-g++: prep
	@ln -s sys/IRIX-6.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_IRIX6.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-irix6.x-g++.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_irix6.x_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# Digital UNIX (OSF1) 4.x
#

.PHONY : osf1_4.x-g++
osf1_4.x-g++: prep
	@ln -s sys/OSF1-4.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_empty.h \
           include/jcore/jMissingProto.h
	@ln -s config-osf1-4.0.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_osf1_4.0_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# AIX 4.x
#

.PHONY : aix4.x-g++
aix4.x-g++: prep
	@ln -s sys/AIX-4.x_g++ \
           include/make/jx_config
	@ln -s ../../include/missing_proto/jMissingProto_AIX4.x.h \
           include/jcore/jMissingProto.h
	@ln -s config-aix-4.x.h \
           ACE/ACE_wrappers/ace/config.h
	@ln -s platform_aix4_g++.GNU \
           ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU
	@${INSTALL_CMD}

#
# shared code
#

.PHONY : prep
prep: check_install_dir fix_ace clean_links

.PHONY : check_install_dir
check_install_dir:
  ifdef JX_INSTALL_ROOT
	@if { test ! -d ${JX_INSTALL_ROOT}; } then \
         { \
         echo; \
         echo "Please set JX_INSTALL_ROOT to a valid directory and make sure that"; \
         echo "it is on your execution path."; \
         echo; \
         false; \
         } \
     fi
  else
	@if { test ! -d ${HOME}/bin; } then \
         { \
         echo; \
         echo "Please create ~/bin or set JX_INSTALL_ROOT to a valid directory and"; \
         echo "make sure that it is on your execution path."; \
         echo; \
         false; \
         } \
     fi
  endif

#	@if { test -d ${ACE_ROOT} -a ! ACE/ACE_wrappers -ef ${ACE_ROOT}; } then

CHECK_ACE := lib/util/ace/j_has_ace

CC := ${shell if { which gcc > /dev/null; } \
              then { echo gcc; } \
              else { echo ${CC}; } fi }

.PHONY : fix_ace
fix_ace:
  ifdef ACE_ROOT
	@if { ${CC} ${CHECK_ACE}.c -o ${CHECK_ACE}; \
          ${CHECK_ACE} ${ACE_ROOT} ACE/ACE_wrappers; } then \
         { \
         ${RM} -r ACE/ACE_wrappers; \
         ln -s ${ACE_ROOT} ACE/ACE_wrappers; \
         } \
     fi
  endif

.PHONY : clean_links
clean_links:
	@${RM} include/make/jx_config
	@${RM} include/jcore/jMissingProto.h
	@${RM} ACE/ACE_wrappers/ace/config.h
	@${RM} ACE/ACE_wrappers/include/makeinclude/platform_macros.GNU

#
# clean up
#

.PHONY : tidy
tidy:
	@cd lib; ${JMAKE} tidy

.PHONY : clean
clean:
	@cd lib; ${JMAKE} clean

.PHONY : uninstall
uninstall:
	@cd lib; ${JMAKE} uninstall
