# Makefile machinery to add subtitles and prepare Ogg Media files out
# of extracted portions.

# Copyright 2007 Alexandre Oliva <lxoliva@fsfla.org>

# 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 program 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, a copy can be downloaded from
# http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

LANGS = pt en es

.PHONY: all
all:

DEFEXT = ogm

AWK = awk

MENCODER = mencoder
MCOPY = $(MENCODER) -vf harddup -oac lavc -ovc lavc -lavcopts vcodec=ffv1 -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -o $@

VFS21 = -vf scale=360:180,harddup
VFL43 = -vf expand=720:540:0:0:1,scale=1024:768,harddup
VFS43 = -vf expand=720:540:0:0:1,scale=360:270,harddup

ALL_NAMES = fsmatrix trailer1 trailer2
ALL_AVI = $(patsubst %, %.avi, $(ALL_NAMES))
ALL_LARGE = $(foreach LANG, $(LANGS), $(patsubst %, %.$(LANG).$(DEFEXT), $(ALL_NAMES)))
ALL_SMALL = $(patsubst %, small/%, $(ALL_LARGE))
ALL_NOSUB = $(patsubst %, small/%.nosub.$(DEFEXT), $(ALL_NAMES))
ALL = $(ALL_LARGE) $(ALL_SMALL) $(ALL_NOSUB)
TEMP_AVI = $(ALL:%.ogm=%.avi)

ifeq ($(extract),1)
include Makefile.x
else
$(ALL_AVI):
	@test -f $@ || echo re-run make with extract=1
endif

all: $(ALL)

SUB = -utf8 -subfont-text-scale 2.5 -subalign 0 -subpos 67 -subwidth 85 -sub $(notdir $(@:%.ogm=%.mpsub))

fsmatrix.%.mpsub: mergempsub fsmatrix-mov.%.mpsub fsmatrix-new.%.mpsub
	rm -f $@T
	$(AWK) -f ./mergempsub fsmatrix-mov.$*.mpsub fsmatrix-new.$*.mpsub > $@T
	mv $@T $@

SUBLANG = $(basename $(basename $@))

$(foreach LANG, $(LANGS), %.$(LANG).avi): %.avi
$(ALL_LARGE:%.ogm=%.avi): %.avi:
	-rm -f $@
	ln -s $(basename $*).avi $@

$(foreach LANG, $(LANGS) nosub, small/%.$(LANG).avi): %.avi
$(ALL_SMALL:%.ogm=%.avi) $(ALL_NOSUB:%.ogm=%.avi): small/%.avi:
	-rm -f $@
	mkdir -p small
	ln -s ../$(basename $*).avi $@

$(ALL_LARGE): %.ogm: %.mpsub %.avi
	-rm -f $@
	MPLAYER_VIDEO_FLAGS="$(VFL43) $(SUB)" \
	./mov2ogm $(@:%.ogm=%.avi)

$(ALL_SMALL): small/%.ogm: %.mpsub small/%.avi
	-rm -f $@
	MPLAYER_VIDEO_FLAGS="$(VFS43) $(SUB)" \
	  ./mov2ogm  $(@:%.ogm=%.avi)

$(ALL_NOSUB): small/%.nosub.ogm: small/%.nosub.avi
	-rm -f $@
	MPLAYER_VIDEO_FLAGS="$(VFS21)" \
	  ./mov2ogm  $(@:%.ogm=%.avi)

.PHONY: xclean clean-temp clean-avi clean distclean
xclean:

clean-avi:
	-rm -f $(ALL_AVI)

clean-temp:
	-rm -f $(TEMP_AVI)

clean: clean-temp
	-rm -f $(ALL)

distclean: clean clean-links xclean
