#  Copyright (c) 2000-2004 Silicon Graphics, Inc All Rights Reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it would be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
#  Mountain View, CA  94043, or:
#
#  http://www.sgi.com

# Set up the TOP directory for the build 
BUILD_TOP=$(PWD)
export BUILD_TOP

# Name of the build definitions file (contains command macros, command
# options, and installation directory macros)
BUILDDEFS=$(BUILD_TOP)/Builddefs
export BUILDDEFS

# Include file of build definitions
include $(BUILDDEFS)

##SGI # Version of kernel (if target not already set).
##SGI # Used to get location of kernel include files.
##SGI KVERSION=2.4.X
##SGI #ifndef  $(KVERSION)
##SGI #	KVERSION  = $(shell $(UNAME) -r)
##SGI #endif

# Variables used for all build environment
#	Location of kernel include files
KINCLUDES   :=/usr/src/linux
								    ##SGI
export KINCLUDES

# Variables used for all build environments
SPECFILE=./job.spec
VERSION=$(shell $(GREP) "Version:" $(SPECFILE) | $(CUT) -d' ' -f2 )
RPMVERSION=$(shell $(GREP) "Release:" $(SPECFILE) | $(CUT) -d' ' -f2 )

# Subdirectories to be traversed in build
LIB_DIRS = lib
PAM_DIRS = pam
CMD_DIRS = cmd
MAN_DIRS = man
INC_DIRS = include
SCP_DIRS = scripts
TST_DIRS = tests
SUB_DIRS = $(INC_DIRS) $(LIB_DIRS) $(MAN_DIRS) $(SCP_DIRS) $(CMD_DIRS) $(PAM_DIRS) $(TST_DIRS)

# Needed to build non-ProPack RPMs
# We only support ia32 and ia64.  Check if we're ia64.  If we're not, we're
# i386.
ARCH=$(shell if test `uname -m` = ia64; then echo ia64; else echo i386; fi)
RPMS=job-$(VERSION)-$(RPMVERSION).$(ARCH).rpm
SRPMS=job-$(VERSION)-$(RPMVERSION).src.rpm
TARFILE=./job-$(VERSION).tar.gz ##SGI
REQFILE=include/linux/paggctl.h


# Target for building all subdirectories
.PHONY: build
build: $(patsubst %, _build_%, $(SUB_DIRS))

# Target to build the commands subdirectory
.PHONY: lib
lib: $(patsubst %, _build_%, $(LIB_DIRS))

# Target to build the pam module subdirectory
.PHONY: pam
pam: $(patsubst %, _build_%, $(PAM_DIRS))

# Target to build the commands subdirectory
.PHONY: cmd
cmd: $(patsubst %, _build_%, $(CMD_DIRS))

# Target for the man page subdirectory
.PHONY: man
man: $(patsubst %, _build_%, $(MAN_DIRS))

# Target for the tests direcotry
.PHONY: tests
tests: $(patsubst %, _build_%, $(TST_DIRS))

# Target for each of the subdirectories
.PHONY: $(patsubst %, _build_%, $(SUB_DIRS))
$(patsubst %, _build_%, $(SUB_DIRS)):
	$(MAKE) -C $(patsubst _build_%, %, $@) build

# Target to install the binaries
.PHONY: install
install: $(patsubst %, _inst_%, $(SUB_DIRS))

.PHONY: $(patsubst %, _inst_%, $(SUB_DIRS))
$(patsubst %, _inst_%, $(SUB_DIRS)):
	$(MAKE) -C $(patsubst _inst_%, %, $@) install 

# Target to clean up after a build
.PHONY: clean
clean: $(patsubst %, _clean_%, $(SUB_DIRS))
	$(RM) -f $(TARFILE) ##SGI

.PHONY: $(patsubst %, _clean_%, $(SUB_DIRS))
$(patsubst %, _clean_%, $(SUB_DIRS)):
	$(MAKE) -C $(patsubst _clean_%, %, $@) clean 

.PHONY: exports
exports:

.PHONY: realclean
realclean:

# Target to build RPM packages
# Note that RPM building happens outside of this Makefile for official SGI
# ProPack builds.
rpm:
	# 1. Make sure required kernel include file is present
	# 2. Make sure the directories for the RPM build are 
	#    present and create as required.
	# 3. Create tarball file of source
	# 4. Execute rpm against the tarball
	if [ ! -f $(KINCLUDES)/$(REQFILE) ]; then \
		$(ECHO) "Make failed: $(KINCLUDES)/$(REQFILE)"; \
		$(ECHO) "not found.  PAGG header files are required"; \
		$(ECHO) "for build to succeed."; \
		exit 1; \
	fi; \
	$(TAR) -zcvf $(TARFILE) .
	$(RPMBUILD) $(RPM_OPTS) -ta $(TARFILE)
	$(RM) -f $(TARFILE)
