RVERSION=$(cat package.json | jq -r '.version')
RMAJOR=$(echo $RVERSION | cut -d. -f1)
RMINOR=$(echo $RVERSION | cut -d. -f2)
RPATCH=$(echo $RVERSION | cut -d. -f3)

re='^[0-9]+$'

if ! [[ $RMAJOR =~ $re ]] ; then
   echo "error: Major part of the version is not a number" >&2; exit 1
fi

if ! [[ $RMINOR =~ $re ]] ; then
   echo "error: Minor part of the version is not a number" >&2; exit 1
fi

if ! [[ $RMAJOR =~ $re ]] ; then
   echo "error: Patch part of the version is not valid, must start with a number and optionally have a dash followed by an all uppercase tag" >&2; exit 1
fi

NEW_VERSION=$RMAJOR.$RMINOR.$RPATCH
sed -i "" "s/^\([[:space:]]*\"version\":[[:space:]]*\"\)[^\"]*\(\"\,\)$/\1$NEW_VERSION\2/g" package.json
zip -r roar-agent.$NEW_VERSION.zip package.json build certs scripts LICENSE CHANGELOG.md
aws s3 cp roar-agent.$NEW_VERSION.zip s3://liongard/agent/roar-agent.$NEW_VERSION.zip --acl public-read
aws s3 cp roar-agent.$NEW_VERSION.zip s3://liongard/agent/roar-agent.latest.zip --acl public-read
