#!/usr/bin/make -f
# -*- makefile -*-
#
#   Makefile - main makefile for package dvdwizard
#
#   Copyright (c) 2009,2012 Joo Martin <joomart2009 at users.sf.net>
#   Copyright (c) 2010-2011 Joo Martin & Markus Kohm <kohm at users.sf.net>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# -------------------------------------------------------------------------


# NOTE:
#   - Linux distributors may want to change docdir into $(datadir)/doc/packages.
#   - All installation destinations will be prefixed by $(DESTDIR).
# common prefix for installation directories:
prefix    = /usr/local
bindir    = $(prefix)/bin
datadir   = $(prefix)/share
mandir    = $(datadir)/man
infodir   = $(datadir)/info
docdir    = $(datadir)/doc
localedir = $(datadir)/dvdwizard/locale
confdir   = /etc

# without country specific translations
LANG=C

# common variables
PACKAGE    := dvdwizard
VERSION    := 0.7
PATCHLEVEL := 1
DATE       := 2012-03-19

SHELL := $(shell which bash)
ifeq ($(SHELL),)
SHELL := /bin/bash
endif

# the install commands
INSTALL := /usr/bin/install
INSTALL_PROG := $(INSTALL) -m 755
INSTALL_DATA := $(INSTALL) -m 644

SCRIPTS := dvdwizard_common chaptercheck dvdcpics dvdtguess dvdwizard \
	   mk_vmgm mk_vtsm mk_vtsm_info mk_vtsm_lang mpgprobe txt2png \
	   dvdwizard_test
MPAGES1 := dvdwizard.1 mk_vmgm.1 mk_vtsm.1 
MPAGES5 := dvdwizard.conf.5
INFOS   := DVDwizard.info
DOCS    := AUTHORS CHANGELOG CONTRIB COPYING README TODO TOOLS \
	   dvdwizard.conf.sample DVDwizard.pdf DVDwizard.html
LANGS   := de
DATA    := Clapperboard.svg

MOFILES := $(foreach lang, $(LANGS), $(lang)/LC_MESSAGES/dvdwizard.mo)
POFILES := $(foreach lang, $(LANGS), po/$(lang).po)
HOLDER  := Markus Kohm, Joachim Wiedorn

MPAGES    := $(MPAGES1) $(MPAGES5)
PATCHED   := $(SCRIPTS) dvdwizard.conf $(DATA)
UNPATCHED := $(addprefix doc/,$(MPAGES)) $(INFOS) \
	     $(DOCS) DVDwizard.txt $(addprefix locale/,$(MOFILES))

DISTFILES := $(SCRIPTS) $(addprefix doc/,$(MPAGES)) $(DOCS) $(POFILES) $(DATA)\
	     Makefile install.sh dvdwizard.spec
DISTDIR   = dvdwizard-$(VERSION)$(if $(PATCHLEVEL),.)$(PATCHLEVEL)

# ~~~~~~~~~~~~~~~~~~~~~~~~ rules ~~~~~~~~~~~~~~~~~~~~~~~~

#
# Copy all files to directory build.
# For all files at $(PATCHED) the pathes @LOCALDIR@, @DOCDIR@, @MANDIR@, 
# @DATADIR@, @CONFDIR@, @PREFIX@, @VERSION@, @PATCHLEVEL@, and @DATE@ 
# will be relaced by their value at this makefile (see above).  If you need 
# one of those strings at one of those files, just double the `@', 
# e.g., @@PREFIX@@ would be replaced by @PREFIX@.
build: $(PATCHED) $(UNPATCHED)
	@echo -e '\n~~~~~~~~~~ make build ~~~~~~~~~~'
	@echo "Create all files into build for installation ..."
	@if test ! -d build; then mkdir build; fi
	@echo -n "- convert pathes in: "
	@$(foreach patched, $(PATCHED), \
		echo -n "$(patched)..."; \
		sed -e 's#\([^@]\)@LOCALEDIR@\([^@]\)#\1$(localedir)\2#g;s#@LOCALEDIR@#LOCALEDIR#g' \
		    -e 's#\([^@]\)@DOCDIR@\([^@]\)#\1$(docdir)\2#g;s#@DOCDIR@#DOCDIR#g' \
		    -e 's#\([^@]\)@MANDIR@\([^@]\)#\1$(mandir)\2#g;s#@MANDIR@#MANDIR#g' \
		    -e 's#\([^@]\)@DATADIR@\([^@]\)#\1$(datadir)\2#g;s#@DATADIR@#DATADIR#g' \
		    -e 's#\([^@]\)@CONFDIR@\([^@]\)#\1$(confdir)\2#g;s#@CONFDIR@#CONFDIR#g' \
		    -e 's#\([^@]\)@PREFIX@\([^@]\)#\1$(prefix)\2#g;s#@PREFIX@#PREFIX#g' \
		    -e 's#\([^@]\)@VERSION@\([^@]\)#\1$(VERSION)\2#g;s#@VERSION@#VERSION#g' \
		    -e 's#\([^@]\)@PATCHLEVEL@\([^@]\)#\1$(PATCHLEVEL)\2#g;s#@PATCHLEVEL@#PATCHLEVEL#g' \
		    -e 's#\([^@]\)@DATE@\([^@]\)#\1$(DATE)\2#g;s#@DATE@#DATE#g' \
		    $(patched) >build/$(patched); )
	@echo "done."
	@echo -n "- create link to: "
	@$(foreach unpatched, $(UNPATCHED), \
		echo -n "$(unpatched)..."; \
		mkdir -p build/"$(dir $(unpatched))" >&2; \
		ln -f $(unpatched) build/$(unpatched); )
	@mv -f build/DVDwizard.txt build/README
	@echo "done."
	@echo "build made."

