# Makefile for up2us
# $Id: Makefile,v 1.13 2003/04/08 20:42:54 tomj Exp tomj $
#
# This Makefile controls the entire build process for up2us.
# It extracts the version/release/arch data from the main up2us
# module which is exported the program and rpm build.
#
# 
# make			builds the source TAR file only
# make RPM		above plus builds RPM
# make install		above plus installs RPM locally
# make www		copies .rpm files to www dir and
# 			rsync's them to wps.com.
# 
# --------------------------------------------------------------
# Note that := causes the expression to be evaluated before assignment;
# the normal = assignment defers evaluation until referenced, causing
# havok when a variable used as a target, eg $(TARGET):.

# CROCK: the name of the module 'up2us' hardcoded here, I got work to do
# and using $(PROG) makes it loop forever...?
PKG:=	  $(shell egrep "^[[:space:]]PKG" up2us     | perl -e '<> =~ /"(.*)"/; print $$1')
VERSION:= $(shell egrep "^[[:space:]]VERSION" up2us | perl -e '<> =~ /"(.*)"/; print $$1')
RELEASE:= $(shell egrep "^[[:space:]]RELEASE" up2us | perl -e '<> =~ /"(.*)"/; print $$1')
ARCH:=	  $(shell egrep "^[[:space:]]ARCH" up2us    | perl -e '<> =~ /"(.*)"/; print $$1')

# --------------------------------------------------------------

# Source TAR target file. Note that it doesn't contain 
# RELEASE.
TARGET=		$(PKG)-$(VERSION).tar.gz

# Make creates these files; they contain version-specific
# data used by the build process.
VERSIONFILE=	version

# Where files go (for RPM making).
WHERE=		/usr/src/redhat
SOURCES =	$(WHERE)/SOURCES
RPMS =		$(WHERE)/RPMS
BUILD =		$(WHERE)/BUILD
SPECS =		$(WHERE)/SPECS
SRPMS =		$(WHERE)/SRPMS

# Files and dependencies.
# NOTE: Version.pm is created by this Makefile, and
# contains program version info.
PROG= up2us
MODULES= Header.pm cmdline.pm config.pm dns.pm execute.pm
FILES=	README $(PROG) $(MODULES) \
	up2us.pam up2us.security \
	up2us.1
DEPS=	Makefile up2us.spec $(FILES) $(TARGET)

# Junk needed for updating the website.
ACCT= tomjennings@up2us.sourceforge.net
WDIR= /home/groups/u/up/up2us/htdocs
WFILES= Makefile up2us.spec index.html up2us.css
TEMPDIR= /tmp/up2us.make.www.temp

# - - - - - - - - - - - - - - - - - - - -
#
all:
	echo "target=$(TARGET)"
	echo "pkg=$(PKG) version=$(VERSION) release=$(RELEASE) arch=$(ARCH)"
	make clean
	make $(TARGET)

# MAKE THIS. rpm will invoke make for the rest.
RPM:
	@make $(TARGET)
	rpm --sign -ba up2us.spec

# We create a subdir with a package-name convention,
# and put all the sources there, and tar it up.
$(TARGET):
	@echo "Building source tree in directory $(PKG)-$(VERSION)"
	@rm -rf $(PKG)-$(VERSION)
	@mkdir -p $(PKG)-$(VERSION)
	echo "$(VERSION) $(RELEASE)" > $(PKG)-$(VERSION)/$(VERSIONFILE)
	@cp -p $(FILES) $(PKG)-$(VERSION)
	@echo "Making $(TARGET)"
	tar -vzcf $(TARGET) $(PKG)-$(VERSION)
	@echo "Copying $(TARGET) and '$(VERSIONFILE)' to RPM source directory"
	@cp  $(TARGET) $(SOURCES)
	@cp  $(PKG)-$(VERSION)/$(VERSIONFILE) $(SOURCES)

clean:
	@echo "Deleting $(TARGET)"
	@rm -f $(TARGET)
	@echo "Deleting source tree directory $(PKG)-$(VERSION)"
	@rm -rf $(PKG)-$(VERSION)
	@echo "Deleting files in $(SOURCES)"
	@rm -f $(SOURCES)/*
	@echo "Deleting $(VERSIONFILE)"
	@rm -f $(VERSIONFILE)

# TESTING TESTING TESTING TESTING TESTING TESTING TESTING
# Make the TAR, rpm, and install it.
install:	
	@make
	@make RPM
	@rpm -iv --force $(RPMS)/$(ARCH)/$(PKG)-$(VERSION)-$(RELEASE).$(ARCH).rpm
# TESTING TESTING TESTING TESTING TESTING TESTING TESTING

# Copy to the web page at sourceforge. We assemble all the files
# in a temp dir first.
www:
	rm -rf $(TEMPDIR)
	mkdir $(TEMPDIR)
	cp $(RPMS)/$(ARCH)/$(PKG)-$(VERSION)-$(RELEASE).$(ARCH).rpm \
	    $(SRPMS)/$(PKG)-$(VERSION)-$(RELEASE).src.rpm \
	    $(TARGET) $(WFILES) $(PKG)-$(VERSION)/* $(TEMPDIR)
	chmod o+w $(TEMPDIR)/*
	rsync -vuazx -e ssh $(TEMPDIR)/* $(ACCT):$(WDIR)
	rm -rf $(TEMPDIR)


