#! /bin/sh

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

if test "$#" != "0"; then
  echo "Usage: $0"
  echo "  Calls automake, autoconf, autoheader, autopoint and other auto* to generate"
  echo "  m4 macros and prepare Makefiles."
  exit 1
fi

###
###  Get a sane environment, just in case
###
LANG=C
export LANG
CYGWIN=binmode
export CYGWIN

set -e
set -x

##
## Check for various tools
##
AUTOMAKESUCKS=no
INSTALLSUCKS=no

ACLOCAL_ARGS="-I m4"

# Check for contrib directory
if test -d extras/contrib/bin; then
  export PATH=./extras/contrib/bin:$PATH
  if test -d extras/contrib/share/aclocal; then
    ACLOCAL_ARGS="${ACLOCAL_ARGS} -I extras/contrib/share/aclocal"
  fi
  if test ".`uname -s`" = ".Darwin"; then
    export LD_LIBRARY_PATH=./extras/contrib/lib:$LD_LIBRARY_PATH
    export DYLD_LIBRARY_PATH=./extras/contrib/lib:$DYLD_LIBRARY_PATH
  elif test ".`uname -s`" = ".BeOS"; then
    export LIBRARY_PATH=./extras/contrib/lib:$LIBRARY_PATH
    export BELIBRARIES=./extras/contrib/lib:$BELIBRARIES
  fi
fi

# Check for autoconf
rm -f m4/autoconf260.m4
case "$(autoconf --version|head -1)" in
  *2.5[012345678]*)
    echo "Hey, your autoconf is quite old. Update it." >&2
    exit 1
    ;;

  *2.59*)
    echo "Enabling provisional autoconf 2.59 work-around. Update autoconf ASAP."
    cp -f extras/m4/autoconf260.m4 m4/
    ;;
esac

# Check for automake
amvers="no"
for v in "-1.9" "19" "-1.8" "18" "-1.7" "17"; do
  if automake${v} --version >/dev/null 2>&1; then
    amsuff="${v}"
    amvers="${v}"
    break
  fi
done

if test "${amvers}" = "no" && automake --version > /dev/null 2>&1; then
  amvers="`automake --version | sed -e '1s/[^0-9]*//' -e q`"
  amsuff=""
fi

case "${amvers}" in
  no|1.[0123456]|1.[0123456].*|1[0123456])
    set +x
    echo "$0: you need automake version 1.7 or later"
    exit 1
    ;;
esac

# Check for libtool
libtoolize="no"
if glibtoolize --version >/dev/null 2>&1; then
  libtoolize="glibtoolize"
elif libtoolize --version >/dev/null 2>&1; then
  libtoolize="libtoolize"
fi

if test "$libtoolize" = "no"; then
  set +x
  echo "$0: you need libtool"
  exit 1
fi

# Check for gettext
if gettextize --version >/dev/null 2>&1; then
# Autopoint is available from 0.11.3, but we need 0.11.5
if expr `gettextize --version | sed -e '1s/[^0-9]*//' -e q` \
        '>=' 0.11.5 >/dev/null 2>&1; then
  # We have gettext, and a recent version! Everything is cool.
  autopoint=autopoint
  GETTEXT=yes
else
  # User's gettext is too old. try to continue anyway.
  autopoint=:
  GETTEXT=old
fi;else
  set +x
  echo "you need gettextize (package gettext-devel or gettext)"
  exit 1
fi

# Check for pkg-config
if pkg-config --version >/dev/null 2>&1; then
  # We have pkg-config, everything is cool.
  PKGCONFIG=yes
else
  PKGCONFIG=no
fi

aclocal=aclocal${amvers}
automake=automake${amvers}
autoconf=autoconf
autoheader=autoheader

##
## Generate the modules makefile, by parsing modules/**/Modules.am
##

set +x
echo "generating modules/**/Makefile.am and m4/private.m4"

# Prepare m4/private.m4
rm -f m4/private.m4 && cat > m4/private.m4 << EOF
dnl  Private VLC macros - generated by bootstrap

