# Project: Jigdo (Jigsaw download)
#  __   _
#  |_) /|  Copyright (C) 2001 Richard Atterer
#  | \/¯|  <richard@atterer.net>
#  ¯ '` ¯
# this Makefile mostly forwards make commands to the other directories
srcdir =	@srcdir@
VPATH =		@srcdir@

prefix =	@prefix@
exec_prefix =	@exec_prefix@
bindir =	@bindir@
mandir =	@mandir@
datadir =	@datadir@

PACKAGE =	jigdo
INSTALL =	@INSTALL@
INSTALL_EXE =	@INSTALL@ -s
INSTALL_DATA =	@INSTALL@ -m 644
AWK =		@AWK@

# echo `sed -n 's/^.*>\(.*\.xpm\)<.*$/\1/p' jigdo.glade|sort|uniq`
icons =		button-cross.xpm button-pause.xpm button-play.xpm \
		jigdo-logo.xpm new.xpm preferences.xpm quit.xpm
catalogs =	@CATALOGS@

.PHONY:		all all_msg clean distclean mostlyclean maintainer-clean \
		dep depend doc cvsdist check install test loc deb debrpm \
		install-jigdo-file install-jigdo-lite install-jigdo \
		install-po
# "gfx" symlink is needed so that the pixmaps are found
all doc mostlyclean dep depend check: Makefile
		-test -e gfx || ln -s "$(srcdir)/gfx" gfx
		cd src && $(MAKE) $@
		cd doc && $(MAKE) $@
		cd po  && $(MAKE) $@
test:
		@echo "You mean \`make check'. Try again... ;-)"
clean:		Makefile
		cd src && $(MAKE) $@
		cd doc && $(MAKE) $@
		cd po  && $(MAKE) $@
		rm -f mirrors.jigdo mirrors.list mirrors.txt
