ifeq ($(PLATFORM), AR7161)
GNU_TARGET_NAME = mips-linux-uclibc
endif

FFMPEG_DIR := ffmpeg
LIBDLNA_DIR := libdlna-0.2.3
LIBUPNP_DIR := libupnp-1.6.6
FUSE_DIR := fuse-2.7.6
LIBICONV_DIR := libiconv-1.11
GETTEXT_DIR := gettext-0.14.6
USHARE_DIR := ushare-1.1a
NTFS-3G_DIR := ntfs-3g
INSTALL_DIR := $(shell pwd)/_install

CFLAGS += -I$(INSTALL_DIR)/include
LDFLAGS += -L$(INSTALL_DIR)/lib

all: ushare
#all: ushare ntfs-3g

$(FFMPEG_DIR)/.configured:
	cd $(FFMPEG_DIR); \
	./configure \
	--prefix=/ \
	--disable-strip \
	--disable-network \
	--disable-zlib \
	--disable-vhook \
	--disable-ffmpeg \
	--disable-ffserver \
	--disable-ffplay \
	--enable-small \
	--disable-mpegaudio-hp \
	--disable-encoders \
	--disable-decoders \
	--disable-muxers \
	--disable-bsfs \
	--disable-devices \
	--enable-debug \
	--disable-protocol=udp \
	--disable-protocol=tcp \
	--disable-protocol=rtp \
	--disable-protocol=pipe \
	--disable-protocol=http \
	--disable-parser=cavsvideo \
	--disable-parser=dca \
	--disable-parser=dirac \
	--disable-parser=dnxhd \
	--disable-parser=mjpeg \
	--disable-parser=mlp \
	--disable-parser=pnm \
	--disable-parser=vp3 \
	--disable-demuxer=amr \
	--disable-demuxer=apc \
	--disable-demuxer=ape \
	--disable-demuxer=ass \
	--disable-demuxer=bethsoftvid \
	--disable-demuxer=bfi \
	--disable-demuxer=c93 \
	--disable-demuxer=daud \
	--disable-demuxer=dnxhd \
	--disable-demuxer=dsicin \
	--disable-demuxer=dxa \
	--disable-demuxer=ffm \
	--disable-demuxer=gsm \
	--disable-demuxer=gxf \
	--disable-demuxer=idcin \
	--disable-demuxer=iff \
	--disable-demuxer=image2 \
	--disable-demuxer=image2pipe \
	--disable-demuxer=ingenient \
	--disable-demuxer=ipmovie \
	--disable-demuxer=lmlm4 \
	--disable-demuxer=mm \
	--disable-demuxer=mmf \
	--disable-demuxer=msnwc_tcp \
	--disable-demuxer=mtv \
	--disable-demuxer=mxf \
	--disable-demuxer=nsv \
	--disable-demuxer=nut \
	--disable-demuxer=oma \
	--disable-demuxer=pva \
	--disable-demuxer=rawvideo \
	--disable-demuxer=rl2 \
	--disable-demuxer=roq \
	--disable-demuxer=rpl \
	--disable-demuxer=segafilm \
	--disable-demuxer=shorten \
	--disable-demuxer=siff \
	--disable-demuxer=smacker \
	--disable-demuxer=sol \
	--disable-demuxer=str \
	--disable-demuxer=thp \
	--disable-demuxer=tiertexseq \
	--disable-demuxer=tta \
	--disable-demuxer=txd \
	--disable-demuxer=vmd \
	--disable-demuxer=voc \
	--disable-demuxer=wc3 \
	--disable-demuxer=wsaud \
	--disable-demuxer=wsvqa \
	--disable-demuxer=xa \
	--disable-demuxer=yuv4mpegpipe \
	--enable-demuxer=matroska \
	--disable-armv5te \
	--disable-armv6 \
	--disable-altivec \
	--disable-vis \
	--enable-shared \
	--disable-static \
	--cross-prefix=$(CROSS_COMPILE) \
	--cross-compile \
	--arch=mips; \
	sh ./debian/fixup-config.sh >> config.h
	touch $@

$(FFMPEG_DIR)/.built: $(FFMPEG_DIR)/.configured
	$(MAKE) -C $(FFMPEG_DIR)
	touch $@

