all: agentController

UNAME = $(shell uname -s)
MNAME = $(shell uname -m)
EXTENS = script

CP_NO_DREF_OPT = -R

ifeq ($(UNAME), SunOS)
    # Solaris requires the -R option to be used in conjunction with -P
    CP_NO_DREF_OPT = -RP
    ifeq ($(MNAME), i86pc)
        ifdef SIXTYFOURBIT
            BUILD_PLATFORM = solaris_x64
            EXTENS = script64
        else
            BUILD_PLATFORM = solaris_x86
        endif    
    else
        ifdef SIXTYFOURBIT
            BUILD_PLATFORM = solaris-sparc-v9
            EXTENS = script64
        else
            BUILD_PLATFORM = solaris-sparc
        endif
    endif
endif

ifeq ($(UNAME), Linux)
	CP_NO_DREF_OPT = -d
	ifeq ($(MNAME), s390x)
		ifdef FORCE31
			BUILD_PLATFORM = linux_s390
		else
			BUILD_PLATFORM = linux_s390x
			EXTENS = script64
		endif
	else
		ifeq ($(MNAME), s390)
			BUILD_PLATFORM = linux_s390
		else
			ifeq ($(MNAME), ppc64)
				BUILD_PLATFORM = linux_ppc64
			else
				ifeq ($(MNAME), ia64)
					BUILD_PLATFORM = linux_ipf
					EXTENS = script64
				else
					ifeq ($(MNAME), x86_64)
						ifdef FORCE32
							BUILD_PLATFORM = linux_ia32
						else
							BUILD_PLATFORM = linux_em64t
							EXTENS = script64
						endif
					else
						BUILD_PLATFORM = linux_ia32
					endif
				endif
			endif
		endif
	endif
endif

ifeq ($(UNAME), AIX)
	CP_NO_DREF_OPT = -h
	ifdef SIXTYFOURBIT
		BUILD_PLATFORM = aix_ppc64
		EXTENS = script64
	else
		BUILD_PLATFORM = aix_ppc
	endif
endif

ifeq ($(UNAME), OS/390)
    # Note:	for the -P option to prevent the cp command from following symbolic links,
    #		the environment variable _UNIX03=YES must be set. The -P option can be
    #		used only in conjunction with the -R or -r options
    CP_NO_DREF_OPT = -RP
    ifdef SIXTYFOURBIT
        BUILD_PLATFORM = zos_s390x
        EXTENS = script64
    else
        BUILD_PLATFORM = zos_s390
    endif
endif

agentController:
	mkdir -p lib
	cd build; \
	if [ ! -x ./build_tptp_ac.${EXTENS} ]; then chmod a+x ./build_tptp_ac.${EXTENS}; fi; \
	./build_tptp_ac.${EXTENS}; \
	if [ ! -x ./build_tptp_samples.${EXTENS} ]; then chmod a+x ./build_tptp_samples.${EXTENS}; fi; \
	./build_tptp_samples.${EXTENS}; \
	cd ..

clean:
	cd build; \
	if [ ! -x ./build_tptp_ac.${EXTENS} ]; then chmod a+x ./build_tptp_ac.${EXTENS}; fi; \
	./build_tptp_ac.${EXTENS} veryclean; \
	if [ ! -x ./build_tptp_samples.${EXTENS} ]; then chmod a+x ./build_tptp_samples.${EXTENS}; fi; \
	./build_tptp_samples.${EXTENS} veryclean; \
	cd ..
	
package:
#	Note about the packing process.  Only files that should appear in the SDK should be added here.  Runtime files should be added after creating agntctrl.base.$(BUILD_PLATFORM).zip
	mkdir -p packaging/bin
	mkdir -p packaging/lib

	if [ -e packaging_md/plugins ]; then cp -r packaging_md/plugins packaging; \
	fi;

	if [ -e ${CBE_SDK_HOME}/lib/libcbe.so ]; then \
	    cp ${CBE_SDK_HOME}/lib/libcbe.so packaging/lib; \
    fi;

