#!/bin/bash

# ----------------------------------------------------------------------
# This is a makefile for creating the various nap distributions (linux
# binary, alpha binary, Redhat rpm, etc) from the "master" source
# distribution. It is deliberately formulated to be independent of the
# main Makefile, so that it is effectively a standalone program. This
# way, we can easily re-create distributions of older versions without
# having to untar them manually.
#
# Usage:
# Makedist        (to build all available distributions)
# Makedist target (where target = sourcerpm, binaryrpm, binarydist, ...)
# Makedist clean  (to remove all created files except web site)
# Makedist www    (to make web site; also requires index.html.in and UPDATE.in)
#
# Author: Peter Selinger <selinger@users.sourceforge.net>
# Date: April 17, 2001. Updated: April 24, 2001
#
# Permission is granted to copy, modify, and distribute this file
# under the terms of the GNU General Public License (GPL) version 2.
# See www.gnu.org for details.

# ----------------------------------------------------------------------
# This is a self-executing makefile: the following line enables typing
# "Makedist targets" instead of "make -f Makedist targets", as it can
# be interpreted both by "make" and by "bash". Note: this makefile uses
# features of GNU make, such as ':=' assignments.

AUTOSTART=AUTOSTART make -f $0 $*; exit

# ----------------------------------------------------------------------
# Determine the version of nap to build. We look for source distributions
# in the current working directory and in its parent directory. We
# prompt the user for which distribution to build from

SHELL=/bin/bash

DISTS:=$(shell ls -1 . .. | egrep "nap-.*\.tar\.gz" | grep -v linux | sort -r)

ifeq "$(DISTS)" ""
  FOO:=$(shell echo No distributions found >&2)
else
  ifndef SOURCEDIST
    SOURCEDIST:=$(shell PS3="Select a distribution: "; \
		       select dist in $(DISTS); do break; done >&2; \
	               echo $$dist)
  endif

# endif is at the end of the file (AAA)

VERSION:=$(shell echo $(SOURCEDIST) | sed -e 's/nap-\(.*\)\.tar\.gz/\1/g')

# ----------------------------------------------------------------------
# the "master" distribution could either be in the current working
# directory, or in its parent directory. We need to know which is the
# case, so we test it here.

SOURCEDISTPATH:=$(SOURCEDIST)

FOUND:=$(shell test -f $(SOURCEDISTPATH) && echo yes)
ifeq "$(FOUND)" ""
  SOURCEDISTPATH:=../$(SOURCEDIST)
endif

FOO:=$(shell echo "Using $(SOURCEDISTPATH)" >&2)

# ----------------------------------------------------------------------
# all the distributions we can make on this particular machine.

OS:=$(shell uname -s | sed -e "s:Linux*:linux:")
MACH:=$(shell uname -m | sed -e "s:i.86*:i386:")

# we can make a binary distribution on any machine, for that machine
ALL:=binarydist

# in addition, we can make RPM's on linux
ifeq "$(OS)" "linux"
  ALL+=sourcerpm binaryrpm
endif

all: $(ALL)

# ----------------------------------------------------------------------
# a list of extra files to include with binary distributions. This
# list contains only "documentation" style files, not things that need
# to get installed in particular places. It is also used in rpm.spec.in.

EXTRADISTFILES := AUTHORS ChangeLog COPYING COPYRIGHT INSTALL NEWS README
EXTRADISTFILES += README.win README.ncurses doc/userguide.html doc/napster.txt
EXTRADISTFILES += doc/nap.1

# ----------------------------------------------------------------------
# the following targets extract and build the files from the "master"
# distribution. Note: we now make the binary distribution statically
# linked, to avoid dependencies on specific versions of shared libraries.

SOURCEDIR:=nap-$(VERSION)

extract: $(SOURCEDIR)

$(SOURCEDIR):
	rm -rf $(SOURCEDIR)
	tar zxf $(SOURCEDISTPATH)

build: $(SOURCEDIR)/build

