#!/usr/bin/make -f

#export DH_VERBOSE=1
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

confflags += --enable-gpl --enable-pp --enable-pthreads
confflags += --enable-vorbis --enable-libogg --enable-a52 --enable-dts --enable-libgsm

DEB_VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: //p')

ifneq (,$(findstring linux,$(DEB_HOST_GNU_TYPE)))
  confflags += --enable-dc1394
  lib1394-dev = libraw1394-dev, libdc1394-13-dev
  FIREWIRELIBS="-lraw1394 -ldc1394_control"
else
  lib1394-dev =
  FIREWIRELIBS=""
endif

ifeq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  confflags += --disable-debug
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  # This hack is necessary because GCC won't build fdct_mmx.c with -O0
  confflags += --extra-cflags="-O1"
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  confflags += --disable-strip
endif

ifneq (,$(findstring risky,$(DEB_BUILD_OPTIONS)))
weak-build-deps += liblame-dev
confflags += --enable-mp3lame
weak-build-deps += libfaad2-dev
confflags += --enable-faadbin --enable-faad
weak-build-deps += libfaac-dev
confflags += --enable-faac
weak-build-deps += libxvidcore-dev
confflags += --enable-xvid
$(warning Make sure these packages are installed: $(weak-build-deps))
endif

build: build-stamp
build-stamp:
	dh_testdir
	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	
	# Build static libraries
	./configure $(confflags) --disable-ffmpeg --disable-ffserver --disable-ffplay --prefix=/usr
	cd doc && $(MAKE)
	$(MAKE)
	
	# Backup static libraries
	mv libavutil/libavutil.a libavutil/libavutil.a.bak
	mv libpostproc/libpostproc.a libpostproc/libpostproc.a.bak
	mv libavcodec/libavcodec.a libavcodec/libavcodec.a.bak
	mv libavformat/libavformat.a libavformat/libavformat.a.bak
	
	# Build ffmpeg and shared libraries
	./configure $(confflags) --enable-shared --prefix=/usr
	
	sed 's/ldts/ldts_pic/' <config.mak >config.mak.pic
	
	cd libavutil && \
	  $(MAKE) clean && \
	  rm -f Makefile.pic && \
	  sed 's/^CFLAGS *=/&-fPIC -DPIC /; s/config.mak/config.mak.pic/' <Makefile >Makefile.pic && \
	  $(MAKE) -f Makefile.pic && \
	  rm -f Makefile.pic
	
	cd libavcodec && \
	  $(MAKE) clean && \
	  rm -f Makefile.pic && \
	  sed 's/^CFLAGS *=/&-fPIC -DPIC /; s/config.mak/config.mak.pic/' <Makefile >Makefile.pic && \
	  $(MAKE) -f Makefile.pic && \
	  rm -f Makefile.pic
	
	cd libpostproc && \
	  $(MAKE) clean && \
	  rm -f Makefile.pic && \
	  sed 's/^CFLAGS *=/&-fPIC -DPIC /; s/config.mak/config.mak.pic/' <Makefile >Makefile.pic && \
	  $(MAKE) -f Makefile.pic && \
	  rm -f Makefile.pic
	
	cd libavformat && \
	  $(MAKE) clean && \
	  rm -f Makefile.pic && \
	  sed 's/^CFLAGS *=/&-fPIC -DPIC /; s/config.mak/config.mak.pic/' <Makefile >Makefile.pic && \
	  $(MAKE) -f Makefile.pic && \
	  rm -f Makefile.pic
	
	rm -f config.mak.pic
	
	# Restore static libraries
	mv libavutil/libavutil.a.bak libavutil/libavutil.a
	mv libpostproc/libpostproc.a.bak libpostproc/libpostproc.a
	mv libavcodec/libavcodec.a.bak libavcodec/libavcodec.a
	mv libavformat/libavformat.a.bak libavformat/libavformat.a
	
	# Fix timestamps
	touch libavutil/libavutil.a libpostproc/libpostproc.a
	touch libavcodec/libavcodec.a libavformat/libavformat.a
	touch ffmpeg ffmpeg_g ffplay ffplay_g
	
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	-cd tests && $(MAKE)
endif
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	-cd doc && $(MAKE) clean
	-cd tests && $(MAKE) clean
	-$(MAKE) distclean
	QUILT_PATCHES=debian/patches quilt pop -a -R || test $$? = 2
	rm -f libavutil/Makefile.pic libavformat/Makefile.pic config.log
	rm -f libpostproc/Makefile.pic libavformat/Makefile.pic
	rm -f libavutil/libavutil.a.bak
	rm -f libpostproc/libpostproc.a.bak
	rm -f libavcodec/libavcodec.a.bak
	rm -f libavformat/libavformat.a.bak
	rm -f libavcodec-uninstalled.pc libavcodec.pc
	rm -f libavutil-uninstalled.pc libavutil.pc
	rm -f libavformat-uninstalled.pc libavformat.pc
	rm -f libpostproc-uninstalled.pc libpostproc.pc
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -ptmp usr/share/doc/ffmpeg/html etc usr/include/ffmpeg usr/lib usr/bin usr/share/man/man1 usr/include/postproc
	
	$(MAKE) install DESTDIR=`pwd`/debian/tmp/ mandir=`pwd`/debian/tmp/usr/share/man
	rm -f debian/tmp/usr/include/ffmpeg/swscale.h # unused yet
	cp libavcodec/dsputil.h debian/tmp/usr/include/ffmpeg
	cp libpostproc/libpostproc.a debian/tmp/usr/lib
	cp libpostproc/postprocess.h debian/tmp/usr/include/postproc
	cp libpostproc.pc debian/tmp/usr/lib/pkgconfig/
	
	dh_install -ptmp libavutil/libavutil.so* libavcodec/libavcodec.so* libpostproc/libpostproc.so* libavformat/libavformat.so* /usr/lib
	
	cp doc/*.html debian/tmp/usr/share/doc/ffmpeg/html/
	cp doc/ffserver.conf debian/tmp/etc/
	
	sed -e s/@FIREWIRELIBS@/$(FIREWIRELIBS)/ -e s/@VERSION@/$(DEB_VERSION)/ ffmpeg-config.in > debian/tmp/usr/bin/ffmpeg-config
	chmod 644 debian/tmp/usr/bin/ffmpeg-config
	cp ffmpeg-config.1 debian/tmp/usr/share/man/man1/ffmpeg-config.1
	
	dh_install --fail-missing --sourcedir=debian/tmp

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs doc/ffmpeg_powerpc_performance_evaluation_howto.txt doc/optimization.txt
	dh_installdocs -A CREDITS doc/TODO debian/patents.txt debian/README.Debian
	dh_installexamples -pffmpeg doc/ffserver.conf
	dh_installexamples -plibavcodec-dev libavcodec/apiexample.c
	dh_installmenu
	dh_installcron
	dh_installinfo
	dh_installmime
	dh_installchangelogs Changelog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -p libavcodec0d -V 'libavcodec0d (>= 0.cvs20060823)'
	dh_makeshlibs -p libavformat0d -V 'libavformat0d (>= 0.cvs20060823)'
	dh_makeshlibs -p libpostproc0d -V 'libpostproc0d (>= 0.cvs20060823)'
	dh_installdeb
	dh_shlibdeps -L libavcodec0d -L libpostproc0d -L libavformat0d -l debian/libavcodec0d/usr/lib:debian/libavformat0d/usr/lib:debian/libpostproc0d/usr/lib
	dh_gencontrol -- -Vlib1394-dev="$(lib1394-dev)"
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
