#----------------------------------------------------------------------------
# file:         makefile for HDF-EOS (for internal ECS usage only)
#
# environment:  requires environment established by the ECS .buildrc file
# 
# environment variables dependencies:
#   MAKECMD  make command to be run on make files
#   ARCH     architecture (i.e. hardware/os type)
#
#
# author:  Guru Tej S. Khalsa / Space Applications Corp.
#          Adura Adekunjo    / L3 Communications GSI
#
# history:
#       10-Nov-1997 GTSK Initital version
#       23-Oct-2003 AA   Modified for Toolkit Linux Delivery
#
# notes:
#       1) This file is only for use by ECS Configuration Management (CM)
#	   personnel and software.  This file should not be distributed
#	   as part of the standard HDF-EOS delivery.
#----------------------------------------------------------------------------

# define shell to be used (use ksh so we can use the "." command)

SHELL=/bin/ksh

# define MAKE to be equivalant to $(MAKECMD)

MAKE=$(MAKECMD)

# define the location of the HDF-EOS STAGE area

HE_STAGE=/ecs/formal/STAGE/hdfeos

# define the location of the standard HDF-EOS development area

HE_HOME=/ecs/hdfeos

# define HDF base directory

HDF_BASE=/ecs/cots/hdf

# define current HDF version

CURRENT_HDF=HDF4.2r3
CURRENT_SZIP=szip-2.1

# define SDP Toolkit home directory

TK_HOME=/ecs/formal/TOOLKIT

# name of tar files for delivery
HDFEOS_SOURCE_TAR=HDFEOS_SOURCE.tar
HDFEOS_TESTDRIVERS_TAR=HDFEOS_TESTDRIVERS.tar

# define the default compiler optimization level
OPT_LVL=-g

# define default target

default:
	@echo "This file is for use by ECS Configuration Management processes only."

# define a target to build BOTH variations (or "flavors") of HDF-EOS
# for a given "brand" (i.e. architecture type)

full: opt debug

# define a target to build the "debug" variation (or "flavor") of HDF-EOS
# for a given "brand" (i.e. architecture type) ("debug" means the library
# will be compiled for use in a souce code debugger)

debug:
	@$(MAKE) flavor_dir FLAVOR=$@
	@export use_flavor=1; $(MAKE) $(TARGET) OPT_LVL=-g FLAVOR=$@

# define a target to build the "optimized" variation (or "flavor") of
# HDF-EOS for a given "brand" (i.e. architecture type) ("optimized" means the
# library will be optimized for execution speed by the compiler)

opt:
	@$(MAKE) flavor_dir FLAVOR=$@
	@export use_flavor=1; $(MAKE) $(TARGET) OPT_LVL=-O FLAVOR=$@

# define standard CM targets

# NOTE: The standard targets defined by ECS CM are:
#       BuildImake, ProductHs, ProductLibs, LastPass, Deliver
#
#       Here the above targets are shadowed by the targets:
#       BuildImake2, ProductHs2, ProductLibs2, LastPass2, Deliver2
#       respectively.  The original targets only call the target
#       HE_ENV which sets up the HDF-EOS environment and then
#       builds the shadow target.
#       For SGI 32 Falvor build or linux 12 Falvor build these targets
#       renamed to FL_......

BuildImake:
	@$(MAKE) $(MFLAGS) HE_ENV TARGET=$@2

BuildImake2:
	@echo "No Imakefile to build."

ProductHs:
	@$(MAKE) $(MFLAGS) HE_ENV TARGET=$@2

ProductHs2:
	@echo "No header files to generate or promote."
	@/bin/touch include/HE2_config.h

ProductLibs:
	@$(MAKE) $(MFLAGS) HE_ENV TARGET=$@2

