#!/usr/bin/make -f
# -*- makefile -*-
#
#   Makefile - main makefile for package subtitleripper
#
#   Copyright (C) 2002-2004 Arne Driescher
#                 2015 Joachim Wiedorn <joodevel@joonet.de>
#
#   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 2 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/>.
#
# -------------------------------------------------------------------------

# Name of the package
PACKAGE = subtitleripper

# version number
VERSION=0.5.2

# all installation destinations will be prefixed by $(DESTDIR)
# common prefix for install directories
prefix  = /usr/local
bindir  = $(prefix)/bin
datadir = $(prefix)/share

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

# use always:
DEFINES :=
LIBS    := -g -lm
INCLUDES :=

### enable ppm support ###
DEFINES += -D_HAVE_LIB_PPM_
LIBS    += -lnetpbm

### enable PNG support ###
DEFINES += -D_HAVE_PNG_
LIBS    += -lpng

### enable zlib support ###
DEFINES += -D_HAVE_ZLIB_
LIBS    += -lz

CC       = gcc
WARN     = -Wall -Wstrict-prototypes
COPT     = -g -O2

CFLAGS = $(COPT) $(WARN) $(DEFINES) $(INCLUDES)

# list of binary files
TARGETS = srttool \
  subtitle2pgm \
  subtitle2vobsub \
  vobsub2pgm

# list of temporary object files
OBJECTS = spudec.o \
  srttool.o \
  subtitle2pgm.o \
  subtitle2vobsub.o \
  vobsub2pgm.o \
  vobsub.o

SCRIPTS = pgm2txt \
  subtitleripper

FILTERS = gocrfilter_de.sed \
  gocrfilter_en.sed \
  gocrfilter_fr.sed \
  gocrfilter_nl.sed \
  gocrfilter_none.sed

# files which are part of the package
PACKAGE_FILES = AUTHORS \
  ChangeLog COPYING \
  gocrfilter_de.sed \
  gocrfilter_en.sed \
  gocrfilter_fr.sed \
  gocrfilter_nl.sed \
  gocrfilter_none.sed \
  Makefile pgm2txt \
  subtitleripper \
  README README.gocr \
  README.srttool \
  README.subtitle2pgm \
  README.vobsub \
  spudec.c spudec.h \
  srttool.c \
  subtitle2pgm.c \
  subtitle2pgm.h \
  subtitle2vobsub.c \
  subtitleripper.spec \
  vobsub2pgm.c \
  vobsub.c \
  vobsub.h


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

build:  $(TARGETS) $(SCRIPTS)


# Generic Rules
%.o:%.c
	@echo Compiling $<
	@$(CC) -c $(CFLAGS) $(CPPFLAGS) $<

# Dependencies
subtitle2pgm.o: subtitle2pgm.c spudec.h subtitle2pgm.h
spudec.o: spudec.c spudec.h

subtitle2vobsub.o: subtitle2vobsub.c vobsub.c vobsub.h

vobsub2pgm.o: vobsub2pgm.c vobsub.h spudec.h

# Target
subtitle2pgm: subtitle2pgm.o spudec.o
	@echo "Linking $@"
	@$(CC) $(LIBS) $(LDFLAGS) $^ -o $@

subtitle2vobsub: subtitle2vobsub.o vobsub.o
	@echo "Linking $@"
	@$(CC) $(LIBS) $(LDFLAGS) $^ -o $@

srttool: srttool.o
	@echo "Linking $@"
	@$(CC) $(LIBS) $(LDFLAGS) $^ -o $@

vobsub2pgm: vobsub2pgm.o vobsub.o spudec.o
	@echo "Linking $@"
	@$(CC) $(LIBS) $(LDFLAGS) $^ -o $@

pgm2txt: pgm2txt.in
	@echo "Generate $@"
	@$(INSTALL_PROG) -T pgm2txt.in pgm2txt
	@sed -i "s#@@FILTDIR@@#$(datadir)/$(PACKAGE)#" pgm2txt
	@sed -i "s#@@VERSION@@#$(VERSION)#" pgm2txt

subtitleripper: subtitleripper.in
	@echo "Generate $@"
	@$(INSTALL_PROG) -T $^ $@
	@sed -i "s#@@FILTDIR@@#$(datadir)/$(PACKAGE)#" $@
	@sed -i "s#@@VERSION@@#$(VERSION)#" $@

install: build
	@mkdir -p $(DESTDIR)$(bindir)
	$(INSTALL_PROG) $(SCRIPTS) $(DESTDIR)$(bindir)
	$(INSTALL_PROG) $(TARGETS)  $(DESTDIR)$(bindir)
	@mkdir -p $(DESTDIR)$(datadir)/$(PACKAGE)
	$(INSTALL_DATA) $(FILTERS)  $(DESTDIR)$(datadir)/$(PACKAGE)

uninstall:
	$(foreach BIN, $(SCRIPTS), rm -f $(DESTDIR)$(bindir)/$(BIN);)
	$(foreach BIN, $(TARGETS), rm -f $(DESTDIR)$(bindir)/$(BIN);)
	$(foreach DAT, $(FILTERS), rm -f $(DESTDIR)$(datadir)/$(PACKAGE)/$(DAT);)
	@rm -rf $(DESTDIR)$(datadir)/$(PACKAGE)


distclean: clean

clean:
	rm -f $(TARGETS)
	rm -f $(OBJECTS)
	rm -rf rpmbuild
	rm -f pgm2txt
	rm -f subtitleripper
	rm -rf $(PACKAGE)-$(VERSION).tar.gz


$(PACKAGE)-$(VERSION).tar.gz: $(PACKAGE_FILES)
	@mkdir $(PACKAGE)-$(VERSION)
	@cp $(PACKAGE_FILES) $(PACKAGE)-$(VERSION)
	@tar cvzf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	@rm -rf $(PACKAGE)-$(VERSION)

rpmbuild/RPMS/noarch/$(PACKAGE)-$(VERSION)-1.noarch.rpm \
rpmbuild/SRPMS/$(PACKAGE)-$(VERSION).src.rpm: $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE).spec
	@chmod a+rw $(PACKAGE)-$(VERSION).tar.gz
	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)/g" $(PACKAGE).spec >rpmbuild/SPECS/$(PACKAGE).spec
	ln -sf ../../$(PACKAGE)-$(VERSION).tar.gz rpmbuild/SOURCES/
	rpmbuild -v -ba --clean --target noarch \
		--define "_topdir $(PWD)/rpmbuild" \
		rpmbuild/SPECS/$(PACKAGE).spec

# Build a tar.gz source package
dist: $(PACKAGE)-$(VERSION).tar.gz
	@mv $(PACKAGE)-$(VERSION).tar.gz  ..

# Build a rpm package
rpm: rpmbuild/SRPMS/$(PACKAGE)-$(VERSION).src.rpm \
     rpmbuild/RPMS/noarch/$(PACKAGE)-$(VERSION)-1.noarch.rpm


.PHONY: install uninstall distclean clean dist rpm