#	Some Solaris systems are unable to copy symbolic links, to work around this, the files are zipped up and unzipped into the correct location.
ifeq ($(UNAME), SunOS)
    # Solaris x86 is able to copy symbolic links
    ifeq ($(MNAME), i86pc)
	    cp $(CP_NO_DREF_OPT) lib/lib* packaging/lib
	    cp $(CP_NO_DREF_OPT) $(XERCESC_HOME)/lib/libxer* packaging/lib
    else
	    zip -qjy tmp.zip lib/lib*
	    unzip -q tmp.zip -d packaging/lib
	    rm -rf tmp.zip
	    zip -qjy tmp.zip $(XERCESC_HOME)/lib/libxer*
	    unzip -q tmp.zip -d packaging/lib
	    rm -rf tmp.zip
    endif
else
	cp $(CP_NO_DREF_OPT) lib/lib* packaging/lib
    # Do not package xerces-c for z/OS.
    ifneq ($(UNAME), OS/390) 
	    cp $(CP_NO_DREF_OPT) $(XERCESC_HOME)/lib/libxer* packaging/lib
    endif
endif
	cp bin/readme.txt packaging/bin
ifeq ($(UNAME), SunOS)
	cp -r config packaging
	cp -r agents packaging
	cp packaging_md/solaris/about.html packaging
	cp -r packaging_md/solaris/about_files packaging
else
    ifeq ($(UNAME), AIX)
	    cp -r config packaging
	    cp -r agents packaging
	    cp packaging_md/aix/about.html packaging
	    cp -r packaging_md/aix/about_files packaging
    else
        ifeq ($(UNAME), OS/390)
	        cp -r packaging_md/zos/config packaging
	        cp -r packaging_md/zos/agents packaging
	        cp packaging_md/zos/about.html packaging
	        cp -r packaging_md/zos/about_files packaging	        
        else
	        cp -r config packaging
	        cp -r agents packaging
	        cp packaging_md/linux/about.html packaging
	        cp -r packaging_md/linux/about_files packaging
        endif
    endif
endif


	
	if [ ! -x ./build/convert_agent_xml.script ]; then chmod a+x ./build/convert_agent_xml.script; fi; \
	./build/convert_agent_xml.script packaging/agents

	if [ -e packaging/lib/libresutils.a ]; then rm -f packaging/lib/libresutils.a; fi;
	if [ -e packaging/lib/libperflin.so ]; then rm -f packaging/lib/libperflin.*; fi;
	if [ -e packaging/lib/librac.so ]; then rm -f packaging/lib/librac.*; fi;

#	Create agntctrl.base.zip which will become the SDK after adding include and samples folders
#	Certain files will have to be excluded as they should only be in the runtime but had to be added before creating the runtime.

ifeq ($(UNAME), OS/390)
	rm -f agntctrl.base.$(BUILD_PLATFORM).pax
	cd packaging; \
	pax -w -f ../agntctrl.base.$(BUILD_PLATFORM).pax -x ustar *; \
	cd ..
