# these variables can be overridden in the INCLUDE file
RELEASE=1.0.3

include INCLUDE

PKG=slp
PKGINST=openslp
COMPONENT=OpenSLP
DISTDIR=$(TOP)/dist
DESTDIR=$(DISTDIR)
ROOT=$(TOP)/$(PKGINST)-$(RELEASE)
PKGDIR=$(TOP)/pkg
IMGDIR=$(TOP)/image
VOLDIR=$(TOP)/VOLS
DOCDIR=usr/share/doc/packages
SSODIR=$(TOP)/sso
PKGOUT=$(TOP)/pkgmk.$(PKG).out
BUILD_OUT=$(TOP)/build.out
MAKE_FLAGS=DESTDIR=$(DISTDIR) DOC_DIR=/$(DOCDIR)/$(PKGINST)-$(RELEASE)

files = Makefile \
	common/Makefile \
	config.cache \
	config.log \
	config.status \
	install.out \
	libslp/Makefile \
	libslpattr/Makefile \
	libtool \
	slp.list \
	slpd/Makefile \
	slptool/Makefile \
	test/Makefile

all: clean-logs chkvars configure build install package

chkvars:
	@-$(MAKE) --version > /tmp/make.pid 2>&1
	@(foo=`cat /tmp/make.pid`; \
	 set -- $$foo; \
	 if [ "$$1" = "Usage:" -o "$$1" = "UX:make:" ] ; then \
		echo "PATH needs to find gmake first"; \
		echo "install gds and/or set PATH with /usr/local/bin before other paths"; \
		exit 1; \
	 fi;)
	@-rm -f /tmp/make.pid
	@if [ -z "$(TOP)" ]; then \
		echo "TOP not set"; \
		echo "Adding \"TOP\" to INCLUDE file"; \
		/bin/echo -n "TOP=" > INCLUDE; \
		pwd >> INCLUDE; \
		echo "restart the build"; \
		exit 1; \
	fi
	@if [ -z "$(ROOT)" ]; then \
		echo "ROOT not set"; \
		exit 1; \
	fi
	@if [ -z "$(DISTDIR)" ]; then \
		echo "DISTDIR not set"; \
		exit 1; \
	fi
	@if [ -z "$(PKGDIR)" ]; then \
		echo "PKGDIR not set"; \
		exit 1; \
	fi
	@if [ -z "$(PKGOUT)" ]; then \
		echo "PKGOUT not set"; \
		exit 1; \
	fi
	@if [ -z "$(IMGDIR)" ]; then \
		echo "IMGDIR not set"; \
		exit 1; \
	fi

uninstall: chkvars
	@echo "Uninstalling source..." | tee -a $(BUILD_OUT)
	@-(cd $(ROOT); DISTDIR=$(DISTDIR) make $(MAKE_FLAGS) uninstall)

install: chkvars
	@echo "Installing source..." | tee -a $(BUILD_OUT)
	@-(cd $(ROOT); make $(MAKE_FLAGS) install) >> $(BUILD_OUT) 2>&1

	@echo "Fixing installation omissions..." | tee -a $(BUILD_OUT)
	@[ -d $(DISTDIR)/etc ] || mkdir -p $(DISTDIR)/etc
	@[ -d $(DISTDIR)/etc/init.d ] || mkdir -p $(DISTDIR)/etc/init.d
	@[ -d $(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE) ] || \
		mkdir -p $(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE)
	@[ -d $(DISTDIR)/etc/sysconfig/daemons ] || \
		mkdir -p $(DISTDIR)/etc/sysconfig/daemons
	@[ -f $(DISTDIR)/etc/init.d/slpd ] || \
		cp $(ROOT)/etc/slpd.calderaUNIX_init \
			$(DISTDIR)/etc/init.d/slpd >> $(BUILD_OUT) 2>&1

	@[ -f $(DISTDIR)/etc/sysconfig/daemons/slpd ] || \
		cp $(TOP)/etc/slpd.init \
			$(DISTDIR)/etc/sysconfig/daemons/slpd \
			>> $(BUILD_OUT) 2>&1

	@cat $(TOP)/doc/$(COMPONENT).html.sh | \
		sed -e 's;__RELEASE__;$(RELEASE);g' \
		> $(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE)/$(COMPONENT).html \
		2>> $(BUILD_OUT)
	@cp $(TOP)/doc/$(PKGINST).book \
		$(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE) >> $(BUILD_OUT) 2>&1
	@cp $(TOP)/doc/$(PKGINST).node \
		$(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE) >> $(BUILD_OUT) 2>&1
	@cp $(TOP)/doc/$(PKGINST).view \
		$(DISTDIR)/$(DOCDIR)/$(PKGINST)-$(RELEASE) >> $(BUILD_OUT) 2>&1

	@echo "Removing CVS directories from distribution tree..." \
		| tee -a $(BUILD_OUT)
	@for f in `find $(DISTDIR) -name CVS`; do \
		rm -rf $$f; \
	done