$(SOURCEDIR)/build: $(SOURCEDIR)
	cd $(SOURCEDIR); ./configure
	rm -f $(SOURCEDIR)/nap
	rm -f $(SOURCEDIR)/napping
	cd $(SOURCEDIR); make LDADD=-static
	strip $(SOURCEDIR)/src/nap
	strip $(SOURCEDIR)/src/napping
	touch $(SOURCEDIR)/build

# ----------------------------------------------------------------------
# following are the targets for each individual distribution

# ----------------------------------------------------------------------
# binarydist: the binary distribution for this machine

BINARYDIST := nap-$(VERSION).$(OS)-$(MACH).tar.gz
BINARYDIR := nap-$(VERSION).$(OS)-$(MACH)

binarydist: $(BINARYDIST)

$(BINARYDIST): $(SOURCEDIR)/build
	rm -rf $(BINARYDIR)
	mkdir $(BINARYDIR)
	for x in src/nap src/napping $(EXTRADISTFILES); do \
	  cp $(SOURCEDIR)/$$x $(BINARYDIR); \
	done
	chmod -R a+rX $(BINARYDIR)
	chmod u+s $(BINARYDIR)/napping
	tar zcf $(BINARYDIST) $(BINARYDIR)

# ----------------------------------------------------------------------
# sourcerpm and binaryrpm: the Redhat Package Manager targets.  Note
# that these (like everything else in this file) are formulated in
# such a way that they do not require root access.

RPMVERSION:=$(VERSION)
RPMRELEASE:=1

SOURCERPM:=nap-$(RPMVERSION)-$(RPMRELEASE).src.rpm
BINARYRPM:=nap-$(RPMVERSION)-$(RPMRELEASE).$(MACH).rpm

RPMDIR:=RPM.$(RPMVERSION)-$(RPMRELEASE)

RPMSUB:= -e s:@SOURCEDIR@:'$(SOURCEDIR)':g
RPMSUB+= -e s:@SOURCEDIST@:'$(SOURCEDIST)':g
RPMSUB+= -e s:@RPMVERSION@:'$(RPMVERSION)':g
RPMSUB+= -e s:@RPMRELEASE@:'$(RPMRELEASE)':g
RPMSUB+= -e s:@EXTRADISTFILES@:'$(EXTRADISTFILES)':g

rpms: sourcerpm binaryrpm

sourcerpm: $(SOURCERPM)

$(SOURCERPM): $(RPMDIR)/build
	cp $(RPMDIR)/SRPMS/$(SOURCERPM) .

binaryrpm: $(BINARYRPM)

$(BINARYRPM): $(RPMDIR)/build
	cp $(RPMDIR)/RPMS/$(MACH)/$(BINARYRPM) .

$(RPMDIR)/build: $(RPMDIR)
	rpm --define "_topdir $(PWD)/$(RPMDIR)" \
		--buildroot $(PWD)/$(RPMDIR)/BUILDROOT \
		-ba $(RPMDIR)/SPECS/$(SOURCEDIR).spec
	touch $(RPMDIR)/build



rpmdir: $(RPMDIR)

$(RPMDIR): $(SOURCEDISTPATH) $(SOURCEDIR)
	rm -rf $(RPMDIR)
	mkdir $(RPMDIR) $(RPMDIR)/BUILD $(RPMDIR)/BUILDROOT $(RPMDIR)/RPMS \
		$(RPMDIR)/SOURCES $(RPMDIR)/SPECS $(RPMDIR)/SRPMS
	cp $(SOURCEDISTPATH) $(RPMDIR)/SOURCES
	sed $(RPMSUB) $(SOURCEDIR)/rpm.spec.in \
		> $(RPMDIR)/SPECS/$(SOURCEDIR).spec
	@A=`cat $(RPMDIR)/SPECS/$(SOURCEDIR).spec | egrep '@[^.]*@'`; \
            test ! "$$A" || \
	    (echo Substitution failure for $(RPMDIR)/SPECS/$(SOURCEDIR).spec:;\
	    echo $$A ; rm -f $(RPMDIR)/SPECS/$(SOURCEDIR).spec; false)