$(FFMPEG_DIR)/.installed: $(FFMPEG_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(FFMPEG_DIR) install
	touch $@

ffmpeg-install: $(FFMPEG_DIR)/.installed
	cp -dpR $(INSTALL_DIR)/lib/libavcodec.so* $(TARGET)/lib
	$(STRIP) $(TARGET)/lib/libavcodec.so*
	cp -dpR $(INSTALL_DIR)/lib/libavformat.so* $(TARGET)/lib
	$(STRIP) $(TARGET)/lib/libavformat.so*
	cp -dpR $(INSTALL_DIR)/lib/libavutil.so* $(TARGET)/lib
	$(STRIP) $(TARGET)/lib/libavutil.so*

ffmpeg: ffmpeg-install

ffmpeg-clean:
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(FFMPEG_DIR) uninstall
	$(MAKE) -C $(FFMPEG_DIR) clean
	rm -f $(FFMPEG_DIR)/.configured $(FFMPEG_DIR)/.built $(FFMPEG_DIR)/.installed

$(LIBDLNA_DIR)/.configured:
	cd $(LIBDLNA_DIR); \
	CFLAGS="$(CFLAGS) -I$(INSTALL_DIR)/include" \
	LDFLAGS="$(LDFLAGS) -L$(INSTALL_DIR)/lib -lavcodec -lavutil -lavformat" \
	./configure \
	--cross-compile \
	--cross-prefix=$(CROSS_COMPILE) \
	--prefix=/ \
	--libdir=/lib \
	--includedir=/include \
	--with-ffmpeg-dir=$(INSTALL_DIR) \
	--disable-static
	touch $@

$(LIBDLNA_DIR)/.built: $(LIBDLNA_DIR)/.configured
	$(MAKE) -C $(LIBDLNA_DIR)
	touch $@

$(LIBDLNA_DIR)/.installed: $(LIBDLNA_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBDLNA_DIR) install
	touch $@

libdlna-install: $(LIBDLNA_DIR)/.installed
	cp -dpR $(INSTALL_DIR)/lib/libdlna.so* $(TARGET)/lib
	$(STRIP) $(TARGET)/lib/libdlna.so*

libdlna: ffmpeg libdlna-install

libdlna-clean:
	-$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBDLNA_DIR) uninstall
	$(MAKE) -C $(LIBDLNA_DIR) clean
	rm -f $(LIBDLNA_DIR)/.configured $(LIBDLNA_DIR)/.built $(LIBDLNA_DIR)/.installed

$(FUSE_DIR)/.configured:
	cd $(FUSE_DIR); \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--prefix=/ \
	--exec-prefix=/ \
	--bindir=/bin \
	--sbindir=/sbin \
	--libdir=/lib \
	--libexecdir=/lib \
	--sysconfdir=/etc \
	--datadir=/usr/share \
	--localstatedir=/var \
	--includedir=/include \
	--mandir=/usr/man \
	--infodir=/usr/info \
	--enable-shared \
	--enable-static \
	--disable-rpath \
	--enable-kernel-module \
	--enable-lib \
	--enable-util \
	--disable-example \
	--disable-auto-modprobe \
	--with-kernel="$(KERNEL_PATH)" \
	--disable-mtab; \
	for i in `find . -name Makefile -type f`;do \
		sed -e "s/^INSTALL = .*/INSTALL = \/usr\/bin\/install -c/" -i $$i; \
	done;
	touch $@

$(FUSE_DIR)/.built: $(FUSE_DIR)/.configured
	$(MAKE) -C $(FUSE_DIR)
	touch $@

