#
# Makefile for openclipart packages
#
# Copyright 2005, Jon Phillips.
#
# It is quite basic. But getting more complex by the day.
#
# To do a basic installation:
#	make install
#
# To do a basic packaging build:
# 	make dist
# 
# Then get your packages out the folder, packages
#
# Read through below for specific directives on how to build different versions
#
# To make the windows installer, make sure you have nsis installed. I set it
# up because it's a nonstandard install, to /opt/nsis, which is a symlink to
# the full path, which is like /opt/nsis-2.08. So, make a symlink to yours in
# that location, or just do:
#
# 	MAKENSIS=/PATH/TO/YOUR/makensis make nsis
#
# I did not add MAKENSIS into the normal make dist, because it has a little
# more quirkiness.
#
# TODO:
# * Fix RPM stuff. I removed from make dist for now.
#
#
RELEASE_NUM = $(shell cat VERSION)
APPNAME = openclipart
FULLNAME= Open Clip Art Library
SRCDIR = clipart
TOOLSDIR= tools
PACKAGEDIR=packages

PACK_FULL_DIR=$(APPNAME)-$(RELEASE_NUM)-full
PACK_SVG_DIR=$(APPNAME)-$(RELEASE_NUM)-svgonly
PACK_TOOLS_DIR=$(APPNAME)-$(RELEASE_NUM)-tools

PACK_PREP_FULL_DIR=$(PACKAGEDIR)/$(PACK_FULL_DIR)
PACK_PREP_SVG_DIR=$(PACKAGEDIR)/$(PACK_SVG_DIR)
PACK_PREP_TOOLS_DIR=$(PACKAGEDIR)/$(PACK_TOOLS_DIR)

NSISDIR=nsis
NSIS_SCRIPT = build.nsi

INSTALL = /bin/install -c
MAKENSIS = /opt/nsis/makensis

PREFIX = /usr/local
EXEC_PREFIX = $(PREFIX)
BINDIR=$(PREFIX)/bin
DATADIR=$(PREFIX)/share
CLIPARTDIR=$(DATADIR)/clipart
# Do we really want the release number here? I don't think so.
#APPDIR=$(CLIPARTDIR)/$(APPNAME)-$(RELEASE_NUM)
APPDIR=$(CLIPARTDIR)/$(APPNAME)
MANDIR=$(DATADIR)/man
MAN1DIR=$(MANDIR)/man1
DESTDIR=

# List of author credit files
AUTHORS_FILES=AUTHORS.in

# List of files to be included in distributions
DIST_FILES=AUTHORS \
	ChangeLog \
	LICENSE \
	NEWS \
	README \
	VERSION \
	INSTALL \
	Makefile \
	$(APPNAME).spec \
	$(APPNAME).spec.in \
	$(AUTHORS_FILES)

# Tools directory needs a major cleanup and possibly its own Makefile
# before it's ready to be distributed
TOOLS_FILES=AUTHORS \
	VERSION \
	LICENSE

# DIST_FILES=`find $(SRCDIR) -name "*"`

# The following files are autogenerated and should not be in CVS
# AUTHORS
# nist/build.nsi
# openclipart.spec

all: AUTHORS versions