distclean:	Makefile
		cd src && $(MAKE) $@
		cd doc && $(MAKE) $@
		cd po  && $(MAKE) $@
		rm -f mirrors.jigdo mirrors.list mirrors.txt
		rm -f jigdo-*.tar.gz jigdo-*.tar.bz2
		rm -f *~ \#*\# *.bak
		rm -f Makefile config.h config.cache config.log config.status
		rm -f deb/*~ deb/\#*\# deb/*.bak deb/changelog
		rm -f scripts/*~ scripts/\#*\# scripts/*.bak
#		-rm -f debian
maintainer-clean: Makefile
		@echo 'This command is intended for maintainers to use; it'
		@echo 'deletes files that may need special tools to rebuild.'
		cd src && $(MAKE) $@
		cd doc && $(MAKE) $@
		cd po  && $(MAKE) $@
		rm -f mirrors.jigdo mirrors.list mirrors.txt
		rm -f jigdo-*.tar.gz jigdo-*.tar.bz2
		rm -f *~ \#*\# *.bak
		rm -f Makefile config.h config.cache config.log config.status
		rm -f configure
		cd $(srcdir) && autoconf

install:	install-po @INSTALL_TARGETS@
install-jigdo-file:
		$(INSTALL) -d $(DESTDIR)$(bindir)
		$(INSTALL_EXE) src/jigdo-file $(DESTDIR)$(bindir)
		$(INSTALL) -d $(DESTDIR)$(mandir)/man1
		x="doc/jigdo-file.1"; \
		test -f "$$x" || x="$(srcdir)/$$x"; \
		$(INSTALL) "$$x" $(DESTDIR)$(mandir)/man1
install-jigdo-lite:
		$(INSTALL) -d "$(DESTDIR)$(bindir)"
		$(INSTALL) -d "$(DESTDIR)$(datadir)/jigdo"
		$(INSTALL) "$(srcdir)/scripts/jigdo-lite" \
		    "$(DESTDIR)$(bindir)"
		sed -e 's%"\(mirrors.jigdo"\)%"$(datadir)/jigdo/debian-\1%' \
		    <"$(srcdir)/scripts/jigdo-lite" \
		    >"$(DESTDIR)$(bindir)/jigdo-lite"
		chmod 755 "$(DESTDIR)$(bindir)/jigdo-lite"
		$(INSTALL_DATA) "$(srcdir)/scripts/debian-mirrors.jigdo" \
		    "$(DESTDIR)$(datadir)/jigdo"
		$(INSTALL) -d $(DESTDIR)$(mandir)/man1
		x="doc/jigdo-lite.1"; \
		test -f "$$x" || x="$(srcdir)/$$x"; \
		$(INSTALL) "$$x" $(DESTDIR)$(mandir)/man1
install-jigdo:
		$(INSTALL) -d "$(DESTDIR)$(bindir)"
		$(INSTALL_EXE) src/jigdo "$(DESTDIR)$(bindir)"
		$(INSTALL) -d "$(DESTDIR)$(datadir)/jigdo/pixmaps"
		@for x in $(icons); do \
		    echo "$(INSTALL_DATA) \"$(srcdir)/gfx/$$x\"" \
		        "\"$(DESTDIR)$(datadir)/jigdo/pixmaps\""; \
		    $(INSTALL_DATA) "$(srcdir)/gfx/$$x" \
		        "$(DESTDIR)$(datadir)/jigdo/pixmaps" || exit 1; \
		done
		$(INSTALL_DATA) "$(srcdir)/COPYING" \
		    "$(DESTDIR)$(datadir)/jigdo/COPYING"
install-po:
		@test "$(catalogs)" \
                && for file in $(catalogs); do \
		    lang=`echo $$file | sed -e 's/\.gmo$$//'`; \
		    dir="$(DESTDIR)$(datadir)/locale/$$lang/LC_MESSAGES"; \
		    echo "$(INSTALL) -d \"$$dir\""; \
		    $(INSTALL) -d "$$dir"; \
		    s="$(srcdir)/po/$$file"; \
		    if test -f "po/$$file"; then s="po/$$file"; fi; \
		    echo "$(INSTALL_DATA) $$s \"$$dir/$(PACKAGE).mo\""; \
		    $(INSTALL_DATA) "$$s" "$$dir/$(PACKAGE).mo" \
		        || exit 1; \
		done

Makefile:	Makefile.in configure VERSION
		sh config.status # update Makefile from Makefile.in

configure:	configure.in
		cd $(srcdir) && autoconf

# allow "make CXXFLAGS=-O0" while staying compatible with non-GNU-make
.EXPORT_ALL_VARIABLES: ;
#______________________________________________________________________

# Nonstandard targets

# Vanity meter :-)
loc:
		@f=`find $(srcdir)/src -name '*.cc' -o -name '*.[hif]h'` \
		    && printf '%d files, %d lines of code\n' \
		       `echo $$f|wc -w` `cat $$f|wc -l`

# Compile and package unofficial Debian package
deb:
		@if test "`pwd`" != "`cd '$(srcdir)' && pwd`"; then \
		 echo "   * Building .deb package only possible if you are";\
		 echo "   * building in the source dir, i.e. invoked the"; \
		 echo "   * configure script as \`./configure'"; \
		 exit 1; fi
		test -e debian || ln -s deb debian
		dpkg-buildpackage -rfakeroot -us -uc -b

# Convert Debian package into RPM format
debrpm:		deb
		for deb in ../jigdo*.deb; do \
		    fakeroot alien -r "$$deb"; \
		done

# Compile and create a tar.gz containing the tools and documentation
bindist:	Makefile
		@if test -f jigdo-bin-`cat $(srcdir)/VERSION`; then \
		 echo "Object \`jigdo*' already exists - delete it first";\
		 exit 1; \
		fi
		rm -f src/jigdo-file
		cd src && $(MAKE) LD='c++ -static' all-msg jigdo-file strip
		cd doc && $(MAKE) all
		jdir=jigdo-bin-`cat $(srcdir)/VERSION`; \
		    mkdir "$$jdir"; \
		    $(INSTALL) $(srcdir)/scripts/jigdo-lite "$$jdir"; \
		    $(INSTALL) src/jigdo-file "$$jdir"; \
		    $(INSTALL_DATA) $(srcdir)/doc/jigdo-file.html "$$jdir"; \
		    $(INSTALL_DATA) $(srcdir)/doc/jigdo-file.1    "$$jdir"; \
		    $(INSTALL_DATA) $(srcdir)/doc/jigdo-lite.html "$$jdir"; \
		    $(INSTALL_DATA) $(srcdir)/doc/jigdo-lite.1    "$$jdir"; \
		    $(INSTALL_DATA) $(srcdir)/scripts/debian-mirrors.jigdo \
			"$$jdir/mirrors.jigdo"; \
		    cp src/jigdo-file "$$jdir"; \
		    tar -cf "$$jdir.tar" "$$jdir"; \
		    bzip2 -f -k -9 "$$jdir.tar"; \
		    gzip -f -9 "$$jdir.tar"; \
		    rm -rf "$$jdir" src/jigdo-file

# .jigdo file for Debian FTP mirrors - actively checks whether
mirrors.jigdo:	mirrors.list
		$(srcdir)/scripts/check-mirrors "$<" "$@"
mirrors.list:	mirrors.txt
		$(AWK) -f $(srcdir)/scripts/convert-mirrors.awk "$<" "$@"
mirrors.txt:
		wget -O - ftp://ftp.debian.org/debian/README.mirrors.txt \
		    ftp://ftp.debian.org/debian/README.non-US | cat >"$@"

# Create source tarballs from CVS
cvsdist:	Makefile
		@if test -f jigdo -o -f jigdo-`cat VERSION`; then \
		    echo "Object jigdo* already exists - delete it first"; \
		    exit 1; \
		fi
		@read d v version <"jigdo.spec"; \
		if test "$$version" != "`cat VERSION`"; then \
		    echo "Different version in VERSION and jigdo.spec"; \
		    exit 1; \
		fi
		cvs -Q checkout jigdo
		cd jigdo && autoconf
		@f=`find jigdo -name '*.cc' -o -name '*.[hif]h'` \
		    && printf 'version %s, %d files, %d lines of code\n' \
		       `cat jigdo/VERSION` `echo $$f|wc -w` `cat $$f|wc -l`
		@cvstag="rel-`sed -e 's/[^0-9a-zA-Z_-]\+/-/g' jigdo/VERSION`"; \
		    printf "Tag this as \`$$cvstag' in CVS? [y/N/force] "; \
		    read; \
		    if test "$$REPLY" = y; then \
		        echo "cd jigdo && cvs tag $$cvstag ."; \
		        cd jigdo && cvs tag $$cvstag .; \
		    elif test "$$REPLY" = force; then \
		        echo "cd jigdo && cvs tag -F $$cvstag ."; \
		        cd jigdo && cvs tag -F $$cvstag .; \
		    fi
		cd jigdo/doc && $(MAKE) -f ../../doc/Makefile
		cd src && $(MAKE) depend mingw
		cp src/{Makedeps,gtk-interface.cc.tmp,*.mingw} jigdo/src
		cd jigdo/po && $(MAKE) -f ../../po/Makefile update-gmo
		jdir=jigdo-`cat jigdo/VERSION`; \
		    mv jigdo "$$jdir"; \
		    tar --exclude=CVS -cf "$$jdir.tar" "$$jdir"; \
		    bzip2 -f -k -9 "$$jdir.tar"; \
		    gzip -f -9 "$$jdir.tar"; \
		    rm -rf "$$jdir"
