# Debian build recipe for postcss-rtlcss.
#
# Upstream's `build` script is
#
#   pnpm clean && rollup --config rollup.config.js --bundleConfigAsCjs &&
#   pnpm copy && pnpm modify-dts
#
# pnpm is not used to build Debian packages, so the four steps are performed
# here directly.  scripts/clean.sh is replaced by debian/clean, the rollup run
# is the one described in debian/build/rollup.config.mjs, scripts/copy.sh and
# the modify-dts step are reproduced below.

rollup --config debian/build/rollup.config.mjs

# Intermediate TypeScript output from @rollup/plugin-typescript; leave only the
# bundles upstream ships.
rm -rf dist/tsbuild dist/esm/tsbuild

# upstream scripts/copy.sh: dist/ becomes the package root and the ES module
# flavour is marked as such.
rm -rf esm
mkdir esm
cp -a dist/. ./
printf '{\n    "type": "module"\n}\n' > esm/package.json

# upstream `replace-in-file --configFile=config.replace.json`: rollup-plugin-dts
# emits the CommonJS flavour as
#
#   declare namespace postcssRTLCSS {
#       var postcss: boolean;
#   }
#
#   export { postcssRTLCSS as default };
#
# whereas the CommonJS entry point needs `declare function postcssRTLCSS(...)`
# followed by `export = postcssRTLCSS`.  The pattern below is upstream's,
# verbatim from config.replace.json (there expressed as a JavaScript RegExp).
perl -0pi -e 's/declare namespace[^}]*\}[^}]*\};/export = postcssRTLCSS/' index.d.ts