distclean: clean
	-rm -rf locale
	-rm -f  DVDwizard.dvi DVDwizard.pdf DVDwizard.info DVDwizard.html
	-rm -f  DVDwizard.txt dvdwizard.conf.sample
	-rm -rf rpmbuild

clean:
	@echo -e '\n~~~~~~~~~~ make clean ~~~~~~~~~~'
	-rm -rf build
	-rm -rf rpmbuild/BUILD rpmbuild/BUILDROOT rpmbuild/SOURCES \
	        rpmbuild/SPECS
	-rm -f dvdwizard.conf
	-rm -f DVDwizard.aux DVDwizard.log DVDwizard.tmp DVDwizard.toc \
	       DVDwizard.au DVDwizard.aus DVDwizard.cp DVDwizard.cps \
	       DVDwizard.fn DVDwizard.fns DVDwizard.ky DVDwizard.kys \
	       DVDwizard.pg DVDwizard.pgs DVDwizard.tp DVDwizard.tps \
	       DVDwizard.vr DVDwizard.vrs

DVDwizard.info: DVDwizard.texi
	makeinfo --no-split $<

DVDwizard.txt: DVDwizard.texi
	makeinfo --plaintext -o $@ $<

DVDwizard.html: DVDwizard.texi
	makeinfo --html --no-split $<

DVDwizard.pdf: DVDwizard.texi
	pdftex --interaction=nonstopmode DVDwizard.texi
	texindex DVDwizard.au DVDwizard.pg
	pdftex --interaction=nonstopmode DVDwizard.texi
	texindex DVDwizard.au DVDwizard.pg
	pdftex --interaction=nonstopmode DVDwizard.texi

dvdwizard.conf.sample: dvdwizard.conf.in
	cat $< | sed "s#@@VERSION@@#$(VERSION)#g" | sed "s#@@LEVEL@@#$(PATCHLEVEL)#g" > $@

dvdwizard.conf: dvdwizard.conf.sample
	ln -f $< $@

check:
	@echo -e '\n~~~~~~~~~~ make check ~~~~~~~~~~'
	@mkdir -p $(DESTDIR)$(bindir)
	@mkdir -p $(DESTDIR)$(datadir)
	@mkdir -p $(DESTDIR)$(docdir)/dvdwizard
	@$(foreach modir,$(dir $(MOFILES)),\
		mkdir -p $(DESTDIR)$(localedir)/$(modir);)

	@mkdir -p $(DESTDIR)$(mandir)/man1
	@mkdir -p $(DESTDIR)$(mandir)/man5
	@mkdir -p $(DESTDIR)$(infodir)
	@mkdir -p $(DESTDIR)$(confdir)
	@$(foreach lang, $(LANGS), mkdir -p $(DESTDIR)$(localedir)/$(lang)/LC_MESSAGES;)
	@echo "All needed directories created."

	$(eval msg=$(shell . ./dvdwizard_common; . ./dvdwizard.conf.sample; check_tools 2>&1 | head -1 | cut -d: -f2))
	@$(if $(msg), $(error $(msg)))
	@echo "All needed tools are installed."