$(FUSE_DIR)/.installed: $(FUSE_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(FUSE_DIR) install
	touch $@

fuse-install: $(FUSE_DIR)/.installed
	cp -a $(INSTALL_DIR)/lib/libfuse.so* $(TARGET)/lib/
	cp -a $(INSTALL_DIR)/lib/libulockmgr.so* $(TARGET)/lib/
	$(STRIP) --strip-unneeded $(TARGET)/lib/libfuse.so*
	$(STRIP) --strip-unneeded $(TARGET)/lib/libulockmgr.so*
	cp -a $(INSTALL_DIR)/bin/mips-linux-uclibc-fusermount $(TARGET)/bin/fusermount
	cp -a $(INSTALL_DIR)/bin/mips-linux-uclibc-ulockmgr_server $(TARGET)/bin/ulockmgr_server
	cp -a $(INSTALL_DIR)/sbin/mount.fuse $(TARGET)/sbin/mount.fuse
	cp -a $(INSTALL_DIR)/lib/modules $(TARGET)/lib

fuse: fuse-install

fuse-clean:
	-$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(FUSE_DIR) uninstall
	$(MAKE) -C $(FUSE_DIR) clean
	rm -f $(FUSE_DIR)/.configured $(FUSE_DIR)/.built $(FUSE_DIR)/.installed

$(LIBICONV_DIR)/.configured:
	cd $(LIBICONV_DIR); \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--prefix=/ \
	--exec-prefix=/ \
	--bindir=/bin \
	--sbindir=/sbin \
	--libdir=/lib \
	--libexecdir=/lib \
	--sysconfdir=/etc \
	--datadir=/usr/share \
	--localstatedir=/var \
	--includedir=/include \
	--mandir=/usr/man \
	--infodir=/usr/info \
	--without-libiconv-prefix \
	--with-libintl-prefix=$(INSTALL_DIR) \
	--disable-nls; \
	for i in `find . -name Makefile -type f`;do \
		sed -e "s/^INSTALL = .*/INSTALL = \/usr\/bin\/install -c/" -i $$i; \
	done;
	touch $@

$(LIBICONV_DIR)/.built: $(LIBICONV_DIR)/.configured
	$(MAKE) -C $(LIBICONV_DIR)
	touch $@

$(LIBICONV_DIR)/.installed: $(LIBICONV_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBICONV_DIR) install
	touch $@

libiconv-install: $(LIBICONV_DIR)/.installed
	cp -dpR $(INSTALL_DIR)/lib/libiconv.so* $(TARGET)/lib
	cp -dpR $(INSTALL_DIR)/lib/libcharset.so* $(TARGET)/lib
	$(STRIP) $(TARGET)/lib/libiconv.so.2.4.0
	$(STRIP) $(TARGET)/lib/libcharset.so.1.0.0

libiconv: libiconv-install

libiconv-clean:
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBICONV_DIR) uninstall
	$(MAKE) -C $(LIBICONV_DIR) clean
	rm -f $(LIBICONV_DIR)/.configured $(LIBICONV_DIR)/.built $(LIBICONV_DIR)/.installed

$(GETTEXT_DIR)/.configured:
	cd $(GETTEXT_DIR); \
	LDFLAGS="$(LDFLAGS) -Wl,--rpath -Wl,/lib -liconv" \
	ac_cv_func_strtod=yes \
	ac_fsusage_space=yes \
	fu_cv_sys_stat_statfs2_bsize=yes \
	ac_cv_func_closedir_void=no \
	ac_cv_func_getloadavg=no \
	ac_cv_lib_util_getloadavg=no \
	ac_cv_lib_getloadavg_getloadavg=no \
	ac_cv_func_getgroups=yes \
	ac_cv_func_getgroups_works=yes \
	ac_cv_func_chown_works=yes \
	ac_cv_have_decl_euidaccess=no \
	ac_cv_func_euidaccess=no \
	ac_cv_have_decl_strnlen=yes \
	ac_cv_func_strnlen_working=yes \
	ac_cv_func_lstat_dereferences_slashed_symlink=yes \
	ac_cv_func_lstat_empty_string_bug=no \
	ac_cv_func_stat_empty_string_bug=no \
	vb_cv_func_rename_trailing_slash_bug=no \
	ac_cv_have_decl_nanosleep=yes \
	jm_cv_func_nanosleep_works=yes \
	gl_cv_func_working_utimes=yes \
	ac_cv_func_utime_null=yes \
	ac_cv_have_decl_strerror_r=yes \
	ac_cv_func_strerror_r_char_p=no \
	jm_cv_func_svid_putenv=yes \
	ac_cv_func_getcwd_null=yes \
	ac_cv_func_getdelim=yes \
	ac_cv_func_mkstemp=yes \
	utils_cv_func_mkstemp_limitations=no \
	utils_cv_func_mkdir_trailing_slash_bug=no \
	ac_cv_func_memcmp_working=yes \
	ac_cv_have_decl_malloc=yes \
	gl_cv_func_malloc_0_nonnull=yes \
	ac_cv_func_malloc_0_nonnull=yes \
	ac_cv_func_calloc_0_nonnull=yes \
	ac_cv_func_realloc_0_nonnull=yes \
	jm_cv_func_gettimeofday_clobber=no \
	gl_cv_func_working_readdir=yes \
	jm_ac_cv_func_link_follows_symlink=no \
	utils_cv_localtime_cache=no \
	ac_cv_struct_st_mtim_nsec=no \
	gl_cv_func_tzset_clobber=no \
	gl_cv_func_getcwd_null=yes \
	gl_cv_func_getcwd_path_max=yes \
	ac_cv_func_fnmatch_gnu=yes \
	am_getline_needs_run_time_check=no \
	am_cv_func_working_getline=yes \
	gl_cv_func_mkdir_trailing_slash_bug=no \
	gl_cv_func_mkstemp_limitations=no \
	ac_cv_func_working_mktime=yes \
	jm_cv_func_working_re_compile_pattern=yes \
	ac_use_included_regex=no \
	gl_cv_c_restrict=no \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--prefix=/ \
	--exec-prefix=/ \
	--bindir=/bin \
	--sbindir=/sbin \
	--libdir=/lib \
	--libexecdir=/lib \
	--sysconfdir=/etc \
	--datadir=/usr/share \
	--localstatedir=/var \
	--includedir=/include \
	--mandir=/usr/man \
	--infodir=/usr/info \
	--disable-libasprintf \
	--disable-static; \
	for i in `find . -name Makefile -type f`;do \
		sed -e "s/^INSTALL = .*/INSTALL = \/usr\/bin\/install -c/" -i $$i; \
	done;
	touch $@

