die () {
    echo "$@" >&2
    exit 1
}

[ -n "$SAGE_LOCAL" ] || die 'Error: $SAGE_LOCAL not set. Source sage-env or run this script from `sage -sh`.'

# Path to "install" command
for cmd in /usr/ucb/install ginstall install; do
    [ -z "$INSTALL" ] || break
    INSTALL=`command -v $cmd 2>/dev/null`
done
[ -n "$INSTALL" ] || die 'No install program found'
echo "Using install program $INSTALL"

# Gettext is a soft build dependency (i18n)
command -v msgfmt >/dev/null 2>&1
if [ $? -ne 0 ]; then
    gettext='NO_GETTEXT=YesPlease'
else
    gettext=''
fi

cd src

# We don't want to think about Fink or Macports
export NO_FINK=1
export NO_DARWIN_PORTS=1

# Use softlinks instead of hardlinks (see Trac #19467)
export NO_INSTALL_HARDLINKS=1

# OSX Git with FSF GCC is broken, disable completely for now. See #17091
if [ "$UNAME" = "Darwin" ]; then
    export NO_OPENSSL=1
fi

# First make GIT-VERSION-FILE (we patched Makefile such that configure
# no longer depends on this, so it's safer to explicitly build this).
$MAKE GIT-VERSION-FILE

# Configure without Tcl/Tk (otherwise git *requires* Tcl/Tk).
# We keep SANE_TOOL_PATH empty, otherwise git messes with the PATH on
# some systems, leading for example to a different "make" being used.
echo "Configuring git..."
./configure --prefix="$SAGE_LOCAL" \
            --libexecdir="$SAGE_LOCAL"/libexec \
            --with-python="$SAGE_LOCAL"/bin/python \
            --without-tcltk \
            --with-sane-tool-path=
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring git."
    exit 1
fi


echo "Building git..."
$MAKE $gettext
if [ $? -ne 0 ]; then
    echo >&2 "Error building git."
    exit 1
fi


echo "Installing git..."
$MAKE -j1 INSTALL="$INSTALL" install $gettext
if [ $? -ne 0 ]; then
    echo >&2 'Error installing git.'
    exit 1
fi


