#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -e

path="$(dirname "$0")"

export PACKAGE_VERSION=$1
export release_branch=$2

# make env available for scripts
rollup_git_url=$(cat $path/../../package.json | grep rollup-umd.git | awk -F ': "' '{print $2}' | awk -F '"' '{print $1}' | grep git)

export COMPANY=${COMPANY:-"Yeutech Company Limited"}
export YEAR=${YEAR:-$(date +%Y)}

# set perms
chmod +x ${path}/*

initial_process_wd=$PWD

# display tools version
npx rollup-umd-scripts --version


function remove_rollup() {
  # remove rollup
  rm declination.json rollup.config.js
  npm uninstall --save-dev \
    rollup \
    rollup-plugin-babel \
    rollup-plugin-cleanup \
    rollup-plugin-commonjs \
    rollup-plugin-inject \
    rollup-plugin-json \
    rollup-plugin-node-builtins \
    rollup-plugin-node-resolve \
    rollup-plugin-replace \
    rollup-plugin-uglify \
    rollup-plugin-visualizer \
    rollup-watch
  sed -i '/"prebuild:dist":/d' package.json
  sed -i '/"build:dist":/d' package.json
  sed -i '/"build:dist:dev":/d' package.json
  sed -i '/"build:dist:watch":/d' package.json
  sed -i '/"build":/c\\ \ \ \ \"build": "npm run build:lib",' package.json
  sed -i '/"build:clean":/c\\ \ \ \ \"build:clean": "rimraf lib/*",' package.json
  sed -i '/"lib",/c\    "lib"\' package.json
  sed -i '/"dist"/d' package.json
  sed -i '/"rollup",/d' package.json
  sed -i '/"jsnext:main": /d' package.json
  sed -i '/"module": /d' package.json
  json=$(mktemp); jq 'del(.browserslist)' package.json > "$json"; mv "$json" package.json
}

export -f remove_rollup

# loop in order declination scripts
for f in ${path}/*; do
  if [[ "${f}" != */create ]]; then
    baseName=$(basename "$f")
    declination=${baseName#*-}
    target_name=rollup-${declination}
    target_dir=${initial_process_wd}/${target_name}
    export PREVIOUS_VERSION=${PREVIOUS_VERSION:=$PACKAGE_VERSION}
    # only in CI master we clone
    if [[ -n "$CI" ]] && [[ "$CI_COMMIT_REF_NAME" = $release_branch ]]; then
      # Foreach declination clone
      git clone "${rollup_git_url/#ssh:/git+ssh:}" "${target_name}" || echo "${target_name} already cloned";
      cd ${target_dir}
    fi

    # only in CI master we are in cloned directory
    if [[ -n "$CI" ]] && [[ "$CI_COMMIT_REF_NAME" = $release_branch ]]; then
      echo "[Release declination] Releasing from $PREVIOUS_VERSION to ${PACKAGE_VERSION}-${declination} using script: $(basename $f)"
      ${f}
    fi

    # build and test
    if [[ "$(npm -v)" = 5.6.0 ]]; then npm install; fi # fix bug with npm 5.6.0
    npm run build
    npm run styleguide:build
    npm run test

    # if passed in CI do tagging
    if [[ -n "$CI" ]] && [[ "$CI_COMMIT_REF_NAME" = $release_branch ]]; then
      tmpBranch=${PACKAGE_VERSION}-${declination}
      git checkout -b ${tmpBranch}
      git add -A
      git commit -m "feat(${tmpBranch}): Create declination ${tmpBranch} from rollup-umd ${PACKAGE_VERSION} [skip ci]"
      git push origin ${tmpBranch} || echo "[Release declination] Already tagged"
      git tag ${PACKAGE_VERSION}-${declination} || echo "[Release declination] skipping..."
      git push --tags || echo "[Release declination] skipping..."
      git reset HEAD --hard
      git checkout ${release_branch}
      git branch -D ${tmpBranch} || echo "[Release declination] skipping..."
      git push --delete origin refs/heads/${tmpBranch} || echo "[Release declination] skipping..."
      cd ${initial_process_wd}
    fi
    export PREVIOUS_VERSION=${PACKAGE_VERSION}-${declination}
  fi
done