ProductLibs2:
	@if [ ! -e $(HDFEOS_HOME)/lib/$(ARCH) ] ; then	\
		cd $(HDFEOS_HOME)/lib;			\
		ln -s `basename $(HDFEOS_LIB)` $(ARCH);		\
	fi
	@cd $(HDFEOS_HOME)/src; $(MAKE) $(MFLAGS) all
	@cd $(TK_HOME)/src/GCT/freeware; $(MAKE) $(MFLAGS) gctp

LastPass:
	@$(MAKE) $(MFLAGS) HE_ENV TARGET=$@2

LastPass2:
	@if [ ! -e $(HDFEOS_HOME)/bin/$(ARCH) ] ; then	\
		cd $(HDFEOS_HOME)/bin;			\
		ln -s `basename $(HDFEOS_BIN)` $(ARCH);\
	fi
	@cd $(HDFEOS_HOME)/util; $(MAKE) $(MFLAGS) all

#	@echo "No utilites to build."



FL_BuildImake:
	@$(MAKE) $(MFLAGS) FL_HE_ENV TARGET=$@2

FL_BuildImake2:
	@echo "No Imakefile to build."

FL_ProductHs:
	@$(MAKE) $(MFLAGS) FL_HE_ENV TARGET=$@2

FL_ProductHs2:
	@echo "No header files to generate or promote."

FL_ProductLibs:
	@$(MAKE) $(MFLAGS) FL_HE_ENV TARGET=$@2
	@/bin/touch include/HE2_config.h

FL_ProductLibs2:
	@if [ ! -e $(HDFEOS_HOME)/lib/$(ARCH) ] ; then	\
		cd $(HDFEOS_HOME)/lib;			\
		ln -s `basename $(HDFEOS_LIB)` $(ARCH);		\
	fi
	@cd $(HDFEOS_HOME)/src; $(MAKE) $(MFLAGS) all
	@cd $(TK_HOME)/src/GCT/freeware; $(MAKE) $(MFLAGS) gctp

FL_LastPass:
	@$(MAKE) $(MFLAGS) FL_HE_ENV TARGET=$@2

FL_LastPass2:
	@if [ ! -e $(HDFEOS_HOME)/bin/$(ARCH) ] ; then	\
		cd $(HDFEOS_HOME)/bin;			\
		ln -s `basename $(HDFEOS_BIN)` $(ARCH);\
	fi
	@cd $(HDFEOS_HOME)/util; $(MAKE) $(MFLAGS) all

#	@echo "No utilites to build."



Deliver:
	@$(MAKE) $(MFLAGS) HE_ENV TARGET=$@2

Deliver2:
	tar cvfX $(HE_HOME)/install/$(HDFEOS_SOURCE_TAR) $(HE_HOME)/make_exclude_list -C /ecs ./hdfeos
	tar cvf $(HE_HOME)/install/$(HDFEOS_TESTDRIVERS_TAR) -C /ecs ./hdfeos/testdrivers

DaacDeliver:
	@if [ $(ARCH) = "sgi6n32" ] ; then				    \
		for arch in sgi6n32 sgi6 ; do			            \
			for targ in FL_ProductHs FL_ProductLibs FL_LastPass ; do	    \
				$(MAKECMD) full TARGET=$$targ ARCH=$$arch;  \
			done;						    \
		done;							    \
	elif [ $(ARCH) = "irix65" ] ; then				    \
		for arch in sgi6n32 sgi6 ; do			            \
			for targ in FL_ProductHs FL_ProductLibs FL_LastPass ; do	    \
				$(MAKECMD) full TARGET=$$targ ARCH=$$arch;  \
			done;						    \
		done;                                                       \
	else								    \
		for targ in FL_ProductHs FL_ProductLibs FL_LastPass ; do		    \
			$(MAKECMD) full TARGET=$$targ;			    \
		done;							    \
	fi

LinuxDaacDeliver:  
	@if [ $(ARCH) = "linux" ] ; then				    \
		for arch in linux ; do					    \
			for targ in FL_ProductHs FL_ProductLibs FL_LastPass ; do	    \
			$(MAKECMD) full TARGET=$$targ ARCH=$$arch;	    \
		done ;							    \
		done ;							    \
	fi

