#! /bin/sh

##  genmf file for the VLC media player
##
## Copyright (C) 2005-2007 the VideoLAN team
##
##  Authors: Sam Hocevar <sam@zoy.org>
##           Rémi Denis-Courmont <rem # videolan # org>

cd $(dirname "$0")/.. || exit 1

while test "$1"
do
  dir="$1"
  modf="modules/${dir}/Modules.am"
  if test ! -f "$modf"; then
    echo "$modf does not exist!" >&2
    exit 1
  fi
  makf="modules/${dir}/Makefile.am"
  basedir="`echo "${dir}" | cut -f1 -d/`"
  mods="`sed -n -e 's/^ *SOURCES_\([^ ]*\).*/\1/p' < "${modf}" | sort | uniq | xargs`"
  plugins="`sed -n -e 's/^.*lib\([^ ]*\)_plugin\.la.*/\1/p' < "${modf}" | sort | uniq | xargs`"
  rm -f "${makf}" && cat > "${makf}" << EOF
# ${makf} automatically generated from ${modf} by $0
# DO NOT EDIT THIS FILE DIRECTLY! See Modules.am instead.

${basedir}dir = \$(pluginsdir)/${basedir}
${basedir}_LTLIBRARIES =
EXTRA_LTLIBRARIES =

EXTRA_DIST = Modules.am

include \$(top_srcdir)/modules/common.am
include \$(srcdir)/Modules.am

if MAINTAINER_MODE
\$(srcdir)/Makefile.am: \$(srcdir)/Modules.am \$(top_srcdir)/modules/genmf
	\$(AM_V_GEN)cd \\\$(top_srcdir) && \\\$(SHELL) modules/genmf ${dir}
endif
EOF
  for mod in $mods
  do
    cat >> "${makf}" << EOF

lib${mod}_plugin_la_SOURCES = \$(SOURCES_${mod})
lib${mod}_plugin_la_CPPFLAGS = \$(AM_CPPFLAGS) \$(CPPFLAGS_${mod}) \
	-DMODULE_NAME_IS_${mod}
lib${mod}_plugin_la_CFLAGS = \$(AM_CFLAGS) \$(CFLAGS_${mod})
lib${mod}_plugin_la_CXXFLAGS = \$(AM_CXXFLAGS) \$(CXXFLAGS_${mod})
lib${mod}_plugin_la_OBJCFLAGS = \$(AM_OBJCFLAGS) \$(OBJCFLAGS_${mod})
lib${mod}_plugin_la_LIBADD = \$(LIBS_${mod})
lib${mod}_plugin_la_LDFLAGS = \$(AM_LDFLAGS) -rpath '\$(${basedir}dir)' \$(LDFLAGS_${mod})
EOF
    case " ${plugins} " in
      *\ ${mod}\ *)
	;;
      *)
        cat >> "${makf}" << EOF
EXTRA_LTLIBRARIES += lib${mod}_plugin.la
${basedir}_LTLIBRARIES += \$(LTLIB${mod})
EOF
      ;;
    esac
  done

  shift
done
