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

[ -n "$SAGE_LOCAL" ] || die "SAGE_LOCAL undefined, maybe run \`sage -sh\`?"

cd src

# Use newer version of config.guess and config.sub (see Trac #23710)
cp "$SAGE_ROOT"/config/config.* .

export CPPFLAGS="-I$SAGE_LOCAL/include $CPPFLAGS"
./configure --prefix="$SAGE_LOCAL" ||
    die "configuring ccache failed"

$MAKE || die "building ccache failed"

$MAKE install || die "installing ccache failed"


set -e

mkdir -p "$SAGE_LOCAL/libexec/ccache"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/cc"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/c++"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/gcc"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/g++"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/clang"
ln -sf ../../bin/ccache "$SAGE_LOCAL/libexec/ccache/clang++"

# Copy a reasonable default configuration for Sage
# (cache size of 4G and compression enabled)
cp -p ../ccache.conf "$SAGE_LOCAL/etc"
