#!/bin/sh
# ./prepare
#
# Prepare sources obtained from GIT for   compilation. Runs GNU autoconf
# to generate all configure  files   and  optionally downloads generated
# documentation  files.  On  first  run  the  desired  handling  of  the
# documentation is stored in the  file   .doc-action.  This  file can be
# deleted to make this script ask again.
#
# This script must be run each time after updating your version from the
# master repository. It is  normally  executed   from  configure  in the
# toplevel directory.
#
# On Windows installations, this script may be run from the MsysGit bash
# shell to initialise the submodules and download the documentation.

version="`cat VERSION`"
#server="http://gollem.science.uva.nl"
#serverpath="/cgi-bin/nph-download/SWI-Prolog"
server="http://prolog.cs.vu.nl"
serverpath="/download"
serverurl="$server$serverpath"
yes=ask
tar=tar

done=false
while [ "$done" = false ]; do
  case "$1" in
    --yes)
	yes=yes
	shift
	;;
    *)
	done=true
	;;
  esac
done


findexe()
{ oldifs="$IFS"
  IFS=:
  for d in $PATH; do
    if [ -x $d/$1 ]; then
       IFS="$oldifs"
       return 0
    fi
  done
  IFS="$oldifs"
  return 1
}


confirm ()
{ if [ "$yes" = yes ]; then
    return 0
  fi

  while true; do
    printf "$1"
    read answer
    case "$answer" in
          y*)   return 0
                ;;
          n*)   return 1
                ;;
          *)
                echo "Please answer yes or no"
                ;;
    esac
  done
}


################################################################
# Fix programs
################################################################

if findexe gtar; then tar=gtar; fi;

################################################################
# Sub-modules
################################################################

if git submodule | grep '^-' > /dev/null; then
  echo "Not (all) submodules have been initialised."
  if confirm "Do you want me to initialize and download the submodules? "; then
    git submodule init
    git submodule update
  fi
fi

outofdate=`git submodule | grep '^[-+]' | awk '{print $2}'`
if [ -z "$outofdate" ]; then
  echo "All submodules are up-to-date"
else
  echo "The following submodules are not up-to-date"
  echo "   $outofdate"
  if confirm "Do you want me to run git submodule update? "; then
    git submodule update
  fi
fi


################################################################
# Documentation check and download
################################################################

download_docs()
{ doc=pl-doc-$version.tar.gz

  if ! findexe curl; then
    echo "ERROR: downloading documentation requires the curl program"
    exit 1
  fi

  printf "Downloading documentation for SWI-Prolog $version from $server ..."
  curl $serverurl/generated/$doc > $doc
  ls -l $doc
  printf "Unpacking $doc ..."
  if $tar zxf $doc; then
     rm $doc
     echo "ok"
  else
     echo "Unpack failed."
  fi
  eval_doc
}


eval_doc()
{ doc=unknown
  if [ ! -d man/Manual ]; then
    doc=absent
  else
    if [ -f doc-version ]; then
      docversion="`cat doc-version`"
      if [ "$docversion" != $version ]; then
	doc=out-of-date
      else
	doc=ok
      fi
    else
      doc=build
    fi
  fi
}

eval_doc

done=false
while [ "$done" = false ]; do
  case "$doc" in
    absent|out-of-date)
      if [ "$yes" = yes ]; then
        download_docs
      elif [ -f .doc-action ]; then
	done=true
	case "`cat .doc-action`" in
	  download)
	    download_docs
	    ;;
	  ask)
	    if confirm "Download documentation for $version from $server"; then
		download_docs
	    fi
	    ;;
	  warn)
	    ;;
	esac
      else
	echo ""
	echo "Could not find documentation.  What do you want to do?"
	echo ""
	echo "    1) Download and unpack documentation from $server"
	echo "       and do this again automatically next time"
	echo "    2) Download and unpack documentation from $server"
	echo "       and ask next time"
	echo "    3) Warn only"
	echo ""
	printf "Option? "
	read answer
	case "$answer" in
	  1) echo download > .doc-action
	     download_docs
	     done=true
	     ;;
	  2) echo ask > .doc-action
	     download_docs
	     done=true
	     ;;
	  3) echo warn > .doc-action
	     done=true
	     ;;
	  *) goto doc_again
	     ;;
	esac
      fi
      ;;
    *)
      done=true
      ;;
  esac
done

case "$doc" in
  absent)
    cat << _EOM_
WARNING: Cannot find documentation in man/Manual.  See README.git
WARNING: and README.doc for further information.
_EOM_
    ;;
  out-of-date)
    cat << _EOM_
WARNING: Documentation version ($docversion) does not match version ($version)
_EOM_
    ;;
esac

################################################################
# Configuration
################################################################

confdir()
{ if grep AC_INIT configure.in 2>&1 >/dev/null; then
    if [ -f configure -a ! configure.in -nt configure ]; then
      return
    fi
    printf "Generating configure in $1 ... "
    if grep AC_CONFIG_HEADER configure.in 2>&1 >/dev/null; then
      autoheader
    fi
    autoconf
    echo done
  fi
}


is_mingw()
{ case `uname` in
    MINGW*)
	return 0
	;;
    *)
	return 1
	;;
  esac
}

if is_mingw; then
  echo "MinGW: skipping autoconf setup"
  echo "Your kit is prepared."
else
  if findexe autoconf; then
    for f in `find . -name configure.in`; do
      d=`dirname $f`
      (cd $d && confdir $d)
    done
    echo "Your kit is prepared."
    echo "Please consult INSTALL for further instructions."
  else
    echo 'WARNING: Cannot find GNU autoconf in $PATH.'
    echo 'WARNING: configure scripts cannot be created.'
    echo 'WARNING: Please install autoconf and re-run this script.'
  fi
fi
