#!/usr/bin/make -f
# -*- makefile -*-
#
#   Makefile - main makefile for package dvdwizard
#
#   Copyright (C) 2009-2011 Joo Martin <joomart2009 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/>.
#
# -------------------------------------------------------------------------


# common prefix for installation directories.
prefix  = /usr/local
bindir  = $(prefix)/bin
datadir = $(prefix)/share
mandir  = $(datadir)/man
confdir = /etc

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

# check some directories
BINDIR_WRITE :=$(shell if [ -w "$(DESTDIR)$(bindir)"  ]; then echo y; else echo n; fi)
DATADIR_WRITE:=$(shell if [ -w "$(DESTDIR)$(datadir)" ]; then echo y; else echo n; fi)
ERRORTEXT:="is not writable or does not exist. Abort!"

SCRIPTS := chaptercheck dvdcpics dvdtguess dvdwizard \
	mk_vmgm mk_vtsm mk_vtsm_info mk_vtsm_lang mpgprobe txt2png

MPAGES1 := dvdwizard.1 mk_vmgm.1 mk_vtsm.1 
MPAGES2 := dvdwizard.conf.5

# Example (at the end of the command write always ';')
# @$(foreach script,$(SCRIPTS), echo $(script);)


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

build:
	@echo -e '\n~~~~~~~~~~ make build ~~~~~~~~~~'
	@echo "Create scripts into bin for installation ..."
	@if test ! -d bin; then mkdir bin; fi
	@$(foreach script, $(SCRIPTS), cat $(script) |\
		sed 's#@DW_RC_FILE@#$(datadir)/dvdwizard/dvdwizardrc#g' >bin/$(script);)
	@echo converted paths in all scripts:
	@echo $(SCRIPTS)


distclean: clean
clean:
	@echo -e '\n~~~~~~~~~~ make clean ~~~~~~~~~~'
	-rm -rf bin


check:
	@echo -e '\n~~~~~~~~~~ make check ~~~~~~~~~~'
  ifneq "$(BINDIR_WRITE)" "y"
	@echo "Target directory $(DESTDIR)$(bindir) $(ERRORTEXT)"; exit 1
  endif
  ifneq "$(DATADIR_WRITE)" "y"
	@echo "Target directory $(DESTDIR)$(datadir) $(ERRORTEXT)"; exit 1
  endif
	@if [ ! -d $(DESTDIR)$(datadir)/dvdwizard ]; then mkdir $(DESTDIR)$(datadir)/dvdwizard; fi
	@if [ ! -d $(DESTDIR)$(mandir)/man1 ]; then mkdir $(DESTDIR)$(mandir)/man1; fi
	@if [ ! -d $(DESTDIR)$(mandir)/man5 ]; then mkdir $(DESTDIR)$(mandir)/man5; fi
	@echo "All checked directories are o.k."


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

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

	@echo "Install central script into $(DESTDIR)$(datadir) ..."
	@$(INSTALL_DATA) -D dvdwizardrc $(DESTDIR)$(datadir)/dvdwizard/dvdwizardrc

	@echo "Install all manpages into $(DESTDIR)$(mandir) ..."
	@$(foreach mpage, $(MPAGES1), $(INSTALL_DATA) doc/$(mpage) $(DESTDIR)$(mandir)/man1;)
	@$(foreach mpage, $(MPAGES2), $(INSTALL_DATA) doc/$(mpage) $(DESTDIR)$(mandir)/man5;)

	@echo .
	@echo "Installing config file into $(DESTDIR)$(confdir) ..."
	@if test ! -e $(DESTDIR)$(confdir); then mkdir -p $(DESTDIR)$(confdir); fi

	@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"; \
	    cp dvdwizard.conf.sample $(DESTDIR)$(confdir)/dvdwizard.conf; \
	    echo "Installed new config as $(DESTDIR)$(confdir)/dvdwizard.conf"; \
 	    echo "Please edit the new config to your preferences."; \
	  fi; \
	else \
	  cp dvdwizard.conf.sample $(DESTDIR)$(confdir)/dvdwizard.conf; \
	  echo "Installed new config as $(DESTDIR)$(confdir)/dvdwizard.conf"; \
	  echo "Please edit the new config to your preferences."; \
	fi

	@echo .
	@echo "Make sure, $(DESTDIR)$(bindir) is inside your PATH."
	@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 "Deinstall central script in $(DESTDIR)$(datadir) ..."
	@-rm -rf $(DESTDIR)$(datadir)/dvdwizard

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



info:
	@echo "Using this Makefile with the global variable 'DESTDIR':"
	@echo "i.e.   make install DESTDIR=debian/dvdwizard"


.PHONY: build clean install uninstall info
