#!/bin/sh

# Create Makefile.in and Makefile in a directory (containing a Makefile.am !)
# Must be run from the toplevel directory (i.e. the one containing configure)
# Saves time compared to re-running configure completely

if [ $# -ne 1 ]; then 
  echo "$0 : creates a Makefile from a Makefile.am"
  echo
  echo "Usage : $0 relativepath/Makefile"
  echo "So the argument is the file you want to create."
  echo
else
  srcdir=
  if test -f config.status && test -f configure; then
	srcdir=.
  else
    if test ! -f Makefile; then 
      echo "$0: in the current directory there is no Makefile"
      echo "you will have to run it from the top build dir."
      echo "if you do not have a Makefile there - rerun configure"
      exit
    fi

    # Find out srcdir
    srcdir=`egrep '^srcdir *=' Makefile | sed -e "s#srcdir *= *##"`
  fi

  # Handle arg with missing "/Makefile"
  relpath=$1
  if test -n "`echo $relpath | grep \/$`"; then
    relpath=`echo $relpath | sed 's/\/$//'`
  fi
  if test -z "`echo $relpath | grep 'Makefile$'`"; then 
    relpath="$relpath/Makefile"
  fi

  (  
    if cd $srcdir ; then 
     automake $relpath || exit
     if test -f admin/am_edit; then perl admin/am_edit $relpath.in ;\
     else 
       if test -f admin/automoc; then perl admin/automoc $relpath.in ; \
       else
         if test -f automoc; then perl automoc -v $relpath.in; fi
       fi
     fi
    fi
  )
  CONFIG_FILES=$relpath CONFIG_HEADERS= ./config.status
fi
