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

include INCLUDE

PKG=ssl
PKGINST=openssl
COMPONENT=OpenSSL
DISTDIR=$(TOP)/dist
DESTDIR=$(DISTDIR)
ROOT=$(TOP)/$(PKGINST)-$(RELEASE)
PKGDIR=$(TOP)/pkg
IMGDIR=$(TOP)/image
VOLDIR=$(TOP)/VOLS
SSODIR=$(TOP)/sso
PKGOUT=$(TOP)/pkgmk.$(PKG).out
BUILD_OUT=$(TOP)/build.out

# requires gnroff open source package be installed
# OSr5 system only
EQN="/usr/local/bin/eqn"
TBL=/usr/local/bin/tbl
NROFF="/usr/local/bin/nroff"

files = $(ROOT)/Makefile

all: clean-logs chkvars configure build install package

chkvars:
	@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 uninstall)

install: install-objs install-cat

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

install-cat: chkvars
	@echo "Creating man page cat files..." | tee -a $(BUILD_OUT)
	@for i in 1 3 5 7; do \
		[ -d $(DESTDIR)/usr/man/cat.$$i ] || \
			mkdir -p $(DESTDIR)/usr/man/cat.$$i; \
		for f in $(DESTDIR)/usr/local/$(PKG)/man/man$$i/*; do \
			catfile=`basename $$f`; \
			echo $(DESTDIR)/usr/man/cat.$$i/$$catfile; \
			cat $$f | $(TBL) | $(EQN) -Tascii | $(NROFF) -man > \
				$(DESTDIR)/usr/man/cat.$$i/$$catfile; \
		done \
	done >> $(BUILD_OUT)

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

configure: chkvars
	@echo "Rerunning configure operations..." | tee -a $(BUILD_OUT)
	@(cd $(ROOT); sh ./config shared --install_prefix=$(DESTDIR)) >> $(BUILD_OUT) 2>&1

$(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)

$(PKGDIR)/$(PKGINST)/prototype: $(PKGDIR)/$(PKGINST)/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)

$(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)/prototype $(PKGDIR)/$(PKGINST)/pkginfo $(PKGDIR)/$(PKGINST)doc/pkginfo $(PKGDIR)/$(PKGINST)doc/prototype $(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 clean) || /bin/true


clean-config: chkvars
	@echo "Removing configure operations..." | tee -a $(BUILD_OUT)
	@for f in $(files); do \
		echo "f: $$f"; \
		[ -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
