#!/usr/bin/env bash
if [ -z "$MESON_SOURCE_ROOT" -o -z "$MESON_DIST_ROOT" ]; then
    echo >&2 "$0: Should only be run by meson"
    exit 1
fi
set -ex
if [ -L "$MESON_DIST_ROOT"/doc -o ! -d "$MESON_DIST_ROOT"/doc ]; then
    # Remove symlink copied in by meson
    rm -f "$MESON_DIST_ROOT"/doc
    # Follow symlink, copy source files.
    # - We use git here so that the exclusions in SAGE_ROOT/.gitignore apply;
    #   in particular, editor backup files and the files such as
    #   src/doc/en/reference/*/sage (which are generated based on sagelib).
    # - We can use git here because meson/mesonpy already refuses to make an sdist
    #   from something that is not a git working copy.
    (cd "$MESON_SOURCE_ROOT/doc" && git archive HEAD --prefix=doc/ .) | (cd "$MESON_DIST_ROOT" && tar -x -f - --no-same-permissions --no-acls --no-same-owner --no-xattrs)
    # Copy files that are not in the repository but generated by bootstrap.
    (cd "$MESON_SOURCE_ROOT" && tar -c -f - doc/en/reference/spkg/*.rst doc/en/installation/*.txt doc/en/reference/repl/*.txt) | (cd "$MESON_DIST_ROOT" && tar -x -f - --no-same-permissions --no-acls --no-same-owner --no-xattrs)
fi
# Copy file generated by bootstrap (not in git)
cp "$MESON_SOURCE_ROOT"/pyproject.toml "$MESON_DIST_ROOT"/
# Remove m4 source
rm -f "$MESON_DIST_ROOT"/pyproject.toml.m4
