#! /bin/sh
# libtoolize (GNU libtool) 1.5a (1.1296 2003/10/21 15:03:52)
# Written by Gary V. Vaughan <gary@gnu.org>

# Copyright (C) 2003 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions.  There is NO
# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# 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 2 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.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# Usage: $progname [OPTION]...
#
# Prepare a package to use libtool.
#
# -c, --copy            copy files rather than symlinking them
#     --debug           enable verbose shell tracing
# -n, --dry-run         print commands rather than running them
# -f, --force           replace existing files
#     --help            display this message and exit
# -i, --install         copy missing auxiliary files
#     --ltdl            install libltdl in a subdirectory
#     --ltdl-tar        install the libltdl tarball
# -q, --quiet           work silently
# -v, --verbose         verbosely report processing
#     --version         print version information and exit
#
# You must `cd' to the top directory of your package before you run
# `$progname'.
#
# Report bugs to <bug-libtool@gnu.org>.

: ${CP="cp -f"}
: ${LN_S="ln -s"}
: ${MKDIR="mkdir"}
: ${RM="rm -f"}

# The name of this program:
progname=`echo "$0" | sed 's%^.*/%%'`
PROGRAM=libtoolize

# Global variables.
dry_run_opt=false
force_opt=false
install_opt=false
link_opt=:
ltdl_opt=false
ltdl_tar_opt=false
quiet_opt=false
verbose_opt=false

seen_gettext=false
seen_libtool=false
seen_ltdl=false

exit_status=0

# Locations for important files:
prefix=/usr
datadir=${prefix}/share
pkgdatadir=${datadir}/libtool
aclocaldir=${datadir}/aclocal
auxdir=
m4dir=
configure_ac=configure.in
libtool_m4="$aclocaldir/libtool.m4"
ltdl_m4="$aclocaldir/ltdl.m4"

sed_dirname='s,/[^/]*$,,'

# func_echo arg...
# Echo program name prefixed message.
func_echo ()
{
    echo $progname: ${1+"$@"}
}

# func_verbose arg...
# Echo program name prefixed message in verbose mode only.
func_verbose ()
{
    $verbose_opt && func_echo ${1+"$@"}
}

# func_error arg...
# Echo program name prefixed message to standard error.
func_error ()
{
    echo $progname: ${1+"$@"} 1>&2
}

# func_fatal_error arg...
# Echo program name prefixed message to standard error, and exit.
func_fatal_error ()
{
    func_error ${1+"$@"}
    exit 1
}

# func_fatal_help arg...
# Echo program name prefixed message to standard error, followed by
# a help hint, and exit.
func_fatal_help ()
{
    func_error ${1+"$@"}
    func_fatal_error "Try \`$progname --help' for more information."
}


# Parse options once, thoroughly.  This comes as soon as possible in
# the script to make things like `libtoolize --version' happen quickly.
{
  # sed scripts:
  my_sed_single_opt='1s/^\(..\).*$/\1/;q'
  my_sed_single_rest='1s/^..\(.*\)$/\1/;q'
  my_sed_help='/^# Usage:/,/# Report bugs to/ {
                s/^# //; s/^# *$//;
		s/\$progname/'$progname'/;
		p;
	     }; d'
  my_sed_version='/^# '$PROGRAM' (GNU /,/# warranty; / {
	        s/^# //; s/^# *$//;
	        s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/;
	        p;
	     }; d'


  while test "$#" -gt 0; do
    case $1 in
      --copy | -c)	shift; link_opt=false	;;

      --debug)		shift
			func_echo "enabling shell trace mode"
			set -x
			;;

      --dry-run | -n)	shift
			if $dry_run_opt; then :; else
			  dry_run_opt=:
			  RM="echo $RM"
			  test -n "$LN_S" && LN_S="echo $LN_S"
			  CP="echo $CP"
			  MKDIR="echo $MKDIR"
			fi
			;;

      --force | -f)	shift; force_opt=:	;;
      --install | -i)	shift; install_opt=:	;;
      --ltdl)		shift; ltdl_opt=:	;;
      --ltdl-tar)	shift; ltdl_tar_opt=:	;;
      --quiet | --automake | -q) # --automake is for 1.5 compatibility
			shift; quiet_opt=:	;;
      --verbose | -v)	shift; verbose_opt=:	;;

      -c*|-i*|-f*|-n*|-q*|-v*)
			opt=`echo "$1" |sed "$my_sed_single_opt"`
			rest=`echo "$1" |sed "$my_sed_single_rest"`
			shift
			set -- "$opt" "-$rest" ${1+"$@"}
			;;

      --version)	sed "$my_sed_version" < $0;	exit 0	;;
      --help)		sed "$my_sed_help" < $0;	exit 0	;;
      --)		shift ;;
      -*)		func_fatal_help "unrecognized option \`$1'"	;;
      *)		func_fatal_help "too many arguments"		;;
    esac
  done
}

