#========================================================================
# THIS IS A "GENERIC" MAKEFILE FOR PERL PROGRAMS
#
# make tar      makes $(TAR) from >>all<< the sources
# make tgz      makes $(TGZ) from >>all<< the sources
# make rpm      makes $(RPM). This is experimental!  See below.
# make svn      does a svn commit and creates the timestamp $(SVNTIME)
# make sync     does a svn commit and rsyncs $(SVNPATH) to host list
# make clean    deletes the application and all object files
# make install  strips and installs application and a man page
# make installweb installs a "standard distribution" set of files on
#               your personal website with rsync.
# make printout prints out all source and include files
#========================================================================
# This should be the name of the binary produced by this package.  The
# directory may or may not be the same:
PROGRAM = amazon_sales_rank
# DIR = $(PROGRAM)
DIR = asr-tools

# The destination on a remote webserver, used as:
#    $(HOME)/public_html/$(WDIR)
# on that server, and the name of an ssh-accessible login server
# that contains the public_html path.
WLOGIN = ganesh.phy.duke.edu
WDIR = General

SVNTREE = $(HOME)/Src/svn-tree
SVNPATH = $(SVNTREE)/$(DIR)
SVNTIME = $(DIR:=.svn.time)

VERSION_MAJOR=0
VERSION_MINOR=1
RELEASE=0


#========================================================================
# Tree point to install (Choose one or set your own)
#========================================================================
# RPM's into /usr, please!
PREFIX=/usr
# Alternatives for public tarball build or private copy
# PREFIX=/usr/local
# PREFIX=$(HOME)
# standard sources and targets.
#========================================================================
TAR = $(DIR).tar
TGZ = $(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE).tgz
ARCH=noarch
BASE_ARCH=`uname -i`
RPM = $(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR)-$(RELEASE).$(ARCH).rpm
SRPM = $(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR)-$(RELEASE).src.rpm
ABS = $(DIR:=.abs)
PHP = $(DIR:=.php)
SPEC = $(DIR:=.spec)

all:

$(TGZ):	tgz
$(RPM): rpm

#========================================================================
# This is a complicated target, but VERY USEFUL.  It makes a .tgz tarball
# of the exact form preferred for an RPM.  Eventually I'll add a sed script
# that automatically fixes the accompanying rpm spec file to correspond
# right before building the tgz to really automate this, so that I can just
# move the spec to SPEC, move the source tgz to SOURCE and do an
# rpm -ba blah.spec.
#========================================================================
tgz:	Makefile $(SPEC) $(PROGRAM)
	rm -f core $(TGZ)
	rm -f .$(DIR)-$(VERSION).$(RELEASE)
	# Update the specfile here, because we want the packed one to be 
	# in sync with the VERSION/RELEASE defined ABOVE (only)!
	# Might need another line or two here, e.g. %Source:
	cat $(SPEC) | \
	sed -e 's/^\(Version:\) \(.*\)/\1 $(VERSION_MAJOR).$(VERSION_MINOR)/' \
	    -e 's/^\(Release:\) \(.*\)/\1 $(RELEASE)/' >> $(SPEC).$$
	mv $(SPEC).$$ $(SPEC)
	mkdir -p .$(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE)
	cp -a * .$(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE)
	mv .$(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE) $(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE)
	# Exclude any cruft/development directories and CVS stuff.  Add
	# lines as needed.
	tar -cvpf $(TAR) \
            --exclude=.svn \
            --exclude=CRUFT \
            --exclude=$(TAR) \
            --exclude=$(TGZ) \
            --exclude=*.rpm \
            ./$(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE)
	gzip $(TAR)
	mv $(TAR).gz $(TGZ)
	rm -rf $(DIR)-$(VERSION_MAJOR).$(VERSION_MINOR).$(RELEASE)

