#!/bin/sh

default()
{
  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

  # Ensure we don't push to <branch>-dist-dist...
  case "$TRAVIS_BRANCH" in
    *-dist ) BRANCH_DIST=$TRAVIS_BRANCH;;
    *) BRANCH_DIST=$TRAVIS_BRANCH-dist;;
  esac
}

# Commit changes
#
commit()
{
  echo "*** Committing changes"
  cd $BUILD_DIR

  if [ -n "$COMMIT_DIST" -a -d "dist" ]; then
    git add $DIST_DIR --force
    if [ -n "SEPARATE_COMMITS" ]; then
      git commit -m "chore(release): dist updates generated by Travis build"
      check $? "git commit failure"
    fi
  fi
  if [ -n "$COMMIT_DIST_DEMO" -a -d "dist-demo" ]; then
    git add $DIST_DEMO_DIR --force
    if [ -n "SEPARATE_COMMITS" ]; then
      git commit -m "chore(release): dist-demo updates generated by Travis build"
      check $? "git commit failure"
    fi
  fi
  if [ -n "$COMMIT_CHANGES" ]; then
    # For semantic release, revert undesirable changes to package.json and package-lock
    git checkout $PACKAGE_JSON
    git checkout $PACKAGE_LOCK_JSON

    git add -A
    if [ -n "SEPARATE_COMMITS" ]; then
      git commit -m "chore(release): updates generated by Travis build"
      check $? "git commit failure" warn
    fi
  fi
  if [ -z "SEPARATE_COMMITS" ]; then
    git commit -m "chore(release): updates generated by Travis build"
    check $? "git commit failure"
  fi
}

# Push generated files to dist branch
#
push_dist()
{
  echo "*** Pushing to $BRANCH_DIST"
  cd $BUILD_DIR

  # Push to dist branch
  EXISTING=`git ls-remote --heads https://github.com/$TRAVIS_REPO_SLUG.git $BRANCH_DIST`

  # Skip merge if we're building a forked repo
  if [ -n "$EXISTING" -a -z "$REPO_FORK" ]; then
    git fetch upstream $BRANCH_DIST:$BRANCH_DIST # <remote-branch>:<local-branch>
    git checkout $BRANCH_DIST
    git merge -X theirs $TRAVIS_BRANCH-local --no-edit --ff
    check $? "git merge failure"

    git push upstream $BRANCH_DIST -v
  else
    git push upstream $TRAVIS_BRANCH-local:$BRANCH_DIST -f -v # <local-branch>:<remote-branch>
  fi
  check $? "git push failure"
}

# Check prerequisites before continuing
#
prereqs()
{
  merge_prereqs

  # Avoid creating a <branch>-dist equivalent for all branches added to the main repository
  if [ -z "$REPO_FORK" -a -z "$DIST_BRANCH" ]; then
    if [ "$TRAVIS_BRANCH" != "$RELEASE_BRANCH" -a "$TRAVIS_BRANCH" != "$RELEASE_DIST_BRANCH" \
        -a "$TRAVIS_BRANCH" != "$NEXT_BRANCH" -a "$TRAVIS_BRANCH" != "$NEXT_DIST_BRANCH" ]; then
      echo "*** Building against $TRAVIS_BRANCH and not the master or master-dist. Do not publish!"
      exit 0
    fi
  fi
}

usage()
{
cat <<- EEOOFF

    This script will commit changes and push to the master-dist branch.

    Note: Intended for use with Travis only.

    AUTH_TOKEN must be set via Travis CI.

    sh [-x] $SCRIPT [-h] -c|d|o|s

    Example: sh $SCRIPT -d

    OPTIONS:
    h       Display this message (default)
    c       Commit changes (except $PACKAGE_JSON and $PACKAGE_LOCK_JSON)
    d       Commit dist directory
    o       Commit dist-demo directory
    s       Separate commits

EEOOFF
}

# main()
{
  default

  while getopts hcdos c; do
    case $c in
      h) usage; exit 0;;
      c) COMMIT_CHANGES=1;;
      d) COMMIT_DIST=1;;
      o) COMMIT_DIST_DEMO=1;;
      s) SEPARATE_COMMITS=1;;
      \?) usage; exit 1;;
    esac
  done

  prereqs
  git_setup
  commit
  push_dist
}