install: build check
	@echo -e '\n~~~~~~~~~~ make install ~~~~~~~~~~~'
	@echo "Installing dvdwizard ..."

	@echo
	@echo "Install all scripts into $(DESTDIR)$(bindir) ..."
	@$(foreach script, $(SCRIPTS), \
		$(INSTALL_PROG) build/$(script) $(DESTDIR)$(bindir);)

	@echo "Install data files into $(DESTDIR)$(datadir)/dvdwizard ..."
	@$(foreach data, $(DATA), \
		$(INSTALL_DATA) build/$(data) $(DESTDIR)$(datadir)/dvdwizard/;)

	@echo "Install i18n files into $(DESTDIR)$(localedir)/<LANGUAGE>/LC_MESSAGES/ ..."
	@$(foreach mofile, $(MOFILES), \
		$(INSTALL_DATA) build/locale/$(mofile) $(DESTDIR)$(localedir)/$(mofile);)

	@echo "Install man pages into $(DESTDIR)$(mandir) ..."
	@$(foreach mpage, $(MPAGES1), \
		$(INSTALL_DATA) build/doc/$(mpage) $(DESTDIR)$(mandir)/man1;)
	@$(foreach mpage, $(MPAGES5), \
		$(INSTALL_DATA) build/doc/$(mpage) $(DESTDIR)$(mandir)/man5;)

	@echo "Install info pages into $(DESTDIR)$(infodir) ..."
	@$(foreach info, $(INFOS), \
		$(INSTALL_DATA) build/$(info) $(DESTDIR)$(infodir)/$(info);)

	@echo "Installing doc files into $(DESTDIR)$(docdir) ..."
	@$(foreach doc, $(DOCS), \
		$(INSTALL_DATA) build/$(doc) $(DESTDIR)$(docdir)/dvdwizard; )

	@echo
	@echo "Installing config file into $(DESTDIR)$(confdir) ..."
	@if  test -f $(DESTDIR)$(confdir)/dvdwizard.conf; then \
	  echo "You already have a $(DESTDIR)$(confdir)/dvdwizard.conf!"; \
	  if  test -f $(DESTDIR)$(confdir)/dvdwizard.conf.old; then \
	    echo "And you still have a $(DESTDIR)$(confdir)/dvdwizard.conf.old"; \
	    echo "Ok, keeping old config files."; \
	    echo "Refer to dvdwizard.conf.sample for new/changed options."; \
	  else \
	    cp -p $(DESTDIR)$(confdir)/dvdwizard.conf $(DESTDIR)$(confdir)/dvdwizard.conf.old; \
	    echo "Saved existing config to $(DESTDIR)$(confdir)/dvdwizard.conf.old"; \
	    $(INSTALL_DATA) build/dvdwizard.conf $(DESTDIR)$(confdir); \
	    echo "Installed new config as $(DESTDIR)$(confdir)/dvdwizard.conf"; \
	    echo "Please edit the new config to your preferences."; \
	  fi; \
	else \
	  $(INSTALL_DATA) build/dvdwizard.conf $(DESTDIR)$(confdir); \
	  echo "Installed new config as $(DESTDIR)$(confdir)/dvdwizard.conf"; \
	  echo "Please edit the new config to your preferences."; \
	fi

	@echo
	@echo "Make sure that:"
	@echo "   $(DESTDIR)$(bindir) is inside your PATH."
	@echo "   $(DESTDIR)$(mandir) is inside your MANPATH."
	@echo
	@echo "DVDwizard installation ready."


uninstall:
	@echo -e '\n~~~~~~~~~~ make uninstall ~~~~~~~~~~~'
	@echo "Deinstalling DVDwizard ..."

	@echo "Deinstall all scripts in \"$(DESTDIR)$(bindir)\" ..."
	@-$(foreach script,$(SCRIPTS), rm -f "$(DESTDIR)$(bindir)/$(script)";)

	@echo
	@echo "Deinstall i18n files in $(DESTDIR)$(localedir)/locale/<LANGUAGE>/LC_MESSAGES/ ..."
	@-$(foreach mofile, $(addprefix $(DESTDIR)$(localedir)/,$(MOFILES)), \
		rm -f "$(mofile)";)
	@-$(foreach modir, $(dir $(addprefix $(DESTDIR)$(localedir)/,$(MOFILES))), \
		if [ -z "`ls -A \"$(modir)\"`" ]; then \
		    echo "Deinstall data dir $(modir) ..."; \
		    rm -rf "$(modir)"; \
		fi; )
	@-$(foreach langdir, $(addprefix $(DESTDIR)$(localedir)/,$(LANGS)), \
		if [ -z "`ls -A \"$(langdir)\"`" ]; then \
		    echo "Deinstall data dir $(langdir) ..."; \
		    rm -rf "$(langdir)"; \
		fi; )
	@if [ -z "`ls -A \"$(DESTDIR)$(localedir)\"`" ]; then \
	    echo "Deinstall data dir $(DESTDIR)$(localedir) ..."; \
	    rm -rf "$(DESTDIR)$(localedir)"; \
	fi