#========================================================================
# AMAZING!  INCREDIBLE!  BUILT IN "make rpm" SUPPORT!
# So much to break, but so much mindless work saved.  Oh, well.
#
# For a root build/install, use
#
# RPM_TOPDIR=/usr/src/redhat
# 
# Otherwise Add the following:
#
# %_topdir	/home/rgb/Src/rpm_tree
#
# to your personal $(HOME)/.rpmmacros after building
# yourself a private copy of the /usr/src/redhat directory structure.
# Change the "rgb" to your OWN home directory and "Src" to your source
# directory, of course.
RPM_TOPDIR=$(HOME)/Src/rpm_tree
#========================================================================
rpm:	Makefile $(SPEC) $(SOURCES) $(SCRIPTS) $(TGZ)
	cp $(TGZ) $(RPM_TOPDIR)/SOURCES
	cp $(SPEC) $(RPM_TOPDIR)/SPECS
	rpmbuild -ba --target $(ARCH) $(RPM_TOPDIR)/SPECS/$(SPEC)
	cp $(RPM_TOPDIR)/RPMS/$(ARCH)/$(RPM) .
	cp $(RPM_TOPDIR)/SRPMS/$(SRPM) .

#========================================================================
# These are useful subversion targets for my own personal svn-tree
# maintenance scheme, whereby I keep a local svn-tree at home and on
# laptop but ALSO keep what should be a perfect mirror at Duke where it
# is properly backed up.  
#========================================================================
TAG = `echo v$(VERSION_MAJOR).$(VERSION_MINOR) r$(RELEASE) | sed -e "s/\./_/"`
tag:
	# force a commit and increment revision of $(SVN), at least
	echo "New Checkin `date`" > $(SVNTIME)
	svn commit .
	svn tag $(TAG)

svn:
	echo "New Checkin `date`" >> $(SVNTIME)	# Will force a commit and increment revision
	svn commit .
	cat $(SVNTIME) | \
	sed -e '/^New Checkin/d' >> $(SVNTIME).tmp
	mv $(SVNTIME).tmp $(SVNTIME)

sync:
	echo "New Checkin `date`" >> $(SVNTIME)	# Will force a commit and increment revision
	svn commit .		# Do the commit
	rsync -avz --delete $(SVNPATH) ganesh.phy.duke.edu:/home/einstein/prof/rgb/Src/svn-tree
	rsync -avz --delete $(SVNPATH) uriel:$(SVNTREE)
	cat $(SVNTIME) | \
	sed -e '/^New Checkin/d' >> $(SVNTIME).tmp
	mv $(SVNTIME).tmp $(SVNTIME)

clean:
	rm -f $(PROGRAM).1.gz

#========================================================================
# printout makes an enscript -2r printout of SOURCES and
# and INCFILES.  Use lpr if you don't have enscript
#========================================================================
LPR = enscript -2r
# LPR = lpr
printout:
	$(LPR) $(PROGRAM)

#========================================================================
# Generic Rule to install.  Note that I presume that ALL applications
# have a man page for documentation!  They'd better!
#========================================================================
install :
	(install -d $(PREFIX)/bin; \
	install -d $(PREFIX)/share/man/man1; \
        gzip -c $(PROGRAM).1 > $(PROGRAM).1.gz; \
        install -m 644 $(PROGRAM) $(PREFIX)/bin; \
        install -m 644 $(PROGRAM).1.gz $(PREFIX)/share/man/man1)

installweb : $(TGZ) $(RPM) $(SRPM)
	(mkdir $(DIR);\
	rsync -avz $(DIR) $(WLOGIN):public_html/$(WDIR)/; \
	rsync -avz $(TGZ) $(WLOGIN):public_html/$(WDIR)/$(DIR)/; \
	rsync -avz $(RPM) $(WLOGIN):public_html/$(WDIR)/$(DIR)/; \
	rsync -avz $(SRPM) $(WLOGIN):public_html/$(WDIR)/$(DIR)/; \
	rsync -avz $(ABS) $(WLOGIN):public_html/$(WDIR)/$(DIR)/; \
	rsync -avz $(PHP) $(WLOGIN):public_html/$(WDIR)/; \
	rmdir $(DIR))

