#!/bin/bash

BASE="backupsitetuff"
TOTAL=10
PARALLEL=3

publish_one() {
  NAME="$1"

  echo "Building $NAME"

  node -e "
    const fs = require('fs');
    const pkg = require('./package.json');
    pkg.name = '$NAME';
    fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
  "

  npm publish --silent > /dev/null 2>&1

  echo "Published: $NAME"
}

count=1

while [ $count -le $TOTAL ]
do
  # limit parallel jobs
  while [ $(jobs -rp | wc -l | tr -d ' ') -ge $PARALLEL ]
  do
    sleep 0.1
  done

  publish_one "${BASE}${count}" &

  count=$((count + 1))
done

wait
echo "Done publishing all"