#!/bin/sh

set -e -u

if [ $# -gt 3 ] || ! pkgbase="$1"; then
  >&2 echo 'usage:'
  >&2 echo '  upload-to-bootstrap $pkgbase [$slave [$slave_host]]'
fi
slave_host="${3-nlopc46}"
slave="${2-arch32-bs0}"

tmp_file=$(mktemp 'pkgs.XXXXXXXX.tar' --tmpdir)
trap 'rm -f "${tmp_file}"' EXIT

ssh "${slave_host}" '
  ssh "'"${slave}"'" "
    cd '"$1"';
    tar -c *.pkg.tar.xz{,.sig}
  "
' > \
  "${tmp_file}"

for i in 1 2; do
  ssh bs32mirror$i '
    cd bootstrap/i486/;
    pkgs=$(
      tar -xv | \
        grep "\.xz\$"
    );
    repo-add bootstrap.db.tar.gz ${pkgs}
  ' < \
  "${tmp_file}"
done