build: chkvars configure
	@echo "Building source..." | tee -a $(BUILD_OUT)
	@-(cd $(ROOT); make $(MAKE_FLAGS) all) >> $(BUILD_OUT) 2>&1

$(ROOT)/Makefile:
	@echo "Rerunning configure operations..." | tee -a $(BUILD_OUT)
	@-(cd $(ROOT); sh ./configure) >> $(BUILD_OUT) 2>&1

configure: chkvars $(ROOT)/Makefile

$(PKGDIR)/$(PKGINST)doc/pkginfo: $(PKGDIR)/$(PKGINST)doc/pkginfo.sh
	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"UnixWare" | "OpenUNIX") \
		cat $@.sh | sed -e 's;__RELEASE__;$(RELEASE);g' > $@ \
			| tee -a $(BUILD_OUT); \
		;; \
	esac)

$(PKGDIR)/$(PKGINST)doc/prototype: $(PKGDIR)/$(PKGINST)doc/prototype.sh
	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"UnixWare" | "OpenUNIX") \
		cat $@.sh | sed -e 's;__RELEASE__;$(RELEASE);g' > $@ \
			| tee -a $(BUILD_OUT); \
		;; \
	esac)

$(PKGDIR)/$(PKGINST)/pkginfo: $(PKGDIR)/$(PKGINST)/pkginfo.sh
	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"UnixWare" | "OpenUNIX") \
		cat $@.sh | sed -e 's;__RELEASE__;$(RELEASE);g' > $@ \
			| tee -a $(BUILD_OUT); \
		;; \
	esac)

$(SSODIR)/input/$(COMPONENT)_doc.pkg: $(SSODIR)/input/$(COMPONENT)_doc.pkg.sh
	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"SCO_SV") \
		cat $@.sh | sed -e 's;__RELEASE__;$(RELEASE);g' > $@ \
			| tee -a $(BUILD_OUT); \
		;; \
	esac)

$(SSODIR)/input/$(COMPONENT).pkg: $(SSODIR)/input/$(COMPONENT).pkg.sh
	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"SCO_SV") \
	cat $@.sh | sed -e 's;__RELEASE__;$(RELEASE);g' > $@ \
			| tee -a $(BUILD_OUT); \
		;; \
	esac)

package: chkvars $(PKGDIR)/$(PKGINST)/pkginfo $(PKGDIR)/$(PKGINST)doc/pkginfo $(PKGDIR)/$(PKGINST)doc/prototype install $(SSODIR)/input/$(COMPONENT)_doc.pkg $(SSODIR)/input/$(COMPONENT).pkg
	@echo "Executing packaging operations..." | tee -a $(BUILD_OUT)

	@-(systype=`uname -s 2>/dev/null`; \
	case "$$systype" in \
	"UnixWare" | "OpenUNIX") \
		cd $(PKGDIR)/$(PKGINST); \
		PSTAMP="`uname -s` `date +%m/%d/%Y` skunkware8"; \
		[ -d $(IMGDIR) ] || mkdir -p $(IMGDIR); \
		pkgmk -or $(DISTDIR) -d $(IMGDIR) -p "$$PSTAMP" $(PKGINST) >> $(PKGOUT) 2>&1; \
		cd $(PKGDIR)/$(PKGINST)doc; \
		pkgmk -or $(DISTDIR) -d $(IMGDIR) -p "$$PSTAMP" $(PKGINST)d >> $(PKGOUT) 2>&1; \
		pkgtrans -s $(IMGDIR) $(IMGDIR)/$(PKGINST).pkg $(PKGINST) $(PKGINST)d >> $(PKGOUT) 2>&1; \
		;; \
	"SCO_SV") \
		[ -d $(VOLDIR) ] || mkdir -p $(VOLDIR); \
		cd $(DISTDIR); \
		find . | cpio -dumpv $(VOLDIR) >> $(PKGOUT) 2>&1; \
		cd $(SSODIR); \
		find . | cpio -dumpv $(VOLDIR) >> $(PKGOUT) 2>&1; \
		cd $(VOLDIR); \
		[ -x $(VOLDIR)/MakeSSO ] && { \
			$(COMPONENT)_VER=$(RELEASE) $(VOLDIR)/MakeSSO >> $(PKGOUT); \
		}; \
		;; \
	*) \
		;; \
	esac)