EOF

if [ "${PKGCONFIG}" = "no" ]; then cat >> m4/private.m4 << EOF
dnl  User does not have pkg-config, so assume package was not found
AC_DEFUN([PKG_CHECK_MODULES],[ifelse([\$4], , :, [\$4])])

EOF
fi

if [ "${GETTEXT}" != "yes" ]; then cat >> m4/private.m4 << EOF
dnl  User does not have gettext, so this is a no-op
AC_DEFUN([AM_GNU_GETTEXT_VERSION], [])

EOF
fi

rm -f m4/private.m4-tmp1 && cat > m4/private.m4-tmp1 << EOF
dnl  The required AM_CONDITIONAL calls
dnl  XXX: too many conditionals make the build very slow, disabled them
AC_DEFUN([VLC_CONDITIONALS], [
EOF
rm -f m4/private.m4-tmp2 && cat > m4/private.m4-tmp2 << EOF
dnl  The required AC_SUBST calls
AC_DEFUN([VLC_SUBSTS], [
EOF
rm -f m4/private.m4-tmp3 && cat > m4/private.m4-tmp3 << EOF
dnl  The required AC_OUTPUT calls
dnl  XXX: this feature is only supported starting from automake-1.7
AC_DEFUN([VLC_MAKEFILES], [AC_OUTPUT([
EOF
rm -f m4/private.m4-tmp4 && cat > m4/private.m4-tmp4 << EOF
dnl  Helper macro for vlc-config generation
AC_DEFUN([VLC_CONFIG_HELPER], [
  cat >> vlc-config.in << BLAH
EOF

modules=""

rm -f modules/Makefile.am && cat > modules/Makefile.am << EOF
# Autogenerated by bootstrap - DO NOT EDIT
EXTRA_DIST = LIST
SUBDIRS = `sed -ne 's,modules/\([^/]*\)/Makefile,\1,p' configure.ac | xargs`
EOF

for dir in `sed -ne 's,modules/\(.*\)/Makefile,\1,p' configure.ac`
do
  printf "."
  modf="modules/${dir}/Modules.am"
  makf="modules/${dir}/Makefile.am"
  basedir="`echo "${dir}" | cut -f1 -d/`"
  # automake will not recurse for make dist if we don't define SUBDIRS = .
  subdirs="`sed -ne 's,'modules/${dir}'/\([^/]*\)/Makefile,\1,p' configure.ac | xargs`"
  mods="`sed -n -e 's/^ *SOURCES_\([^ ]*\).*/\1/p' < "${modf}" | xargs`"
  extra_libs=""
  extra_ltlibs=""
  for mod in $mods
  do
    extra_libs="${extra_libs} lib${mod}_plugin.a lib${mod}.a"
    extra_ltlibs="${extra_ltlibs} lib${mod}_plugin.la"
  done
  rm -f "${makf}" && cat > "${makf}" << EOF

# ${makf} automatically generated from ${modf} by bootstrap
# DO NOT EDIT - edit Modules.am or \$(top_srcdir)/bootstrap instead

basedir = ${basedir}
mods = ${mods}

NULL =
libvlc_LTLIBRARIES =
noinst_LTLIBRARIES =
EXTRA_DIST = Modules.am
BUILT_SOURCES =
clean_modules =
SUBDIRS = ${subdirs}
SUFFIXES = _plugin\$(LIBEXT) _plugin.a

libvlcdir = \$(libdir)/vlc/\$(basedir)

EXTRA_LIBRARIES = ${extra_libs}
EXTRA_LTLIBRARIES = ${extra_ltlibs}

include Modules.am

LTLIBVLC = \$(top_builddir)/src/libvlc.la

if BUILD_SHARED
LIBVLC = -L\$(top_builddir)/src -lvlc
LIBADD = \$(LIBVLC)
endif

all: all-modules

nice:
	\$(top_builddir)/compile

# Find out which modules were enabled and tell make to build them
all-modules:
if USE_LIBTOOL
	@set fnord \$\$MAKEFLAGS; amf=\$\$2; targets=\`\\
	if test "\$(plugin)" != "no"; then z=\$\$(\$(VLC_CONFIG) --list builtin plugin); for mod in \$(mods); do case "\$\$z " in *\ \$\${mod}\ *) echo lib\$\${mod}_plugin.la;; esac; done; fi; \\
	\`; case "\$\$targets" in *lib*) \$(MAKE) \$(AM_MAKEFLAGS) \$\$targets || case "\$\$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; ;; esac; \\
	test -z "\$\$fail"
else
	@set fnord \$\$MAKEFLAGS; amf=\$\$2; targets=\`\\
	if test "\$(plugin)" != "no"; then z=\$\$(\$(VLC_CONFIG) --list plugin); for mod in \$(mods); do case "\$\$z " in *\ \$\${mod}\ *) echo lib\$\${mod}_plugin\$(LIBEXT);; esac; done; fi; \\
	if test "\$(builtin)" != "no"; then z=\$\$(\$(VLC_CONFIG) --list builtin); for mod in \$(mods); do case "\$\$z " in *\ \$\${mod}\ *) echo lib\$\${mod}.a;; esac; done; fi; \\
	\`; case "\$\$targets" in *lib*) \$(MAKE) \$(AM_MAKEFLAGS) \$\$targets || case "\$\$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; ;; esac; \\
	test -z "\$\$fail"
endif

# Build a plugin with the adequate linker and linker's flags
_plugin.a_plugin\$(LIBEXT):
	@mod="\$*" ; mod=\$\${mod#lib} ; \
	ldfl="\`\$(VLC_CONFIG) --libs plugin \$\$mod\` \$(LIBADD) -u \$(SYMPREF)\$(VLC_ENTRY)" ; \
	case \`\$(VLC_CONFIG) --linkage \$\$mod\` in \\
	  c++)  ld="\$(CXXLINK)" ;; \
	  objc) ld="\$(OBJCLINK)" ;; \
	  c|*)  ld="\$(LINK)" ;; \
	esac ; \
	echo \$\$ld \$< \$\$ldfl ; \
	\$\$ld \$< \$\$ldfl
#ifneq (,\$(findstring cygwin,\$(host)))
#	mv -f "\$@.exe" "\$@"
#endif
	@if test -f "\$@.exe"; then mv -f "\$@.exe" "\$@"; fi

if MAINTAINER_MODE
\$(srcdir)/Makefile.am: \$(srcdir)/Modules.am \$(top_srcdir)/bootstrap
	cd \$(top_srcdir) && \$(SHELL) ./bootstrap
endif

mostlyclean-local:
if USE_LIBTOOL
	-rm -f *.la
else
	-rm -f *.a *\$(LIBEXT)
endif

clean-local: \$(clean_modules)

### automake creates libvlcdir after running install-*-local
### so we have to create it ourselves first
install-exec-local: all-modules
if USE_LIBTOOL
	@if test -z "\$(libvlc_LTLIBRARIES)"; then \
	  z=\$\$(\$(VLC_CONFIG) --list builtin plugin); \
	  m=\`for mod in \$(mods); do case "\$\$z " in *\ \$\${mod}\ *) echo -n " lib\$\${mod}_plugin.la" ;; esac; done\` ; \
	  test -z "\$\$m" || \
  	    \$(MAKE) \$(AM_MAKEFLAGS) libvlc_LTLIBRARIES="\$\$m" install-libvlcLTLIBRARIES || exit \$\$? ; \
	fi
else
	mkdir -p -- "\$(DESTDIR)\$(libvlcdir)"
	@z=\$\$(\$(VLC_CONFIG) --list plugin); \
	for mod in \$(mods); do \
	  case "\$\$z " \
	    in *\ \$\${mod}\ *) \
	      echo \$(INSTALL_PROGRAM) "lib\$\${mod}_plugin\$(LIBEXT)" "\$(DESTDIR)\$(libvlcdir)/" ; \
	      \$(INSTALL_PROGRAM) "lib\$\${mod}_plugin\$(LIBEXT)" "\$(DESTDIR)\$(libvlcdir)/" || exit \$\$?; \
	      ;; \
	  esac; \
	done
endif

uninstall-local:
if USE_LIBTOOL
	@if test -z "\$(libvlc_LTLIBRARIES)"; then \
	  z=\$\$(\$(VLC_CONFIG) --list builtin plugin); \
	  m=\`for mod in \$(mods); do case "\$\$z " in *\ \$\${mod}\ *) echo -n " lib\$\${mod}_plugin.la" ;; esac; done\` ; \
	  test -z "\$\$m" || \
	    \$(MAKE) \$(AM_MAKEFLAGS) libvlc_LTLIBRARIES="\$\$m" uninstall-libvlcLTLIBRARIES || exit \$\$?; \
	fi
else
	@z=\$\$(\$(VLC_CONFIG) --list plugin); \
	for mod in \$(mods); do \
	  case "\$\$z " \
	    in *\ \$\${mod}\ *) \
	      echo rm -f "\$(DESTDIR)\$(libvlcdir)/lib\$\${mod}_plugin\$(LIBEXT)" ; \
	      rm -f "\$(DESTDIR)\$(libvlcdir)/lib\$\${mod}_plugin\$(LIBEXT)" || true; \
	      ;; \
	  esac; \
	done
endif

EOF
  for mod in $mods
  do
    if grep '^nodist_SOURCES_'${mod}'' < "${modf}" >/dev/null 2>&1; then
        NODIST=''; else
        NODIST='#'; fi
    cat >> m4/private.m4-tmp4 << EOF
    ${mod}) list="\\\${list} ${dir}/lib${mod}" ;;
EOF
# Generation of modules/**/Makefile.am
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# - L_ is for LIBRARIES_, D_ for DATA_, B_ for BUILT_SOURCES_, F_ for LDFLAGS_,
#   S_ for SOURCES_, _p is for _plugin, _b is for _builtin. This is to reduce
#   the resulting file size.
# - *_CFLAGS, *_CXXFLAGS etc. because per-object CPPFLAGS does not seem to
#   work properly with any automake version I tested.
    cat >> "${makf}" << EOF
# The ${mod} plugin

EOF
    if [ "${NODIST}" != "#" ]; then cat >> "${makf}" << EOF
clean_modules += clean-${mod}
clean-${mod}:
	-test -z "\$(nodist_SOURCES_${mod})" || rm -f \$(nodist_SOURCES_${mod})

EOF
    fi
    if [ "${NODIST}" != "#" ]; then cat >> "${makf}" << EOF
BUILT_SOURCES += \$(B${mod})
EOF
    fi
    cat >> "${makf}" << EOF
lib${mod}_plugin_a_SOURCES = \$(SOURCES_${mod})
lib${mod}_plugin_la_SOURCES = \$(SOURCES_${mod})
EOF
    if [ "${NODIST}" != "#" ]; then cat >> "${makf}" << EOF
nodist_lib${mod}_plugin_a_SOURCES = \$(nodist_SOURCES_${mod})
nodist_lib${mod}_plugin_la_SOURCES = \$(nodist_SOURCES_${mod})
EOF
    fi
    cat >> "${makf}" << EOF
lib${mod}_plugin_a_CFLAGS = \`\$(VLC_CONFIG) --cflags plugin ${mod}\`
lib${mod}_plugin_a_CXXFLAGS = \`\$(VLC_CONFIG) --cxxflags plugin ${mod}\`
lib${mod}_plugin_a_OBJCFLAGS = \`\$(VLC_CONFIG) --objcflags plugin ${mod}\`
lib${mod}_plugin_la_CFLAGS = \`\$(VLC_CONFIG) --cflags plugin ${mod}\`
lib${mod}_plugin_la_CXXFLAGS = \`\$(VLC_CONFIG) --cxxflags plugin ${mod}\`
lib${mod}_plugin_la_OBJCFLAGS = \`\$(VLC_CONFIG) --objcflags plugin ${mod}\`
lib${mod}_plugin_la_LDFLAGS = \`\$(VLC_CONFIG) --libs plugin ${mod}\` \\
	-rpath '\$(libvlcdir)' -avoid-version -module -shrext \$(LIBEXT) \\
	-export-symbol-regex ^\$(VLC_ENTRY)\$\$
lib${mod}_plugin_la_LIBADD = \$(LTLIBVLC)

lib${mod}_a_SOURCES = \$(SOURCES_${mod})
EOF
    if [ "${NODIST}" != "#" ]; then cat >> "${makf}" << EOF
nodist_lib${mod}_a_SOURCES = \$(nodist_SOURCES_${mod})
EOF
    fi
    cat >> "${makf}" << EOF
lib${mod}_a_CFLAGS = \`\$(VLC_CONFIG) --cflags builtin pic ${mod}\`
lib${mod}_a_CXXFLAGS = \`\$(VLC_CONFIG) --cxxflags builtin pic ${mod}\`
lib${mod}_a_OBJCFLAGS = \`\$(VLC_CONFIG) --objcflags builtin pic ${mod}\`

EOF
  done
done

cat >> m4/private.m4-tmp1 << EOF
])
EOF
cat m4/private.m4-tmp1 >> m4/private.m4 && rm -f m4/private.m4-tmp1
cat >> m4/private.m4-tmp2 << EOF
])
EOF
cat m4/private.m4-tmp2 >> m4/private.m4 && rm -f m4/private.m4-tmp2
cat >> m4/private.m4-tmp3 << EOF
])])
EOF
cat m4/private.m4-tmp3 >> m4/private.m4 && rm -f m4/private.m4-tmp3
cat >> m4/private.m4-tmp4 << EOF
BLAH
])
EOF
cat m4/private.m4-tmp4 >> m4/private.m4 && rm -f m4/private.m4-tmp4

echo " done."

###
###  classic bootstrap stuff
###
set -x

# remove autotools cruft
rm -f aclocal.m4 configure config.log config.h config.h.in
rm -Rf autom4te*.cache
# remove old autotools extra cruft
rm -f config.guess config.sub missing mkinstalldirs compile depcomp install-sh
# remove new autotools extra cruft
rm -Rf autotools
mkdir autotools
# remove libtool cruft
rm -f ltmain.sh libtool ltconfig
# remove gettext cruft
rm -f ABOUT-NLS
rm -Rf intl
# remove vlc cruft
rm -f stamp-builtin stamp-h* mozilla/stamp-pic

# Automake complains if these are not present
rm -f vlc-config.in && printf "" > vlc-config.in
if [ "$GETTEXT" != "yes" ]; then
  test -d intl || mkdir intl
  printf "" > intl/Makefile.am
  printf "" > ABOUT-NLS
fi

# Libtoolize directory
${libtoolize} --copy --force
if test -f "ltmain.sh"; then
  echo "$0: working around a minor libtool issue"
  mv ltmain.sh autotools/
fi

# Do the rest
${autopoint} -f
${aclocal} ${ACLOCAL_ARGS}
${autoconf}
${autoheader}
${automake} --add-missing --copy -Wall

##
##  files which need to be regenerated
##
rm -f vlc-config.in vlc-config
rm -f src/misc/modules_builtin.h

# Shut up
set +x

##
##  Tell the user about gettext, pkg-config and sed
##
if [ "${GETTEXT}" = "old" ]; then
  cat << EOF

==========================================================
NOTE: you have an old version of gettext installed on your
system. The vlc build will work, but if your system does not
have libintl you will not have internationalization support.
We suggest upgrading to gettext 0.11.5 or later.
EOF
fi

if [ "$PKGCONFIG" = "no" ]; then
  cat << EOF

==============================================================
NOTE: you do not have the "pkg-config" utility on your system;
detection of the Gtk-2.0 and GNOME 2.0 libraries will not be
reliable.
EOF
fi