#!/bin/bash
# This interactive script sets up your npm configuration file to use Artifactory.

NPMRC=${npm_config_userconfig:-$HOME/.npmrc}
if [ -f $NPMRC ]; then
  while true; do
    read -p "Overwrite existing npm config file $NPMRC? " yn < /dev/tty
    case $yn in
      [Yy]* ) break;;
      [Nn]* ) echo 'Okay, bye.'; exit;;
      * ) echo "Please answer yes or no.";;
    esac
  done
fi

read -p "Username: " user < /dev/tty
read -s -p "Password: " passwd < /dev/tty
AUTH=`curl --fail -s -u$user:$passwd https://artifactory.ncbi.nlm.nih.gov/artifactory/api/npm/auth`
if [ $? -ne 0 ]; then
  echo "Contacting Artifactory failed"
  exit 1
fi
echo '# NCBI Artifactory' > $NPMRC
echo 'registry=https://artifactory.ncbi.nlm.nih.gov/artifactory/api/npm/npm-virtual' >> $NPMRC
echo "$AUTH" >> $NPMRC
chmod 600 $NPMRC