# ----------------------------------------------------------------------
# the next targets are for making the web page. They should normally
# be used with the most current version, since index.html.in and
# UPDATE.in are not included in the source distribution and thus are
# not going to revert to an old state. The following declarations only
# work on a linux-i386 and would have to be adapted to work on some
# other machine.

test:
	echo $(OS)-$(MACH)

ifeq "$(OS)-$(MACH)" "linux-i386"

# a bit of acrobatics since we want to avoid prompting for the alpha
# and openbsd distributions unless the www target was selected.
ifneq "$(MAKEWWW)" "yes"
www:
	make -f Makedist MAKEWWW=yes SOURCEDIST=$(SOURCEDIST) www
wwwindex:
	make -f Makedist MAKEWWW=yes SOURCEDIST=$(SOURCEDIST) wwwindex
index:
	make -f Makedist MAKEWWW=yes SOURCEDIST=$(SOURCEDIST) index
UPDATE:
	make -f Makedist MAKEWWW=yes SOURCEDIST=$(SOURCEDIST) UPDATE
else

# determine which Alpha and OpenBSD distribution to use, since the
# current versions are not always available at the same time a release is made

ALPHAS:=$(shell ls -1 | egrep "nap-.*\.linux-alpha\.tar\.gz" | sort -r)
ifeq "$(ALPHAS)" ""
  FOO:=$(shell echo No Alpha distribution found >&2)
else
  ALPHADIST:=$(shell PS3="Select an Alpha distribution: "; \
		     select dist in $(ALPHAS); do break; done >&2; \
		     echo $$dist)
endif

SPARCS:=$(shell ls -1 | egrep "nap-.*\.linux-sparc64\.tar\.gz" | sort -r)
ifeq "$(SPARCS)" ""
  FOO:=$(shell echo No Sparc distribution found >&2)
else
  SPARCDIST:=$(shell PS3="Select a Sparc distribution: "; \
		     select dist in $(SPARCS); do break; done >&2; \
		     echo $$dist)
endif

PPCS:=$(shell ls -1 | egrep "nap-.*\.linux-ppc\.tar\.gz" | sort -r)
ifeq "$(PPCS)" ""
  FOO:=$(shell echo No Ppc distribution found >&2)
else
  PPCDIST:=$(shell PS3="Select a PowerPC distribution: "; \
		     select dist in $(PPCS); do break; done >&2; \
		     echo $$dist)
endif

FREEBSDS:=$(shell ls -1 | egrep "nap-.*\.freebsd-.*\.tar\.gz" | sort -r)
ifeq "$(FREEBSDS)" ""
  FOO:=$(shell echo No FreeBSD distribution found >&2)
else
  FREEBSDDIST:=$(shell PS3="Select a FreeBSD distribution: "; \
		     select dist in $(FREEBSDS); do break; done >&2; \
		     echo $$dist)
endif

OPENBSDS:=$(shell ls -1 | egrep "nap-.*\.tgz" | sort -r)
ifeq "$(OPENBSDS)" ""
  FOO:=$(shell echo No OpenBSD distribution found >&2)
else
  OPENBSDDIST:=$(shell PS3="Select an OpenBSD distribution: "; \
		     select dist in $(OPENBSDS); do break; done >&2; \
		     echo $$dist)
endif

DEBIANS:=$(shell ls -1 | egrep "nap_.*_i386.deb" | sort -r)
ifeq "$(DEBIANS)" ""
  FOO:=$(shell echo No Debian distribution found >&2)
else
  DEBIANDIST:=$(shell PS3="Select a Debian distribution: "; \
		     select dist in $(DEBIANS); do break; done >&2; \
		     echo $$dist)
endif