# this is a special target used to establish the HDF-EOS environment

FL_HE_ENV:
	@case $(ARCH) in					\
		hp10)    brand=hp;      ;;                      \
		sgi6n32) brand=sgi32; sgi_mode=n32;  ;;         \
		irix65)  brand=irix65; sgi_mode=65   ;;         \
		sgi6)    brand=sgi64; sgi_mode=64;  ;;		\
		sun5.5)  brand=sun5;    ;;			\
		sun5.8)  brand=sun5.8;    ;;			\
		linux)   LINUX_BRAND=linux; brand=$(ARCH) ;;    \
		*)       brand=$(ARCH); ;;			\
	esac;							\
	postfix=""; 						\
	opt_flag=-O;						\
	case $(FLAVOR) in					\
		*debug)						\
			opt_flag=-g;				\
			postfix=_debug;				\
		;;						\
	esac;							\
	HDFEOS_HOME=$(HE_HOME);					\
	LNX_COMP_FLAG="-m32";					\
	export LNX_COMP_FLAG;					\
	opt_flag=$(OPT_LVL);					\
	export HDFEOS_HOME sgi_mode opt_flag LINUX_BRAND;	\
	. $(HE_HOME)/bin/$${brand}$$postfix/hdfeos_env.ksh;	\
	MAKE="$(MAKECMD)";					\
	AR='ar r';						\
	HDFINC=$(HDF_BASE)/$$brand/$(CURRENT_HDF)/include;	\
	HDFLIB=$(HDF_BASE)/$$brand/$(CURRENT_HDF)/lib;		\
	PGSINC=$(TK_HOME)/include;				\
	SZIPLIB=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/lib;	\
	SZIPINC=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/include;	\
	export MAKE AR HDFINC PGSINC SZIPINC SZIPLIB HDFLIB;	\
	$(MAKE) $(MFLAGS) $(TARGET)

HE_ENV:
	@case $(ARCH) in					\
		hp10)    brand=hp;      ;;                      \
		sgi6n32) brand=sgi32; sgi_mode=n32;  ;;         \
		irix65)  brand=irix65; sgi_mode=65   ;;         \
		sgi6)    brand=sgi64; sgi_mode=64;  ;;		\
		sun5.5)  brand=sun5;    ;;			\
		sun5.8)  brand=sun5.8;    ;;			\
		linux)   LINUX_BRAND=linux; brand=$(ARCH);;     \
		*)       brand=$(ARCH); ;;			\
	esac;							\
	HDFEOS_HOME=$(HE_HOME);					\
	opt_flag=$(OPT_LVL);					\
	export HDFEOS_HOME sgi_mode opt_flag LINUX_BRAND;	\
	. $(HE_HOME)/bin/tmp/hdfeos_env.ksh.tmp;		\
	if [ $(ARCH) = "linux" ] ; then				\
	CC="gcc -m32";						\
	export CC;						\
	LNX_COMP_FLAG="-m32";					\
	export LNX_COMP_FLAG;					\
	fi;							\
	MAKE="$(MAKECMD)";					\
	AR='ar r';						\
	HDFINC=$(HDF_BASE)/$$brand/$(CURRENT_HDF)/include;	\
	HDFLIB=$(HDF_BASE)/$$brand/$(CURRENT_HDF)/lib;		\
	PGSINC=$(TK_HOME)/include;				\
	SZIPLIB=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/lib;	\
	SZIPINC=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/include;	\
	export MAKE AR HDFINC PGSINC SZIPINC SZIPLIB HDFLIB;	\
	$(MAKE) $(MFLAGS) $(TARGET)