else
	rm -f agntctrl.base.$(BUILD_PLATFORM).zip
	cd packaging; \
	zip -r9y ../agntctrl.base.$(BUILD_PLATFORM).zip * -x bin/*.jar lib/*.jar /plugins/* /swt_licenses/* lib/libswt*; \
	cd ..
endif

#	Files that will appear only in the runtime and not the SDK can now be added.

#	Copy about files into packaged zips.
ifeq ($(UNAME), SunOS)
	cp packaging_md/solaris/book.css packaging
	cp packaging_md/solaris/getting_started.html packaging
else
    ifeq ($(UNAME), AIX)
	    cp packaging_md/aix/book.css packaging
	    cp packaging_md/aix/getting_started.html packaging
    else
        ifeq ($(UNAME), OS/390)
	        cp packaging_md/zos/book.css packaging
	        cp packaging_md/zos/getting_started.html packaging
        else
	        cp packaging_md/linux/book.css packaging
	        cp packaging_md/linux/getting_started.html packaging
        endif
    endif
endif
	cp -r packaging_md/Resources packaging
	cp -r packaging_md/security packaging

#	Some Solaris systems are unable to copy symbolic links, to work arround this, the files are zipped up and unzipped into the correct location.
ifeq ($(UNAME), SunOS)
    ifeq ($(MNAME), i86pc)
	    cp $(CP_NO_DREF_OPT) bin/SampleClient bin/TimeCollector bin/tptp* bin/ACServer bin/ACStart.sh bin/ACStop.sh bin/ACVersion.sh bin/ChkPass packaging/bin
    else
	    zip -qjy tmp.zip bin/SampleClient bin/TimeCollector bin/tptp* bin/ACServer bin/ACStart.sh bin/ACStop.sh bin/ACVersion.sh bin/ChkPass
	    unzip tmp.zip -d packaging/bin
	    rm -rf tmp.zip
    endif
else
    ifeq ($(UNAME), AIX)
	    cp $(CP_NO_DREF_OPT) bin/SampleClient bin/TimeCollector bin/tptp* bin/ACServer bin/ACVersion.sh bin/ChkPass packaging/bin
	    cp packaging_md/aix/bin/ACStart.sh packaging/bin/ACStart.sh
	    cp packaging_md/aix/bin/ACStop.sh packaging/bin/ACStop.sh
    else
        ifeq ($(UNAME), OS/390)
	        cp $(CP_NO_DREF_OPT) bin/SampleClient bin/TimeCollector bin/tptp* bin/ACServer bin/ChkPass packaging/bin
	        cp packaging_md/zos/bin/ACStart.sh packaging/bin/ACStart.sh
	        cp packaging_md/zos/bin/ACStop.sh packaging/bin/ACStop.sh
	        cp packaging_md/zos/bin/ACVersion.sh packaging/bin/ACVersion.sh
        else
	        cp $(CP_NO_DREF_OPT) bin/SampleClient bin/TimeCollector bin/tptp* bin/ACServer bin/ACStart.sh bin/ACStop.sh bin/ACVersion.sh bin/ChkPass packaging/bin
        endif
    endif
endif
	
# Create the symbolic links in the bin directory for RA* versions of the AC* files
	cd packaging/bin; \
	ln -s ./ACServer RAServer; \
	ln -s ./ACStart.sh RAStart.sh; \
	ln -s ./ACStop.sh RAStop.sh; \
	cd ../..

# Add the SetConfig.sh to bin
	cp packaging_md/linux/bin/SetConfig.sh packaging/bin

# Update the shell script file permissions
	chmod 755 packaging/bin/*.sh

# Remove swt libraries and licenses from non ia32 platforms
ifneq ($(BUILD_PLATFORM), linux_ia32)
	rm -rf packaging/swt_licenses packaging/lib/libswt*
endif

ifeq ($(UNAME), OS/390)
	cp agntctrl.base.$(BUILD_PLATFORM).pax  agntctrl.$(BUILD_PLATFORM).pax
	cd packaging; \
	pax -w -a -f ../agntctrl.$(BUILD_PLATFORM).pax -x ustar *; \
	cd ..

	cp -f agntctrl.base.$(BUILD_PLATFORM).pax agntctrl.$(BUILD_PLATFORM).sdk.pax
else
	cp agntctrl.base.$(BUILD_PLATFORM).zip  agntctrl.$(BUILD_PLATFORM).zip
	cd packaging; \
	zip -ur9y ../agntctrl.$(BUILD_PLATFORM).zip *; \
	cd ..

	cp -f agntctrl.base.$(BUILD_PLATFORM).zip agntctrl.$(BUILD_PLATFORM).sdk.zip
endif

ifeq ($(UNAME), OS/390)
    # Find and package the .x files in the z/OS SDK.
	if [ -e ${CBE_SDK_HOME}/lib/libcbe.x ]; then \
	    cp ${CBE_SDK_HOME}/lib/libcbe.x packaging/lib; \
    fi;
	for i in `find src -name *.x`; do cp $$i packaging/lib ; done
    
	cd packaging; \
	pax -w -a -f ../agntctrl.$(BUILD_PLATFORM).sdk.pax -x ustar lib/*.x; \
	cd ..
	
	pax -w -a -f agntctrl.$(BUILD_PLATFORM).sdk.pax -x ustar include
	
    # exclude the .o and .dsp files from being paxed
	pax -w -a -f agntctrl.$(BUILD_PLATFORM).sdk.pax -x ustar `find samples/*/* | grep -v -e "\.o" -e "\.dsp"`

	rm -f agntctrl.base.$(BUILD_PLATFORM).pax
else
	zip -r9uy agntctrl.$(BUILD_PLATFORM).sdk.zip include
	zip -r9uy agntctrl.$(BUILD_PLATFORM).sdk.zip samples -x \*.o \*.dsp

	rm -f agntctrl.base.$(BUILD_PLATFORM).zip
endif
