#! /bin/sh 
#
# Script to do the configuration work required before running configure
# when starting from fresh CVS checkout, or cvs update.
# Since this is run before any auto* tool gets called there's no check for the
# used tools. Read the script to see what tools are required.
# This script should however run fine on each un*x box around...
#
# $Header: /cvsroot/lesstif/lesstif/CVSMake,v 1.38 2005/08/06 11:21:51 amai Exp $
#

echo "$0 running in `pwd`"

# Get m4 from current PATH!
M4_=`which m4`
if test -x "$M4_"
then
   M4="$M4_"
   export M4
else
   echo "No m4 processor found. Exiting"
   exit 1
fi

# Find a tool to dump HTML to ASCII
HTMLDUMP=""
lynx_=`which lynx`
if test -x "$lynx_"
then
  HTMLDUMP="$lynx_ -dump -force_html "
else
  html2text_=`which html2text`
  if test -x "$html2text_"
  then
    HTMLDUMP="$html2text_ -nobs "
  else
    links_=`which links`
    if test -x "$links_"
    then
      HTMLDUMP="$links_ -dump "
    fi
  fi
fi

if test "$HTMLDUMP" != ""
then
  $HTMLDUMP doc/www.lesstif.org/INSTALL.html >INSTALL.txt 2>/dev/null || sed -e 's/<[^<>]*>//g' doc/www.lesstif.org/INSTALL.html >Install
else
  echo "Can not create plain text documents from HTML!"
fi 

# Defaults
distflag="--ignore-deps"
distopt=" "
localmode="no"
clean="no"
cleanopt=""

while test $# -gt 0 ; do
   case "${1}" in
     nodist )
       distflag="--include-deps "
       distopt="nodist"
       echo "(Embedding dependencies)"
       shift ;;
     local )
       localmode="yes"
       echo "local mode:don't run CVSMake in subtrees"
       shift ;;
     clean )
       clean="yes"
       cleanopt="clean"
       echo "Clean up"
       shift ;;
     * )
       echo "unknown flag ${1}"
       exit 1
       shift ;;
   esac
done

# Check for some options
if test "$clean" = "yes" ; then
  if test -f Makefile ; then 
     make  maintainer-clean
  else
     echo "Already clean"
  fi
fi


# Print out some information about the system and the auto* tools
./scripts/LTsysinfo

# Clean up old files which could hurt otherwise.
# Actually this won't help if building outside the source tree ...
rm -f config.cache config.guess config.log config.status config.sub \
      libtool ltmain.sh aclocal.m4 Makefile install-sh mkinstalldirs \
      missing ylwrap

# Our 'canonical' sequence:
echo ""
echo "running libtoolize"
libtoolize --copy --force --automake

echo "running aclocal"
aclocal -I .

echo "running autoconf"
autoconf

echo "running autoheader"
autoheader

echo "running automake"
automake --add-missing --copy --force-missing --foreign $distflag

if test "$localmode" = "yes" ; then
  echo "Local mode: don't run CVSMake in subtrees"
else
  SUBDIRS='test'
  for i in $SUBDIRS
  do
      if test -x "$i/CVSMake"
      then
  	(cd $i && ./CVSMake $distopt $cleanopt)
      fi
  done
fi