flavor_dir:
	@sgi_mode=sgi32;					\
	LNX_COMP_FLAG="-m32";					\
	export LNX_COMP_FLAG;					\
	opt_flag=-O;						\
	postfix="";						\
	case $(FLAVOR) in					\
		*debug)						\
			opt_flag=-g;				\
			postfix=_debug;				\
		;;						\
	esac;							\
	case $(ARCH) in						\
		hp10)						\
			brand=hp;				\
		;;						\
		sgi6n32)					\
			brand=sgi32;				\
			sgi_mode=n32;				\
		;;						\
		irix65)                                         \
			brand=irix65;				\
			sgi_mode=65;				\
		;;						\
		sgi6)						\
			brand=sgi64;				\
			sgi_mode=64;				\
		;;						\
		sun5.5)						\
			brand=sun5;				\
		;;						\
		sun5.8)						\
			brand=sun5.8;				\
		;;						\
		*)						\
			brand=$(ARCH);				\
		;;						\
	esac;							\
	he_obj=$(HE_HOME)/obj/$${brand}$$postfix;		\
	he_lib=$(HE_HOME)/lib/$${brand}$$postfix;		\
	he_bin=$(HE_HOME)/bin/$${brand}$$postfix;		\
	tmpdir=$(HE_HOME)/bin/tmp;				\
	if [ ! -d $$he_obj ] ; then				\
		mkdir $$he_obj;					\
	fi;							\
	if [ ! -d $$he_lib ] ; then				\
		mkdir $$he_lib;					\
	fi;							\
	if [ ! -d $$he_bin ] ; then				\
		mkdir $$he_bin;					\
	fi;							\
	hdflib=$(HDF_BASE)/$${brand}/$(CURRENT_HDF)/lib;	\
	hdfinc=$(HDF_BASE)/$${brand}/$(CURRENT_HDF)/include;	\
	sziplib=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/lib;	\
	szipinc=$(HDF_BASE)/$${brand}/$(CURRENT_SZIP)/include;	\
	if [ ! -f $$he_bin/hdfeos_env.csh ] ; then			\
		cd $$he_bin;						\
		echo "setenv HDFEOS_HOME $(HE_HOME)" > hdfeos_env.csh;	\
		echo "setenv HDFLIB $$hdflib" >> hdfeos_env.csh;	\
		echo "setenv HDFINC $$hdfinc" >> hdfeos_env.csh;	\
		echo "setenv SZIPLIB $$sziplib" >> hdfeos_env.csh;	\
		echo "setenv SZIPINC $$szipinc" >> hdfeos_env.csh;	\
		echo "set sgi_mode=$$sgi_mode" >> hdfeos_env.csh;	\
		echo "set opt_flag=$$opt_flag" >> hdfeos_env.csh;	\
		echo "set use_flavor=1" >> hdfeos_env.csh;		\
		sed "s^LNX_CMP_FLAG^$$LNX_COMP_FLAG^" 			\
		$$tmpdir/hdfeos_env.csh.tmp >> hdfeos_env.csh;		\
	fi;								\
	if [ ! -f $$he_bin/hdfeos_env.ksh ] ; then			\
		cd $$he_bin;						\
		echo "HDFEOS_HOME=$(HE_HOME)" > hdfeos_env.ksh;		\
		echo "HDFLIB=$$hdflib" >> hdfeos_env.ksh;		\
		echo "HDFINC=$$hdfinc" >> hdfeos_env.ksh;		\
		echo "SZIPLIB=$$sziplib" >> hdfeos_env.ksh;		\
		echo "SZIPINC=$$szipinc" >> hdfeos_env.ksh;		\
		echo "sgi_mode=$$sgi_mode" >> hdfeos_env.ksh;		\
		echo "opt_flag=$$opt_flag" >> hdfeos_env.ksh;		\
		echo "use_flavor=1" >> hdfeos_env.ksh;			\
		sed "s^LNX_CMP_FLAG^$$LNX_COMP_FLAG^" 			\
		$$tmpdir/hdfeos_env.ksh.tmp >> hdfeos_env.ksh;		\
	fi

