PLUGIN=$1
ENV=$2

export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_REGION=us-east-1

# Start of validation
if [ ! -d "$PLUGIN" ] && [ ! -f "$PLUGIN" ] ; then
  echo ERROR - the first param must be a dir or a dir/file.
  echo "Usage: insert.sh fileOrDir env(q,s,p)"
  exit 1
fi

if [ "$ENV" != "q" ] && [ "$ENV" != "s" ] && [ "$ENV" != "p" ]; then
  echo ERROR - env param is missing.
  echo "Usage: insert.sh fileOrDir env(q,s,p)"
  exit 1
fi
# End of validation

# Now get to the work.  If a directory was provided then upsert all versions in the dir.
if [ -d "$PLUGIN" ]; then
  for FILE in $PLUGIN/*
  do
    if [[ "$FILE" == *.json ]]; then
      echo Inserting `pwd`/$FILE
      node ../src/sns.js plugin --env $ENV --packageJson `pwd`/$FILE
    else
      echo "Skipping $FILE because it doesn't end with .json"
    fi
  done

# Otherwise insert only the specified version.
else
  echo "Inserting `pwd`/$PLUGIN"
  node ../src/sns.js plugin --env $ENV --packageJson `pwd`/$PLUGIN
fi