CYGWINS:=$(shell ls -1 | egrep "nap-.*\.cygwin32.zip" | sort -r)
ifeq "$(CYGWINS)" ""
  FOO:=$(shell echo No cygwin distribution found >&2)
else
  CYGWINDIST:=$(shell PS3="Select a cygwin distribution: "; \
		     select dist in $(CYGWINS); do break; done >&2; \
		     echo $$dist)
endif

LINUXDIST := $(BINARYDIST)

# note: we link $(OPENBSDDIST) remotely, but we copy it to the website
# anyway, for archival purposes.

WWWDISTS := $(SOURCEDISTPATH) $(LINUXDIST) $(ALPHADIST) $(FREEBSDDIST) 
WWWDISTS += $(SPARCDIST) $(PPCDIST)
WWWDISTS += $(DEBIANDIST) 
WWWDISTS += $(OPENBSDDIST) $(CYGWINDIST) $(BINARYRPM) $(SOURCERPM)
WWWFILES := README ChangeLog doc/userguide.html AUTHORS
WWWFILES += COPYRIGHT NEWS
WWWHOME := $(HOME)/www/nap

DATE:=$(shell date '+%B %e, %Y')

WWWSUB := -e s:@VERSION@:'$(VERSION)':g
WWWSUB += -e s:@SOURCEDIST@:'$(SOURCEDIST)':g
WWWSUB += -e s:@LINUXDIST@:'$(LINUXDIST)':g
WWWSUB += -e s:@ALPHADIST@:'$(ALPHADIST)':g
WWWSUB += -e s:@SPARCDIST@:'$(SPARCDIST)':g
WWWSUB += -e s:@PPCDIST@:'$(PPCDIST)':g
WWWSUB += -e s:@FREEBSDDIST@:'$(FREEBSDDIST)':g
WWWSUB += -e s:@OPENBSDDIST@:'$(OPENBSDDIST)':g
WWWSUB += -e s:@DEBIANDIST@:'$(DEBIANDIST)':g
WWWSUB += -e s:@CYGWINDIST@:'$(CYGWINDIST)':g
WWWSUB += -e s:@SOURCERPM@:'$(SOURCERPM)':g
WWWSUB += -e s:@BINARYRPM@:'$(BINARYRPM)':g
WWWSUB += -e s:@DATE@:'$(DATE)':g

# note: an indirection here, because we want index.html to be rebuilt
# each time.
local/index.html: index

index: local/index.html.in
	sed $(WWWSUB) < local/index.html.in > local/index.html
	@A=`cat local/index.html | egrep '@[^.]*@'`; test ! "$$A" || \
	    (echo Substitution failure for local/index.html:; \
	    echo $$A ; rm -f local/index.html; false)

# note: an indirection here for the same reason
local/UPDATE: update

update: local/UPDATE.in
	sed $(WWWSUB) < local/UPDATE.in > local/UPDATE
	@A=`cat local/UPDATE | egrep '@[^.]*@'`; test ! "$$A" || \
	    (echo Substitution failure for local/UPDATE:; \
            echo $$A ; rm -f local/UPDATE; false)

www: $(WWWDISTS) index update $(SOURCEDIR)
	cp $(WWWDISTS) $(WWWHOME)
	cp local/index.html $(WWWHOME)/index.html
	cp local/UPDATE $(WWWHOME)/UPDATE
	for x in $(WWWFILES); do \
	  cp $(SOURCEDIR)/$$x $(WWWHOME); \
	done
	chmod -R a+rX $(WWWHOME)

wwwindex: index
	cp local/index.html $(WWWHOME)/index.html
	chmod a+rX $(WWWHOME)/index.html

endif
endif

# ----------------------------------------------------------------------
# last but not least, the cleanup target

clean:
	rm -rf $(SOURCEDIR)
	rm -rf $(BINARYDIR)
	rm -rf $(RPMDIR)
	rm -f index.html
	rm -f UPDATE

# ----------------------------------------------------------------------
# the endif from (AAA) above.

endif # $(DISTS) is empty