# 	Note: $(localedir) may be part of $(datadir)/dvdwizard.
#	       Because of this removing all data and datadir should be
#	       done after removing of all locale files and dirs.
	@echo
	@echo "Deinstall data files in $(DESTDIR)$(datadir)/dvdwizard ..."
	@$(foreach data, $(DATA), \
		rm -f "$(DESTDIR)$(datadir)/dvdwizard/$(data)";)
	@if [ -z "`ls -A \"$(DESTDIR)$(datadir)/dvdwizard\"`" ]; then \
	    echo "Deinstall data dir $(DESTDIR)$(datadir)/dvdwizard ..."; \
	    rm -rf "$(DESTDIR)$(datadir)/dvdwizard"; \
	fi

	@echo
	@echo "Deinstall manpages in \"$(DESTDIR)$(mandir)\" ..."
	@-$(foreach mpage, $(MPAGES1), \
		rm -f "$(DESTDIR)$(mandir)/man1/$(mpage)";)
	@-$(foreach mpage, $(MPAGES5), \
		rm -f "$(DESTDIR)$(mandir)/man5/$(mpage)";)

	@echo
	@echo "Deinstall info pages in $(DESTDIR)$(infodir) ..."
	@$(foreach info, $(INFOS), \
		rm -f $(DESTDIR)$(infodir)/$(info);)

	@echo
	@echo "Deinstall doc in $(DESTDIR)$(docdir)/dvdwizard ..."
	@-$(foreach doc, $(DOCS), rm -f $(DESTDIR)$(docdir)/dvdwizard/$(doc);)
	@if [ -z "`ls -A \"$(DESTDIR)$(docdir)/dvdwizard\"`" ]; then \
	    echo "Deinstall doc dir $(DESTDIR)$(docdir)/dvdwizard ..."; \
	    rm -rf "$(DESTDIR)$(docdir)/dvdwizard"; \
	fi

	@echo
	@echo "Note: Configuration file $(DESTDIR)$(confdir)/dvdwizard.conf will not be removed!"
	@echo "DVDwizard deinstallation finished."

#
# i18n support for ui
#
languages: $(addsuffix /LC_MESSAGES/dvdwizard.mo, $(addprefix locale/,$(LANGS)))

po/dvdwizard.pot: $(SCRIPTS)
	@rm -f po/temp.po       2>/dev/null
	@$(foreach script, $(SCRIPTS), bash --dump-po-strings $(script) >>po/temp.po;)
	@xgettext -L PO --from-code=UTF-8 --copyright-holder="$(HOLDER)" \
	  --package-name="$(PACKAGE)" --package-version="$(VERSION)" \
	  --msgid-bugs-address="<$(PACKAGE)-users@lists.sourceforge.net>" \
	  -o po/$(PACKAGE).pot po/temp.po
	@rm -rf po/temp.po 2>/dev/null

$(POFILES): po/dvdwizard.pot
	@if [ -e $@ ]; then \
		msgmerge --output-file $@ $@ $<; \
	else \
		msginit --no-translator --output-file $@ -l $(@F) -i $<; \
	fi

$(addprefix locale/,$(MOFILES)): $(POFILES)
	mkdir -p $(@D)
	msgfmt -o $@ po/$(subst /LC_MESSAGES/dvdwizard.mo,,$(subst locale/,,$@))

#
# Distribution
#

dist: $(DISTDIR).tar

$(DISTDIR).tar: $(DISTFILES)
	@echo -n "Create $(DISTDIR).tar "
	@rm -rf $(DISTDIR) 2>/dev/null
	@mkdir $(DISTDIR)
	@$(foreach distfile,$(DISTFILES), \
		echo -n .; \
		mkdir -p $(DISTDIR)/$(dir $(distfile)); \
		cp -f $(distfile) $(DISTDIR)/$(distfile);)
	@tar cf $(DISTDIR).tar $(DISTDIR)
	@rm -r $(DISTDIR)
	@echo "done."

distgz: $(DISTDIR).tar
	gzip -c $(DISTDIR).tar > $(DISTDIR).tar.gz

distbz2: $(DISTDIR).tar.bz2 