# Create release directories ready for packaging
distprep: all

	# create package directories
	rm -rf $(PACK_PREP_FULL_DIR) $(PACK_PREP_SVG_DIR) $(PACK_PREP_TOOLS_DIR)
	install -m 755 -d $(PACK_PREP_FULL_DIR)
	install -m 755 -d $(PACK_PREP_SVG_DIR)
	install -m 755 -d $(PACK_PREP_TOOLS_DIR)
	
	# copy data into packaging area
	cp -Rfp $(SRCDIR) $(PACK_PREP_FULL_DIR)
	cp -Rfp $(SRCDIR) $(PACK_PREP_SVG_DIR)
	cp -Rfp $(TOOLSDIR)/* $(PACK_PREP_TOOLS_DIR)
	
	# Copy in all dist and package files to both
	install -m 644 $(DIST_FILES) $(PACK_PREP_FULL_DIR)
	install -m 644 $(DIST_FILES) $(PACK_PREP_SVG_DIR)
	install -m 644 $(TOOLS_FILES) $(PACK_PREP_TOOLS_DIR)

	# Copy in nsis files to both
	cp -Rfp $(NSISDIR) $(PACK_PREP_FULL_DIR)
	cp -Rfp $(NSISDIR) $(PACK_PREP_SVG_DIR)

	# Get rid of all CVS folders in the packaging area
	find $(PACK_PREP_FULL_DIR) $(PACK_PREP_SVG_DIR) $(PACK_PREP_TOOLS_DIR) \
		-depth -name CVS -type d -exec rm -rf {} \;

	# Get rid of non-svg files from SVG directory
	find $(PACK_PREP_SVG_DIR)/$(SRCDIR) \( -name '*.*' -not -name '*svg' \)\
		-type f -exec rm -f {} \;

install: all
	$(INSTALL) -m 755 -d $(DESTDIR)$(APPDIR)
	cd $(SRCDIR); \
	for d in `find . -type d -not -name CVS -print`; \
	do \
		$(INSTALL) -m 755 -d $(DESTDIR)$(APPDIR)/$$d; \
		for f in `find $$d -maxdepth 1 -type f -print`; \
		do \
			$(INSTALL) -m 644 $$f $(DESTDIR)$(APPDIR)/$$d; \
		done; \
	done
	$(INSTALL) -m 644 $(DIST_FILES) $(DESTDIR)$(APPDIR)
	# Install manpage if someone writes one
	#$(INSTALL) -m 755 -d $(DESTDIR)$(MAN1DIR)
	#$(INSTALL) -m 644 $(APPNAME).1 $(DESTDIR)$(MAN1DIR)

uninstall:
	rm -rf $(DESTDIR)$(APPDIR)
	# rm -f $(DESTDIR)$(MAN1DIR)/$(APPNAME).1

zip: distprep
	(cd $(PACKAGEDIR); zip -r $(PACK_FULL_DIR).zip $(PACK_FULL_DIR))

tarball: distprep
	tar -C $(PACKAGEDIR) -czf $(PACK_PREP_FULL_DIR).tar.gz $(PACK_FULL_DIR)

bzip: distprep
	tar -C $(PACKAGEDIR) -cjf $(PACK_PREP_FULL_DIR).tar.bz2 $(PACK_FULL_DIR)

zip-svgonly: distprep
	(cd $(PACKAGEDIR); zip -r $(PACK_SVG_DIR).zip $(PACK_SVG_DIR))

tarball-svgonly: distprep
	tar -C $(PACKAGEDIR) -czf $(PACK_PREP_SVG_DIR).tar.gz $(PACK_SVG_DIR)

bzip-svgonly: distprep
	tar -C $(PACKAGEDIR) -cjf $(PACK_PREP_SVG_DIR).tar.bz2 $(PACK_SVG_DIR)

zip-tools: distprep
	(cd $(PACKAGEDIR); zip -r $(PACK_TOOL_DIR).zip $(PACK_TOOLS_DIR))

tarball-tools: distprep
	tar -C $(PACKAGEDIR) -czf $(PACK_PREP_TOOLS_DIR).tar.gz $(PACK_TOOLS_DIR)
	
bzip-tools: distprep
	tar -C $(PACKAGEDIR) -cjf $(PACK_PREP_TOOLS_DIR).tar.bz2 $(PACK_TOOLS_DIR)

rpm:	bzip
	rpmbuild --define "_rpmdir `pwd`" \
		 --define '_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' \
		 -tb $(PACK_PREP_FULL_DIR).tar.bz2
	mv *.rpm $(PACKAGEDIR)

srpm:	bzip
	rpmbuild --define "_srcrpmdir `pwd`" \
		 --define '_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' \
		 -ts $(PACK_PREP_FULL_DIR).tar.bz2
	mv *.rpm $(PACKAGEDIR)

rpms:	bzip
	rpmbuild --define "_rpmdir `pwd`" \
		 --define "_srcrpmdir `pwd`" \
		 --define '_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' \
		 -ta $(PACK_PREP_FULL_DIR).tar.bz2
	mv *.rpm $(PACKAGEDIR)

nsis: distprep
	$(NSISDIR)/bin/buildtranslations.plx $(NSISDIR)/$(NSIS_SCRIPT) \
		$(NSISDIR)/
	$(MAKENSIS) $(NSISDIR)/$(NSIS_SCRIPT)

dist: all zip tarball bzip \
	zip-svgonly tarball-svgonly bzip-svgonly \
	zip-tools tarball-tools bzip-tools

test:
	@echo "RELEASE_NUM: $(RELEASE_NUM)"
	@echo "APPNAME: $(APPNAME)"
	@echo "SRCDIR: $(SRCDIR)"
	@echo "TOOLSDIR: $(TOOLSDIR)"
	@echo "PACKAGEDIR: $(PACKAGEDIR)"
	@echo "PACK_FULL_DIR: $(PACK_FULL_DIR)"
	@echo "PACK_SVG_DIR: $(PACK_SVG_DIR)"
	@echo "PACK_TOOLS_DIR: $(PACK_TOOLS_DIR)"
	@echo "PACK_PREP_FULL_DIR: $(PACK_PREP_FULL_DIR)"
	@echo "PACK_PREP_SVG_DIR: $(PACK_PREP_SVG_DIR)"
	@echo "PACK_PREP_TOOLS_DIR: $(PACK_PREP_TOOLS_DIR)"
	
	@echo "DATADIR: $(DATADIR)"
	@echo "CLIPARTDIR: $(CLIPARTDIR)"
	@echo "APPDIR: $(APPDIR)"
	@echo "MANDIR: $(MANDIR)"
	@echo "MAN1DIR: $(MAN1DIR)"
	@echo "INSTALL: $(INSTALL)"

clean:
	rm -f AUTHORS
	rm -f $(NSISDIR)/$(NSIS_SCRIPT)
	rm -Rf $(NSISDIR)/translations $(NSISDIR)/includes
	rm -f $(APPNAME).spec
	rm -f $(APPNAME).1

distclean: clean
	rm -rf $(PACKAGEDIR)

# ready for adding artists' names generated from the clipart/ processes
# just cat any other AUTHORS files to get this sorted list
AUTHORS: $(AUTHORS_FILES)
	cat $(AUTHORS_FILES) | sort -u > AUTHORS

# embed version number where needed
versions: $(NSISDIR)/$(NSIS_SCRIPT) $(APPNAME).spec

# embed version number in the NSIS build script input file
# also replace some constants that are hard to define in nsis 
$(NSISDIR)/$(NSIS_SCRIPT): $(NSISDIR)/$(NSIS_SCRIPT).in VERSION
	sed -e "s/APP_VERSION/$(RELEASE_NUM)/" \
		$(NSISDIR)/$(NSIS_SCRIPT).in > $(NSISDIR)/$(NSIS_SCRIPT).tmp
	
	sed -e "s/FULL_NAME/$(FULLNAME)/" \
		$(NSISDIR)/$(NSIS_SCRIPT).tmp > $(NSISDIR)/$(NSIS_SCRIPT).tmp2
	
	sed -e "s/SHORT_NAME/$(APPNAME)/" \
		$(NSISDIR)/$(NSIS_SCRIPT).tmp2 > $(NSISDIR)/$(NSIS_SCRIPT).tmp3

	sed -e "s/NSISDIR/$(NSISDIR)/" \
		$(NSISDIR)/$(NSIS_SCRIPT).tmp3 > $(NSISDIR)/$(NSIS_SCRIPT)

	# clean up tmp files
	rm -Rf $(NSISDIR)/$(NSIS_SCRIPT).tmp*

# embed version number in the spec file
$(APPNAME).spec: $(APPNAME).spec.in VERSION
	sed -e "s/APP_VERSION/$(RELEASE_NUM)/" \
		$(APPNAME).spec.in > $(APPNAME).spec

