#!/bin/sh

default()
{
  # Add paths to env (non-Travis build)
  if [ -z "$TRAVIS" ]; then
    PATH=/usr/local/bin:/usr/bin:/bin:$PATH
    export PATH
  fi

  SCRIPT=`basename $0`
  SCRIPT_DIR=`dirname $0`
  SCRIPT_DIR=`cd $SCRIPT_DIR; pwd`

  . $SCRIPT_DIR/../_env.sh
  . $SCRIPT_DIR/../_common.sh
  . $SCRIPT_DIR/_common.sh

  BUILD_DIR=$TRAVIS_BUILD_DIR
}

# npm login
#
npm_login()
{
  NPM_FILE=".npmrc"
  if [ -n "$NPM_TOKEN" -a ! -f "$NPM_FILE" ]; then
    echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > $NPM_FILE
  fi

  # Log into npm if not already logged in
  WHOAMI=`npm whoami`
  if [ "$WHOAMI" != "patternfly-build" -a -n "$NPM_USER" -a -n "$NPM_PWD" ]; then
    printf "$NPM_USER\n$NPM_PWD\n$NPM_USER@redhat.com" | npm login
    check $? "npm login failure" warn
  fi
}

# Check prerequisites before continuing
#
prereqs()
{
  merge_prereqs

  if [ ! -s "$BOWER_JSON" ]; then
    return
  fi

  # Get version generated by 'semantic-release pre'
  VERSION=`grep '"version"' $PACKAGE_JSON | \
    awk -F':' '{print $2}' | \
    sed 's|\"||g' | \
    sed 's|,||g' | \
    sed 's| *||g'`

  BOWER_VERSION=`grep '"version"' $BOWER_JSON | \
    awk -F':' '{print $2}' | \
    sed 's|\"||g' | \
    sed 's|,||g' | \
    sed 's| *||g'`

  if [ "$VERSION" != "$BOWER_VERSION" ]; then
    echo "*** The $PACKAGE_JSON and $BOWER_JSON versions differ. Do not publish!"
    exit 1
  fi
}

# Publish dist
#
# The contents of dist directory are copied back to the root directory, overriding anything in the root which is already
# in dist. Therfore, the .npmignore file is expected to list anything in the root that should not be published.
#
# Note: In order to support semantic-release, 'npm publish' must be run from the root instead of the $DIST_DIR
# directory. When publishing a sub folder, npm loses the ability to insert the correct gitHead information, which
# prevents semantic-release from working properly.
#
# See: https://github.com/npm/read-package-json/issues/66
#
publish_dist() {
  echo "*** Copying $DIST_DIR to root"
  cd $BUILD_DIR

  for FILE in `ls -1 $DIST_DIR`
  do
    rm -rf $FILE
    check $? "Remove $FILE failure"
  done

  cd $DIST_DIR
  find . | cpio -dumpv ..
  check $? "Move files failure"
}

# Publish dist (experimental)
#
# Files such as license, readme, package.json, etc. are copied to the dist directory. The npm publish is performed
# within dist.
#
publish_dist_exp() {
  echo "*** Publishing $DIST_DIR"
  cd $BUILD_DIR

  cp $PACKAGE_JSON $LICENSE $README $NPM_IGNORE $DIST_DIR
  cd $DIST_DIR

  JUNK=`grep '"name": "@' package.json`
  if [ "$?" -eq 0 ]; then
    ACCESS_FLAG="--access public"
  fi

  npm publish $ACCESS_FLAG
  check $? "npm publish failure"

  # Return to root for semantic release post step
  cd $BUILD_DIR
}

# Publish npm
#
publish_npm()
{
  echo "*** Publishing npm"
  cd $BUILD_DIR

  npm_login

  JUNK=`grep '"name": "@' package.json`
  if [ "$?" -eq 0 ]; then
    ACCESS_FLAG="--access public"
  fi

  npm publish $ACCESS_FLAG
  check $? "npm publish failure"
}

usage()
{
cat <<- EEOOFF

    This script runs 'npm publish', but also ensures $PACKAGE_JSON and $BOWER_JSON contain matching version numbers if
    applicable. If the versions do not match, the script exists with an error.

    The -d switch publishes the $DIST_DIR directory by copying its contents to the root directory. The .npmignore file
    is expected to list anything in the root that should not be published.

    The -e switch is an experimental implementation that publishes within the $DIST_DIR directory.

    Note:

    sh [-x] $SCRIPT [-h] -d|e

    Example: sh $SCRIPT -d

    OPTIONS:
    h       Display this message (default)
    d       Publish $DIST_DIR directory
    e       Publish $DIST_DIR directory (experimental)

EEOOFF
}

# main()
{
  default

  while getopts hde c; do
    case $c in
      h) usage; exit 0;;
      d) PUBLISH_DIST=1;;
      e) PUBLISH_DIST_EXP=1;;
      \?) usage; exit 1;;
    esac
  done

  prereqs

  if [ -f "$SKIP_NPM_PUBLISH_FILE" ]; then
    echo "*** Found $SKIP_NPM_PUBLISH_FILE file indicator. Do not publish!"
    exit 0
  fi

  if [ -n "$PUBLISH_DIST_EXP" ]; then
    publish_dist_exp
  elif [ -n "$PUBLISH_DIST" ]; then
    publish_dist
    publish_npm
  else
    publish_npm
  fi
}
