# This file is part of qVamps.
#
# qVamps 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; version 2 of the License.
#
# qVamps 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 qVamps; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


PREFIX    ?= /usr/local
LANGUAGES ?= xx

PLINC      = $(shell perl -e \
	       'use Config; print "$$Config{archlibexp}/CORE\n";')
PLARCH     = $(shell perl -e 'use Config; print "$$Config{archname}\n";')
PLCFLAGS   = $(shell perl -e 'use Config; print "$$Config{ccflags}\n";')

CC         = gcc
CFLAGS    ?= -pipe -O2 -fomit-frame-pointer -fpic
CFLAGS    += -I $(PLINC) $(PLCFLAGS) -Wall -W -Wno-strict-aliasing \
	       -Wno-unused-function -Wno-unused-variable \
	       -Wno-unused-parameter
SOLDFLAGS ?= -s -shared
LOADLIBES ?= -ldvdread

SWIG      ?= swig
SWIGFLAGS ?= -perl -Wall -nodefault

PUIC      ?= puic
PUICFLAGS ?= -p 2

PERL      ?= perl
INSTALL   ?= install
LUPDATE   ?= lupdate
LRELEASE  ?= lrelease

PP        ?= PATH=/opt/perl-5.8.8/bin:$$PATH pp
PPFLAGS   ?= -z 9
LIBSMOKSO ?= /opt/perl-5.8.8/lib/libsmokeqt.so.1

UIMODULES  = MainWindow VTSList MenuPreview RemoveFilesDialog LicenseDialog
MSGMODULES = QVamps TitleListItems VTSListItems XmlWriter MenuWriter \
	       MenuTableItems FpcMgr DvdMaker MpgDumper
GENMODULES = LibDvdRead DesignerMimeSourceFactory_qvamps
OTHMODULES = DvdIfoRead Iso639 MenuDisplay
IMAGES     = add-row-28x28.png down-row-28x28.png folder_open.png icon.png \
	       remove-row-28x28.png renumber.png up-row-28x28.png vspacer.png

ALL       ?= LibDvdRead.so $(addsuffix .pm, $(GENMODULES) $(UIMODULES)) \
	       $(addsuffix .qm, $(LANGUAGES))
VERSION    = 0.98
BINVERS    = 1
BINDIST   ?= qVamps-$(VERSION).$(BINVERS)-Linux-x86-Install.bin


%.pm:		%.ui %.ui.pm
		$(PUIC) $(PUICFLAGS) -o $@.tmp $<
		$(PERL) -wpe 's/->setText( "" );/->setText( undef );/g' \
		  $@.tmp > $@
		@rm -f $@.tmp

%.msgs:		%.pm
		./fakelupd.pl $* $^ > $@

%.qm:		%.ts
		$(LRELEASE) -verbose $< -qm $@


# default target
all:		$(ALL)

# binary distribution installer
bindist:	all $(BINDIST)

# LibDvdRead.so
LibDvdRead.so:	libdvdread_wrap.o
		$(CC) $(SOLDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@

libdvdread_wrap.c LibDvdRead.pm:	libdvdread.i ifo_read.i ignores.i
		$(SWIG) $(SWIGFLAGS) $<

ifo_read.i:	ifo_read.c
		$(CC) -E -P $< > $@

# .pm from .png
DesignerMimeSourceFactory_qvamps.pm:	$(addprefix images/,$(IMAGES))
		$(PUIC) $(PUICFL) -o $@ -embed qvamps $^

# .ts from .msgs
$(addsuffix .ts,$(LANGUAGES)):	$(addsuffix .msgs,$(UIMODULES) $(MSGMODULES))
		$(LUPDATE) -verbose $^ -ts $@

# .msgs from .pm
TitleListItems.msgs:	DvdIfoRead.pm Iso639.pm

MenuPreview.msgs:	MenuDisplay.pm

# binary distribution archive
qvamps-bin:	qvamps LibDvdRead.so $(addsuffix .pm, $(GENMODULES) \
		  $(UIMODULES) $(MSGMODULES) $(OTHMODULES))
		$(PP) $(PPFLAGS) -o $@ -l $(LIBSMOKSO) -l LibDvdRead.so \
		  -M encoding qvamps

# binary distribution installer
$(BINDIST):	qvamps LibDvdRead.so main_switch.pl install.pl uninstall.pl \
		  qvamps_drv.sh README.bindist $(addsuffix .pm, $(GENMODULES) \
		  $(UIMODULES) $(MSGMODULES) $(OTHMODULES) InstWiz InstCfg \
		  Installer UninstWiz)
		$(PP) $(PPFLAGS) -o $@ -l $(LIBSMOKSO) -l LibDvdRead.so \
		  -M encoding -M MainWindow -M UninstWiz -X Archive::Zip \
		  -a "qvamps;run/qvamps" -a "uninstall.pl;run/uninstall.pl" \
		  -a "main_switch.pl;script/main_switch.pl" \
		  -a "images/inst_splash.png" -a instfiles install.pl
		@chmod +x $@

# other targets go below
install:	all
		$(INSTALL) -m 755 -d $(PREFIX)/share/qvamps/etc \
		  $(PREFIX)/share/qvamps/lib \
		  $(PREFIX)/share/qvamps/translations \
		  $(PREFIX)/lib/qvamps/$(PLARCH)
		$(INSTALL) -m 755 qvamps $(PREFIX)/bin
		$(INSTALL) -m 644 $(addsuffix .pm,$(UIMODULES) \
		  $(MSGMODULES) $(GENMODULES) $(OTHMODULES)) \
		  $(PREFIX)/share/qvamps/lib
		$(INSTALL) -m 644 $(addsuffix .qm,$(LANGUAGES)) \
		  $(PREFIX)/share/qvamps/translations
		$(INSTALL) -m 644 penguin.jpg silence.mpa penguin_ntsc.mpg \
		  penguin_pal.mpg $(PREFIX)/share/qvamps
		$(INSTALL) -m 755 LibDvdRead.so $(PREFIX)/lib/qvamps/$(PLARCH)

uninstall:
		rm -rf $(PREFIX)/bin/qvamps $(PREFIX)/share/qvamps \
		  $(PREFIX)/lib/qvamps

clean:
		rm -f LibDvdRead.so libdvdread_wrap.c ifo_read.i $(addsuffix \
		  .pm,$(UIMODULES) $(GENMODULES) InstWiz UninstWiz) \
		  qvamps-bin $(BINDIST) *.o *.msgs *.qm *.pm.tmp *~ core
