#!/usr/bin/env bash
{ set +x; } 2>/dev/null

usage() {
    echo "usage: $(basename $0) name ..." 1>&2
    [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; exit
}

[[ $1 == "-h" ]] || [[ $1 == "--help" ]] && usage "$@"

[[ $# == 0 ]] && usage

# https://www.npmjs.com/package/name

while (($#)); do
    name="$1"
    shift
    url="https://www.npmjs.com/package/$name"
    ( set -x; curl -f -I -L -sS -o /dev/null "$url" ) && echo "SKIP: $name EXISTS, $url" && continue
    path="$(mktemp -d)" || exit
    # { set -x; cd "$path" || exit; { set +x; } 2>/dev/null; }
    cd "$path" || exit
    cat <<EOF > package.json
{
  "name": "$name",
  "version": "0.0.0"
}
EOF
    ( set -x; npm publish ) || exit
    echo "$url"
done;:
