#!/bin/sh

set -e

VERSION=$1
BASE=$2

mmdebstrap --variant=buildd unstable \
  --components="main contrib non-free non-free-firmware" \
  --customize-hook='chroot "$1" mkdir /apt-ocaml' \
  --customize-hook="copy-in $BASE/usr/lib/ocaml.list /etc/apt/sources.list.d" \
  --customize-hook="copy-in $BASE/usr/lib/ocaml.pref /etc/apt/preferences.d" \
  "$BASE/build/ocaml-$VERSION.tar.zst" http://deb.debian.org/debian

cat > $BASE/sbuild.conf <<EOF
\$chroot_mode = 'unshare';
\$chroot = '$BASE/build/ocaml-$VERSION.tar.zst';
\$unshare_bind_mounts = [ { directory => "$BASE/pool", mountpoint => "/apt-ocaml" } ];
\$run_lintian = 0;
EOF