$(DISTDIR).tar.bz2: $(DISTDIR).tar
	bzip2 -c $< > $@

distlzma: $(DISTDIR).tar
	lzma -c $(DISTDIR).tar > $(DISTDIR).tar.lzma

distzip: $(DISTDIR).tar
	@tar xf $(DISTDIR).tar
	zip -r $(DISTDIR).zip $(DISTDIR)
	@rm -r $(DISTDIR)

distrpm: rpmbuild/RPMS/noarch/$(DISTDIR)-1.noarch.rpm \
         rpmbuild/SRPMS/$(DISTDIR)-1.src.rpm

rpmbuild/RPMS/noarch/$(DISTDIR)-1.noarch.rpm \
rpmbuild/SRPMS/$(DISTDIR)-1.src.rpm: $(DISTDIR).tar.bz2 dvdwizard.spec
	mkdir -p rpmbuild/BUILD
	mkdir -p rpmbuild/RPMS
	mkdir -p rpmbuild/SOURCES
	mkdir -p rpmbuild/SPECS
	mkdir -p rpmbuild/SRPMS
	sed -e "s/@VERSION@/$(VERSION).$(PATCHLEVEL)/g" dvdwizard.spec >rpmbuild/SPECS/dvdwizard.spec
	ln -sf ../../$(DISTDIR).tar.bz2 rpmbuild/SOURCES/
	rpmbuild -v -ba --clean \
		--define "_topdir $(PWD)/rpmbuild" \
		--target noarch \
		rpmbuild/SPECS/dvdwizard.spec

#
# help info
#
info: help

help:
	@echo 'make help'
	@echo '	Show this help and exit.'
	@echo
	@echo 'make info'
	@echo '	Same like "make help".'
	@echo
	@echo 'make build'
	@echo '	Prepare the source for the installation.'
	@echo
	@echo 'sudo make install'
	@echo '	Install DVDwizard into your system. If you have not called "make build"'
	@echo '	before, make build would be done using root permissions!'
	@echo
	@echo 'DISTDIR=foo make install'
	@echo '	Install DVDwizard into an anticipated system located at foo.'
	@echo
	@echo 'sudo make uninstall'
	@echo '	Remove an previously installed DVDwizard from your system.'
	@echo '	NOTE: Only installations using "sudo make installed" may be uninstalled'
	@echo '	      correctly!'
	@echo
	@echo 'DISTDIR=foo make uninstall'
	@echo '	Remove an DVDwizard installation, that was made using'
	@echo '	"DISTDIR=foo make install".'
	@echo
	@echo 'make languages'
	@echo '	Prepare/Update pot-file and po-files for all supported languages.'
	@echo
	@echo 'make dist'
	@echo '	Build '$(DISTDIR).tar' with all files needed to distribute DVDwizard source.'
	@echo
	@echo 'make distgz'
	@echo '	Build '$(DISTDIR).tar' and '$(DISTDIR).tar.gz' with all files'
	@echo '	needed to distribute DVDwizard source.'
	@echo
	@echo 'make distbz2'
	@echo '	Build '$(DISTDIR).tar' and '$(DISTDIR).tar.bz2' with all files'
	@echo '	needed to distribute DVDwizard source.'
	@echo
	@echo 'make distlzma'
	@echo '	Build '$(DISTDIR).tar' and '$(DISTDIR).tar.lzma' with all files'
	@echo '	needed to distribute DVDwizard source.'
	@echo
	@echo 'make distzip'
	@echo '	Build '$(DISTDIR).tar' and '$(DISTDIR).zip' with all files'
	@echo '	needed to distribute DVDwizard source.'
	@echo
	@echo 'make distrpm'
	@echo '	Build '$(DISTDIR).tar' and '$(DISTDIR).tar.bz2' with all files'
	@echo '	needed to distribute DVDwizard source and create the source rpm'
	@echo '	'rpmbuild/SRPMS/$(DISTDIR)-1.src.rpm' and the rpm '
	@echo '	'rpmbuild/RPMS/noarch/$(DISTDIR)-1.noarch.rpm'.'
	@echo '	NOTE: You may change the release number at dvdwizard.spec before.'
	@echo
	@echo 'make clean'
	@echo '	Remove all garbage made by "make build".'
	@echo
	@echo 'make distclean'
	@echo '	Remove also all files needed by distributions but build from the primary'
	@echo '	source files.'


.PHONY: build clean install uninstall info languages help \
	dist distclean distgz distbz2 distlzma distzip distrpm