clean-dist: chkvars
	@echo "Cleaning distribution tree..." | tee -a $(BUILD_OUT)
	@[ -d $(DISTDIR) ] && rm -rf $(DISTDIR) || /bin/true

clean-package clean-pkg: chkvars
	@echo "Cleaning package tree..." | tee -a $(BUILD_OUT)
	@[ -f $(PKGDIR)/$(PKGINST)/pkginfo ] && { \
		echo "\tremoving file $(PKGDIR)/$(PKGINST)/pkginfo..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(PKGDIR)/$(PKGINST)/pkginfo; } || /bin/true
	@[ -f $(PKGDIR)/$(PKGINST)doc/pkginfo ] && { \
		echo "\tremoving file $(PKGDIR)/$(PKGINST)doc/pkginfo..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(PKGDIR)/$(PKGINST)doc/pkginfo; } || /bin/true
	@[ -f $(PKGDIR)/$(PKGINST)doc/prototype ] && { \
		echo "\tremoving file $(PKGDIR)/$(PKGINST)doc/prototype..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(PKGDIR)/$(PKGINST)doc/prototype; } || /bin/true
	@[ -f $(PKGDIR)/pkginfo ] && { \
		echo "\tremoving file $(PKGDIR)/pkginfo..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(PKGDIR)/pkginfo; } || /bin/true
	@[ -f $(PKGDIR)/prototype ] && { \
		echo "\tremoving file $(PKGDIR)/prototype..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(PKGDIR)/prototype; } || /bin/true
	@[ -f $(SSODIR)/input/$(COMPONENT)_doc.pkg ] && { \
		echo "\tremoving file $(SSODIR)/input/$(COMPONENT)_doc.pkg..." \
			| tee -a $(BUILD_OUT); \
		rm -f $(SSODIR)/input/$(COMPONENT)_doc.pkg; } || /bin/true
	@[ -f $(IMGDIR)/$(PKGINST).pkg ] && { \
		echo "\tremoving $(IMGDIR)/$(PKGINST).pkg package stream..." | tee -a $(BUILD_OUT); \
		rm -f $(IMGDIR)/$(PKGINST).pkg; } || /bin/true
	@[ -d $(IMGDIR)/$(PKGINST) ] && { \
		echo "\tremoving $(IMGDIR)/$(PKGINST) directory..." | tee -a $(BUILD_OUT); \
		rm -rf $(IMGDIR)/$(PKGINST); } || /bin/true
	@[ -d $(IMGDIR)/$(PKGINST)d ] && { \
		echo "\tremoving $(IMGDIR)/$(PKGINST)d directory..." | tee -a $(BUILD_OUT); \
		rm -rf $(IMGDIR)/$(PKGINST)d; } || /bin/true
	@-[ -d $(IMGDIR) ] && { \
		echo "\tremoving $(IMGDIR) directory..." | tee -a $(BUILD_OUT); \
		rmdir $(IMGDIR) 2>/dev/null; } || /bin/true
	@-[ -n "$(VOLDIR)" -a -d "$(VOLDIR)" ] && { \
		echo "\tremoving $(VOLDIR) directory..." | tee -a $(BUILD_OUT); \
		rm -rf $(VOLDIR) 2>/dev/null; } || /bin/true
	@[ -f $(PKGOUT) ] && { \
		echo "\tremoving $(PKGOUT)..." | tee -a $(BUILD_OUT); \
		rm -f $(PKGOUT); } || /bin/true

clean-build: chkvars
	@echo "Cleaning source tree..." | tee -a $(BUILD_OUT)
	@-[ -f $(ROOT)/Makefile ] && (cd $(ROOT); make $(MAKE_FLAGS) clean) || /bin/true


clean-config: chkvars
	@echo "Removing configure operations..." | tee -a $(BUILD_OUT)
	@for f in $(files); do \
		[ -f "$(ROOT)/$$f" ] && rm -f $(ROOT)/$$f || /bin/true; \
	done

clean-logs: chkvars
	@-[ -f $(BUILD_OUT) ] && rm -f $(BUILD_OUT) || /bin/true
	@-[ -f $(PKGOUT) ] && rm -f $(PKGOUT) || /bin/true

clean: clean-dist clean-build clean-config clean-pkg
