#!/usr/bin/env bash
#
# ./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.
# default submodules to pull. Use --all to get all registered submodule
# You can also create a file modules in this directory, listing the
# desired modules one-per-line
COREMODULES="bench packages/chr packages/clpqr packages/inclpr packages/jpl"
COREMODULES+=" packages/xpce packages/odbc packages/protobufs"
COREMODULES+=" packages/sgml packages/clib packages/http packages/plunit"
COREMODULES+=" packages/pldoc packages/RDF packages/semweb packages/ssl"
COREMODULES+=" packages/R packages/zlib packages/tipc packages/table"
COREMODULES+=" packages/nlp packages/cpp packages/windows packages/PDT"
COREMODULES+=" packages/utf8proc packages/archive packages/swipl-win"
version="`cat VERSION`"
server="http://www.swi-prolog.org"
serverpath="/download"
serverurl="$server$serverpath"
yes=ask
man=auto
tar=tar
# avoid surprises and printing paths.
unset CDPATH
usage()
{
cat << _EOM_
Usage: $0 [--yes] [--all] [--man]
_EOM_
exit 1
}
while [ ! -z "$1" ]; do
case "$1" in
--yes)
yes=yes
shift
;;
--all)
COREMODULES=
shift
;;
--man)
man=download
shift
;;
*)
usage
;;
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
################################################################
nothere=`git submodule status $COREMODULES | awk '/^[-]/ {print $2}'`
if [ ! -z "$nothere" ]; then
echo "The following submodules are not yet initialised"
for m in $nothere; do
echo " $m"
done
if confirm "Do you want me to run git submodule update --init? "; then
git submodule update --init $nothere
fi
fi
outofdate=`git submodule | awk '/^[+]/ {print $2}'`
if [ -z "$outofdate" ]; then
echo "All submodules are up-to-date"
else
echo "The following submodules are not up-to-date"
for m in $outofdate; do
echo " $m"
done
if confirm "Do you want me to run git submodule update? "; then
git submodule update $outofdate
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 ..."
if curl -f $serverurl/generated/$doc > $doc; then
ls -l $doc
printf "Unpacking $doc ..."
rm -f man/Manual/*.html man/Manual/*.gif
if $tar zxf $doc; then
rm $doc
echo "ok"
else
rm $doc
echo "Unpack failed."
fi
else
echo "Failed to download documentation from $serverurl/generated/$doc"
fi
eval_doc
}
eval_doc()
{ doc=unknown
if [ ! -r man/Manual/index.html ]; 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
}
if [ "$man" = download ]; then
doc=out-of-date
else
eval_doc
fi
done=false
while [ "$done" = false ]; do
case "$doc" in
absent|out-of-date)
if [ "$yes" = yes -o "$man" = download ]; 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
for dep in "$2/ac_swi_c.m4" "$2/ac_swi_noc.m4"; do
if [ -f configure -a -f "$dep" -a "$dep" -nt configure ]; then
rm configure;
fi
done
if [ -d ac -a -f configure ]; then
for dep in ac/*.m4; do
if [ -f configure -a -f "$dep" -a "$dep" -nt configure ]; then
rm configure;
fi
done
fi
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 findexe autoconf; then
for f in ./src/configure.in $(find packages -name configure.in); do
pkgdir="$(cd packages && pwd)"
d=`dirname $f`
(cd $d && confdir $d $pkgdir)
done
echo "Your kit is prepared."
echo "Please consult INSTALL for further instructions."
else
if is_mingw; then
cat << _EOM_
Detected MinGW but could not find GNU autoconf.
- If you wish to use MinGW for building SWI-Prolog, please install
autoconf and re-run this script.
- If you plan to use Microsoft MSVC for building SWI-Prolog, GNU
autoconf is not required and you may proceed with building.
_EOM_
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
gettarget()
{ while true; do
printf "$1"
read answer
case "$answer" in
[xX]64)
echo "X64"
return 0
;;
[xX]86)
echo "X86"
return 0;
;;
*)
echo "Please answer X64 (for 64-bits) or X86 (for 32-bits)"
;;
esac
done
}
if is_mingw; then
if [ -d "X64/lib" ]; then
echo
echo "Found prerequisites for X64 (64-bit) version"
elif [ -d "X86/lib" ]; then
echo
echo "Found prerequisites for X86 (32-bit) version"
else
target=`gettarget "What is your target OS (x64 or X86)? "`
if [ ! -z "$target" ]; then
echo "Running git submodule init $target ..."
git submodule init $target
echo "ok"
fi
fi
fi