# func_mkdir_p dir
# Make sure the entire path to DIR is available.
func_mkdir_p ()
{
    my_dir=$1
    my_dirs=

    while test ! -d "$my_dir"; do
      my_dirs="$my_dir $my_dirs"
      case $my_dir in */*) ;; *) break ;; esac
      my_dir=`echo "$my_dir" | sed "$sed_dirname"`
    done
   test ! -n "$my_dirs" || $MKDIR $my_dirs
}

# func_copy srcfile destfile
# If option `--copy' was specified, or soft-linking SRCFILE to DESTFILE fails,
# then try to copy SRCFILE to DESTFILE.
func_copy ()
{
    my_srcfile="$1"
    my_destfile="$2"
    my_return_status=1

    func_mkdir_p `echo "$my_destfile" | sed "$sed_dirname"`

    $RM "$my_destfile"
    if $link_opt && $LN_S "$my_srcfile" "$my_destfile"; then
      $quiet_opt || func_echo "copying file \`$my_destfile'"
      my_return_status=0
    elif $CP "$my_srcfile" "$my_destfile"; then
      $quiet_opt || func_echo "copying file \`$my_destfile'"
      my_return_status=0
    else
      func_error "can not copy \`$my_srcfile' to \`$my_destfile'"
      exit_status=1
    fi

    return $my_return_status
}

# func_copy_all_files srcdir destdir [glob_exclude]
# For each file in SRCDIR, if option `--copy' was specified, or soft-linking
# the file to DESTDIR fails, then try to copy the file to DESTDIR.  If
# GLOB_EXCLUDE is given, exclude any matching filenames from the copy.
func_copy_all_files ()
{
    my_srcdir="$1"
    my_destdir="$2"
    my_glob_exclude="$3"

    for my_filename in `cd "$my_srcdir" && ls`; do

      # ignore excluded filenames
      if test -n "$my_glob_exclude"; then
        eval 'case $my_filename in '$my_glob_exclude') continue ;; esac'
      fi

      if test -f "$my_srcdir/$my_filename"; then
	$force_opt || if test -f "$my_destdir/$my_filename"; then
          $quiet_opt \
	    || func_error "\`$my_destdir/$my_filename' exists: use \`--force' to overwrite"
	  continue
        fi
      else
        # Not a regular file
        continue
      fi

      func_copy "$my_srcdir/$my_filename" "$my_destdir/$my_filename"
    done
}

# func_grep expression filename
# Check whether EXPRESSION matches any line of FILENAME, without output.
func_grep ()
{
    grep "$1" "$2" >/dev/null 2>&1
}

# func_scan_files
# Trace configure.(ac|in) for use of autoconf, gettext, aclocal, automake,
# libltdl and libtool.  Possibly running some of these tools if necessary.
func_scan_files ()
{
    # Prefer configure.ac to configure.in
    test -f configure.ac && configure_ac=configure.ac
    test -f "$configure_ac" \
        || func_fatal_help "\`$configure_ac' does not exist"

    test -n "`cd $pkgdatadir && ls`" \
        || func_fatal_error "can not list files in \`$pkgdatadir'"


    # Set local variables to reflect contents of configure.ac
    my_uses_autoconf=false
    my_sed_scan_configure_ac='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
	/AC_INIT/ { s,^.*$,my_uses_autoconf=:,; p; };
	d'
    eval `sed "$my_sed_scan_configure_ac" "$configure_ac"`

    $my_uses_autoconf || {
        func_verbose "$configure_ac: not using Autoconf"
	return
    }

    # ---------------------------------------------------- #
    # Probe macro usage in configure.ac and/or aclocal.m4. #
    # ---------------------------------------------------- #

    my_sed_traces='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
    	/AC_CONFIG_AUX_DIR[^_]/  {
	    s,^.*AC_CONFIG_AUX_DIR([[ 	]*\([^])]*\).*$,auxdir=\1,; p;
        };
    	/AC_CONFIG_MACRO_DIR/   {
	     s,^.*AC_CONFIG_MACRO_DIR([[ 	]*\([^])]*\).*$,m4dir=\1,; p;
        };
	/A[CM]_PROG_LIBTOOL/ { s,^.*$,seen_libtool=:,; p; };
	/AC_LIB_LTDL/        { s,^.*$,seen_ltdl=:,; p; };
	d;'
    eval `cat aclocal.m4 "$configure_ac" | sed "$my_sed_traces"`


    # ---------------- #
    # Validate auxdir. #
    # ---------------- #

    if test -n "$auxdir"; then
      # If $configure_ac contains AC_CONFIG_AUX_DIR, check that it was
      # not given in terms of a shell variable!
      case "$auxdir" in
      *\$*)
        func_fatal_error "can not handle variables in AC_CONFIG_AUX_DIR"
        ;;
      *);;
      esac
    else
      # Try to discover auxdir the same way it is discovered by configure.
      # Note that we default to the current directory.
      for dir in . .. ../..; do
        if test -f "$dir/install-sh"; then
          auxdir=$dir
          break
        elif test -f "$dir/install.sh"; then
          auxdir="$dir"
          break
        fi
      done
    fi


    # ------------------------------ #
    # Find local m4 macro directory. #
    # ------------------------------ #

    # If AC_CONFIG_MACRO_DIR turned nothing up, we hunt for ACLOCAL_AMFLAGS
    # in `Makefile.am' for a `-I' argument.

    my_sed_aclocal_flags='/^[ 	]*ACLOCAL_[A-Z_]*FLAGS[ 	]*=/ {
	s,^[^=]*=[ 	]*\(.*\), \1,; q; }; d'
    if test ! -n "$m4dir" && test -f Makefile.am; then
      my_m4dir_is_next=false
      for arg in `sed "$my_sed_aclocal_flags" Makefile.am`; do
        if $my_m4dir_is_next; then
          m4dir="$arg"
          break
        else
          if test "X$arg" = "X-I"; then
            my_m4dir_is_next=:
          else
            my_m4dir_is_next=false
          fi
        fi
      done
    fi

    # Just use the current directory if all else fails.
    test -n "$m4dir" || m4dir=.
}

# func_included_files searchfile
# Output INCLUDEFILE if SEARCHFILE m4_includes it, else output SEARCHFILE.
func_included_files ()
{
    my_searchfile="$1"

    my_include_regex=
    my_sed_include='
        /^m4_include(\[.*\])$/ { s,^m4_include(\[\(.*\)\])$,\1,; p; };
        d'

    test -f "$my_searchfile" && echo "$my_searchfile"

    # Only recurse when we don't care if all the variables we use get
    # trashed, since they are in global scope.
    for my_filename in `sed "$my_sed_include" "$my_searchfile"`; do
      func_included_files $my_filename
    done
}

# func_serial macro_regex filename
# Output the value of the serial number comment in FILENAME, where the
# comment line must also match MACRO_REGEX.
func_serial ()
{
    my_macro_regex="$1"
    my_filename="$2"
    my_sed_serial='/^# serial [1-9][0-9]* '"$my_macro_regex"'[ 	]*$/ {
	  s,^# serial \([1-9][0-9]*\) .*$,\1,; q;
	}; d'

    # Search FILENAME and all the files it m4_includes for a serial number
    # in the file that AC_DEFUNs MACRO_REGEX.
    my_serial=
    for my_file in `func_included_files "$my_filename"`; do
      if func_grep '^AC_DEFUN(\['"$my_macro_regex" "$my_file"; then
        my_serial=`sed -e "$my_sed_serial" "$my_file"`
	break
      fi
    done

    # If the file has no serial number, we assume it's ancient.
    test -n "$my_serial" || my_serial=0

    echo $my_serial
}

# func_serial_update macro_regex srcfile destfile
# Copy SRCFILE to DESTFILE provided SRCFILE has a newer serial number, or
# DESTFILE does not yet exist, or the user specified `--force'.
func_serial_update ()
{
    my_macro_regex="$1"
    my_srcfile="$2"
    my_destfile="$3"

    my_return_status=1
    my_update_p=:

    if test -f "$my_destfile"; then
      my_src_serial=`func_serial "$my_macro_regex" "$my_srcfile"`
      my_dest_serial=`func_serial "$my_macro_regex" "$my_destfile"`

      test "$my_src_serial" -eq 0 && {
        func_error "warning: no serial number on \`$my_srcfile', not copying."
	return
      }

      # Only perform the file update if the destination has an older serial.
      test "$my_src_serial" -gt "$my_dest_serial" || my_update_p=false

      test "$my_src_serial" -gt "$my_dest_serial" \
        && func_verbose "\`$my_srcfile' is serial $my_srcserial, greater than $my_destserial in \`$my_destfile'"

      if test "$my_src_serial" -lt "$my_dest_serial"; then
        func_error "\`$my_srcfile' is serial $my_srcserial, less than $my_destserial in \`$my_destfile'"
	$force_opt \
          || func_fatal_error "Use \`--force' to replace newer libtool files with this version."
      fi
    fi

    if $my_update_p || $force_opt; then
      func_copy "$my_srcfile" "$my_destfile"
      my_return_status=$?
    else
      $quiet_opt \
        || func_echo "\`$my_destfile' is already up to date."
    fi

    # Do this after the copy for hand maintained `aclocal.m4', incase
    # it has `m4_include([DESTFILE])', so the copy effectively already
    # updated `aclocal.m4'.
    $use_aclocal || if test -f aclocal.m4; then
      test "$my_src_serial" -gt `func_serial "$my_macro_regex" aclocal.m4` \
	  && func_echo "You should add the contents of \'$my_destfile' to \`aclocal.m4'."
    fi

    return $my_return_status
}

# func_check_macros
# Sanity check macros from aclocal.m4 against installed versions.
func_check_macros ()
{
    # Don't trace for this, we're just checking the user didn't invoke it
    # directly from configure.ac.
    sed 's,dnl .*$,,; s,# .*$,,' "$configure_ac" | grep AC_PROG_RANLIB \
      && func_echo "\`AC_PROG_RANLIB' is rendered obsolete by \`AC_PROG_LIBTOOL'"

    $seen_libtool \
      || func_echo "Remember to add \`AC_PROG_LIBTOOL' to \`$configure_ac'."

    # FIXME: Ensure ltmain.sh, libtool.m4 and ltdl.m4 are from the same release
}

# func_ltmain_update srcfile destfile
# Copy SRCFILE to DESTFILE provided SRCFILE has a newer VERSION/TIMESTAMP,
# or DESTFILE does not yet exist, or the user specified `--force'.
func_ltmain_update ()
{
    my_srcfile="$1"
    my_destfile="$2"
    my_sed_ltmain='
	s,^VERSION=[^0-9]*\(.*\)[ 	]*$,\1,; t
	s,^TIMESTAMP=[^0-9]*\([.0-9]*\) .*$,\1,; t
	d'

    if test -f "$my_srcfile"; then :
    else
      func_error "\`$my_srcfile' does not exist."
      return
    fi

    # FIXME:  check versions, and only downgrade with --force
    cmp -s  "$my_srcfile" "$my_destfile"
    if test "$?" -ne 0 || $force_opt; then
      func_copy "$my_srcfile" "$my_destfile"
    else
      $quiet_opt \
        || func_echo "\`$my_destfile' is already up to date."
    fi
}

# func_config_update srcfile destfile
# Copy SRCFILE to DESTFILE provided SRCFILE has a newer TIMESTAMP,
# or DESTFILE does not yet exist, or the user specified `--force'.
func_config_update ()
{
    my_srcfile="$1"
    my_destfile="$2"
    my_sed_config='s,^timestamp=[^0-9]*\([.0-9-]*\)[^0-9].*$,\1,; t; d'

    if test -f "$my_srcfile"; then :
    else
      func_error "\`$my_srcfile' does not exist."
      return
    fi

    # FIXME:  check versions, and only downgrade with --force
    cmp -s  "$my_srcfile" "$my_destfile"
    if test "$?" -ne 0 || $force_opt; then
      func_copy "$my_srcfile" "$my_destfile"
    else
      $quiet_opt \
        || func_echo "\`$my_destfile' is already up to date."
    fi
}



## ----------- ##
##    Main.    ##
## ----------- ##

{
  rerun_aclocal=false

  func_scan_files
  $quiet_opt || func_check_macros

  # Copy all the files from installed libltdl to this project, if the
  # user specified `--with-ltdl'.
  $ltdl_opt && eval func_copy_all_files "$pkgdatadir/libltdl" libltdl

  # Copy all the files from installed libltdl to a tarball in this project,
  # if the user specified `--with-ltdl-tar'.
  $ltdl_tar_opt && {
      if test -f libltdl.tar.gz; then
        if $force_opt; then
          test -d libltdl && ${RM}r libltdl
          func_copy_all_files "$pkgdatadir/libltdl" libltdl
          tar -chof - libltdl | gzip --best > libltdl.tar.gz
          ${RM}r libltdl
        else
          func_error "\`libltdl.tar.gz' exists: use \`--force' to overwrite"
        fi
      fi
  }

  # Copy all the installed utility files to the auxiliary directory if
  # `--install' was passed, or else copy just ltmain.sh.
  $quiet_opt || if test "$auxdir" != .; then
    func_echo "putting files in AC_CONFIG_AUX_DIR, \`$auxdir'."
  fi
  if $install_opt || $force_opt; then
    func_copy_all_files "$pkgdatadir" "$auxdir" 'config.guess|config.sub|ltmain.sh'
    func_config_update "$pkgdatadir/config.guess" "$auxdir/config.guess"
    test -f "$pkgdatadir/config.sub" \
      && func_config_update "$pkgdatadir/config.sub" "$auxdir/config.sub"
  fi
  func_ltmain_update "$pkgdatadir/ltmain.sh" "$auxdir/ltmain.sh"

  # Copy libtool's m4 macros to the macro directory, if they are newer.
  $quiet_opt || if test "$m4dir" != .; then
    func_echo "putting macros in AC_CONFIG_MACRO_DIR, \`$m4dir'."
  fi
  func_serial_update 'A[CM]_PROG_LIBTOOL' "$aclocaldir/libtool.m4" "$m4dir/libtool.m4"

  if $seen_ltdl; then
    func_serial_update 'AC_LIB_LTDL' "$aclocaldir/ltdl.m4" "$m4dir/ltdl.m4"
  else
    func_verbose "Not copying \`$aclocaldir/ltdl.m4', AC_LIB_LTDL not used."
  fi
}

exit $exit_status

# Local Variables:
# mode:shell-script
# sh-indentation:2
# End:
