#!/bin/sh

echo "Checking that the version in package.json doesn't already exist in the npm registry..."



PKG_VERSION="$(npm ls | head -1 | sed -ne 's/[^0-9]*\(\([0-9]*\.\)\{0,4\}[0-9]*[^.]\).*/\1/p')"
PKG_TRIM="$(echo $PKG_VERSION | tr -d ' ')"
VERSION="$(npm view @credsimple/run-scripts versions --json | grep "\"$PKG_TRIM\"")"

if [ -z $VERSION ]; then 
    echo "Version" $PKG_VERSION "can be used to publish a new version of this repo."
    exit 0
else
    echo "ERROR: Version" $PKG_VERSION "cannot be used to publish a new version of this repo."
    exit 1
fi
