#!/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
}

# Check prerequisites before continuing
#
prereqs()
{
  echo "*** Publishing bower to webjar"
  cd $BUILD_DIR

  merge_prereqs

  # 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'`

  if [ "$?" -eq 0 -a "$VERSION" != "0.0.0-semantically-released" ]; then
    echo "*** Found package version: $VERSION"
  else
    echo "*** The $PACKAGE_JSON version has not been updated. Do not publish!"
    exit 1
  fi
}

# Publish bower to webjar
#
# $1: Repo name
# $2: Version
publish_bower()
{
  merge_prereqs
  cd $BUILD_DIR

  # OLD
  # http://www.webjars.org/_bower/deploy?name=patternfly&version=3.18.1&channelId=e2627542-dd69-362d-8860-05704720017f
  # curl -X POST "http://www.webjars.org/_bower/deploy?name=$1&version=$2&channelId=`random_guid`"

  curl -X POST "https://www.webjars.org/deploy?webJarType=bower&nameOrUrlish=$1&version=$2"
  check $? "publish bower webjar failure"

  printf "\n"
}

# Publish npm to webjar
#
# $1: Repo name
# $2: Version
publish_npm()
{
  echo "*** Publishing npm to webjar"
  cd $BUILD_DIR

  # OLD
  # http://www.webjars.org/_npm/deploy?name=patternfly&version=3.18.1&channelId=e2627542-dd69-362d-8860-05704720017f
  # curl -X POST "http://www.webjars.org/_npm/deploy?name=$1&version=$2&channelId=`random_guid`"

  # Todo: Cannot deploy bower webjar due to non-spec-compliant licenses
  #
  # Got package info for org.webjars.bower google-code-prettify 1.0.5
  # All attempts to determine an acceptable license have been exhausted. The bower.json file did not contain a
  # spec-compliant license definition and the license could not be determined by trolling through the source repo:
  # https://github.com/spencewood/google-code-prettify.git
  # The acceptable open source software licenses are at bottom of: https://bintray.com/docs/api/
  # The provided licenses were:
  # This problem will likely need to be resolved by working with the library maintainers directly.
  #
  # Got package info for org.webjars.bower patternfly-bootstrap-combobox 1.1.7
  # All attempts to determine an acceptable license have been exhausted. The bower.json file did not contain a
  # spec-compliant license definition and the license could not be determined by trolling through the source repo:
  # https://github.com/patternfly/patternfly-bootstrap-combobox.git
  # The acceptable open source software licenses are at bottom of: https://bintray.com/docs/api/
  # The provided licenses were: Apache 2.0
  # This problem will likely need to be resolved by working with the library maintainers directly.
  #
  # Got package info for org.webjars.bower datatables-colvis 1.1.2
  # All attempts to determine an acceptable license have been exhausted. The bower.json file did not contain a
  # spec-compliant license definition and the license could not be determined by trolling through the source repo:
  # https://github.com/DataTables/ColVis.git
  # The acceptable open source software licenses are at bottom of: https://bintray.com/docs/api/
  # The provided licenses were:
  # This problem will likely need to be resolved by working with the library maintainers directly.

  # curl -X POST "https://www.webjars.org/deploy?webJarType=npm&nameOrUrlish=$1&version=$2"
  # check $? "publish npm webjar failure"
  echo "BOWER DEPLOY IS DISABLED due to non-spec-compliant licenses"

  printf "\n"
}

# Generate random GUIDs
#
# See: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript/105074#105074
random_guid()
{
  NUM1=`random_number`
  NUM2=`random_number`
  RESULT1=`expr $NUM1 + $NUM2`

  NUM1=`random_number`
  NUM2=`random_number`
  NUM3=`random_number`
  NUM4=`random_number`
  RESULT2=`expr $NUM1 + $NUM2 + $NUM3 + $NUM4`

  echo "$RESULT1-`random_number`-`random_number`-`random_number`-$RESULT2"
}

# Generate random number
#
# See: http://www.mactricksandtips.com/2012/01/generate-random-numbers-in-terminalbash.html
random_number()
{
  echo "`od -vAn -N4 -tu < /dev/urandom | head -1 | awk '{print $1}'`"
}

usage()
{
cat <<- EEOOFF

    This script will publish webjars from published npm packages.

    sh [-x] $SCRIPT [-h|n] -a|n|p|x

    Example: sh $SCRIPT -p

    OPTIONS:
    h       Display this message (default)
    a       Angular PatternFly
    p       PatternFly
    x       PatternFly NG

EEOOFF
}

# main()
{
  default

  if [ "$#" -eq 0 ]; then
    usage
    exit 1
  fi

  while getopts hapx c; do
    case $c in
      h) usage; exit 0;;
      a) REPO_NAME=$REPO_NAME_PTNFLY_ANGULAR;;
      p) REPO_NAME=$REPO_NAME_PTNFLY;;
      x) REPO_NAME=$REPO_NAME_PTNFLY_NG;;
      \?) usage; exit 1;;
    esac
  done

  prereqs
  publish_bower $REPO_NAME $VERSION
  publish_npm $REPO_NAME $VERSION
}