$(GETTEXT_DIR)/.built: $(GETTEXT_DIR)/.configured
	$(MAKE) -C $(GETTEXT_DIR)
	touch $@

$(GETTEXT_DIR)/.installed: $(GETTEXT_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(GETTEXT_DIR) install
	touch $@

libintl-install: $(GETTEXT_DIR)/.installed
	cp -a $(INSTALL_DIR)/lib/libgettext*.so* $(TARGET)/lib/
	cp -a $(INSTALL_DIR)/lib/libintl*.so* $(TARGET)/lib/
	rm -f $(TARGET)/lib/libgettext*.so*.la $(TARGET)/lib/libintl*.so*.la
	$(STRIP) $(TARGET)/lib/libgettext*.so*
	$(STRIP) $(TARGET)/lib/libintl*.so*

libintl: libiconv libintl-install

libintl-clean:
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(GETTEXT_DIR) uninstall
	$(MAKE) -C $(GETTEXT_DIR) clean
	rm -f $(GETTEXT_DIR)/.configured $(GETTEXT_DIR)/.built $(GETTEXT_DIR)/.installed

$(LIBUPNP_DIR)/.configured:
	cd $(LIBUPNP_DIR); \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--prefix=/usr \
	--exec-prefix=/usr \
	--bindir=/usr/bin \
	--sbindir=/usr/sbin \
	--libdir=/lib \
	--libexecdir=/usr/lib \
	--sysconfdir=/etc \
	--datadir=/usr/share \
	--localstatedir=/var \
	--includedir=/include \
	--mandir=/usr/man \
	--infodir=/usr/info \
	--disable-static; \
	for i in `find . -name Makefile -type f`;do \
		sed -e "s/^INSTALL = .*/INSTALL = \/usr\/bin\/install -c/" -i $$i; \
	done;
	touch $@

$(LIBUPNP_DIR)/.built: $(LIBUPNP_DIR)/.configured
	$(MAKE) -C $(LIBUPNP_DIR)
	touch $@

$(LIBUPNP_DIR)/.installed: $(LIBUPNP_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBUPNP_DIR) install
	touch $@

libupnp-install: $(LIBUPNP_DIR)/.installed
	cp -a $(INSTALL_DIR)/lib/libupnp.so* $(TARGET)/lib/
	cp -a $(INSTALL_DIR)/lib/libthreadutil.so* $(TARGET)/lib/
	cp -a $(INSTALL_DIR)/lib/libixml.so* $(TARGET)/lib/
	$(STRIP) --strip-unneeded $(TARGET)/lib/libupnp.so*
	$(STRIP) --strip-unneeded $(TARGET)/lib/libthreadutil.so*
	$(STRIP) --strip-unneeded $(TARGET)/lib/libixml.so*

libupnp: libdlna libupnp-install

libupnp-clean:
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(LIBUPNP_DIR) uninstall
	$(MAKE) -C $(LIBUPNP_DIR) clean
	rm -f $(LIBUPNP_DIR)/.configured $(LIBUPNP_DIR)/.built $(LIBUPNP_DIR)/.installed

$(USHARE_DIR)/.configured:
	cd $(USHARE_DIR); \
	CFLAGS="$(CFLAGS) -I$(shell pwd)/$(USHARE_DIR)" \
	LDFLAGS="$(LDFLAGS) -lintl -liconv" \
	LIBS="$(LDFLAGS) -Wl,--rpath-link -Wl,$(INSTALL_DIR)/lib -lintl -liconv" \
	ac_cv_func_malloc_0_nonnull=yes \
	ac_cv_func_calloc_0_nonnull=yes \
	ac_cv_func_realloc_0_nonnull=yes \
	./configure \
	--cross-compile \
	--cross-prefix=$(CROSS_COMPILE) \
	--prefix=/ \
	--localedir=/usr \
	--bindir=/bin \
	--sysconfdir=/etc \
	--enable-dlna \
	--with-libupnp-dir=$(INSTALL_DIR) \
	--with-libdlna-dir=$(INSTALL_DIR)
	touch $@

$(USHARE_DIR)/.built: $(USHARE_DIR)/.configured
	$(MAKE) -C $(USHARE_DIR)
	touch $@

$(USHARE_DIR)/.installed: $(USHARE_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(USHARE_DIR) install
	touch $@

ushare-install: $(USHARE_DIR)/.installed
	cp -a $(INSTALL_DIR)/bin/ushare $(TARGET)/bin/
	$(STRIP) --strip-unneeded $(TARGET)/bin/ushare
	cp -a $(INSTALL_DIR)/etc/ushare.conf $(TARGET)/etc

ushare: fuse libiconv libintl libupnp ushare-install

ushare-clean:
	-$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(USHARE_DIR) uninstall
	$(MAKE) -C $(USHARE_DIR) clean
	rm -f $(USHARE_DIR)/.configured $(USHARE_DIR)/.built $(USHARE_DIR)/.installed

$(NTFS-3G_DIR)/.configured:
	cd $(NTFS-3G_DIR); \
	ac_cv_path_LDCONFIG="" \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--prefix=/usr \
	--libdir=/usr/lib \
	--bindir=/usr/bin \
	--sbindir=/usr/sbin \
	--libexecdir=/usr/lib \
	--sysconfdir=/etc \
	--datadir=/usr/share \
	--localstatedir=/var \
	--includedir=/usr/include \
	--program-prefix="" \
	--with-gnu-ld \
	--enable-shared; \
	for i in `find . -name Makefile -type f`;do \
		sed -e "s/^INSTALL = .*/INSTALL = \/usr\/bin\/install -c/" -i $$i; \
	done;
	touch $@

$(NTFS-3G_DIR)/.built: $(NTFS-3G_DIR)/.configured
	$(MAKE) -C $(NTFS-3G_DIR)
	touch $@

$(NTFS-3G_DIR)/.installed: $(NTFS-3G_DIR)/.built
	$(MAKE) DESTDIR=$(INSTALL_DIR) -C $(NTFS-3G_DIR) install
	touch $@

#ntfs-3g-install: $(NTFS-3G_DIR)/.installed
#	rm -f $(TARGET)/lib/libntfs-3g.so*
#	cp -dpf $(INSTALL_DIR)/lib/libntfs-3g.so* $(TARGET)/lib/
#	$(STRIP) $(TARGET)/lib/libntfs-3g.so*
#	-unlink $(TARGET)/usr/lib/libntfs-3g*
#	mkdir -p $(TARGET)/usr/lib
#	ln -sf /lib/libntfs-3g.so $(TARGET)/usr/lib/libntfs-3g.so
#	cp -dpf $(INSTALL_DIR)/bin/ntfs-3g $(TARGET)/bin/
#	cp -dpf $(INSTALL_DIR)/bin/lowntfs-3g $(TARGET)/bin/
#	cp -dpf $(INSTALL_DIR)/sbin/mount.ntfs-3g $(TARGET)/sbin/
#	cp -dpf $(INSTALL_DIR)/sbin/mount.lowntfs-3g $(TARGET)/sbin/
#	$(STRIP) $(TARGET)/bin/ntfs-3g

ntfs-3g: fuse ntfs-3g-install

install: ushare-install fuse-install libiconv-install libintl-install libupnp-install ffmpeg-install libdlna-install #ntfs-3g

clean: ffmpeg-clean libdlna-clean fuse-clean libiconv-clean libintl-clean libupnp-clean ushare-clean
	-rm -rf $(INSTALL_DIR)

.PHONY: all clean install
