#!/bin/bash

usage() {
  echo "./update-meshblu-connector"
  echo " "
  echo '** Run in current directory of connector **'
  echo " "
  echo "Required Enviroment:"
  echo "   env GITHUB_RELEASE_KEY (Unencrypted Github API Token)"
  echo "   env APPVEYOR_GITHUB_RELEASE_KEY (The encrypted token from AppVeyor without 'secure: ')"
  echo " "
}

run_generator() {
  echo "* running generator"
  yo meshblu-connector
}

encrypt_travis() {
  local value="$1"
  local response="$(travis encrypt "$1")"
  echo "secure: $response"
}

update_connector() {
  echo "* updating connector"
  git pull
}

update_travis() {
  echo "* updating .travis.yml"
  local github_release_key="$1"
  local npm_secret_key="$(get_npm_key)"
  local secure_npm_secret="$(encrypt_travis "$npm_secret_key")"
  replace_in_file "./.travis.yml" "npm-email" "serveradmin@octoblu.com"
  replace_in_file "./.travis.yml" "npm-release-key" "$secure_npm_secret"
  local secure_github_key="$(encrypt_travis "$github_release_key")"
  replace_in_file "./.travis.yml" "github-release-key" "$secure_github_key"
}

update_appveyor() {
  echo "* updating appveyor.yml"
  local appveyor_github_key="$1"
  replace_in_file "./appveyor.yml" "github-release-key" "secure: $appveyor_github_key"
}

enable_travis() {
  travis sync $@ --no-interactive
  travis enable $@ --no-interactive
}

npm_check() {
  echo "* running npm-check, follow the commands to make sure everything is up to date"
  npm-check -u
}

replace_in_file() {
  local file="$1"
  local key="$2"
  local value="$(echo "$3" | sed -e 's/[\/&]/\\&/g')"
  if [ ! -f "$file" ]; then
    echo "Missing file"
    exit 1
  fi
  sed -i .bk -e "s/\[$key\]/$value/" "$file"
  if [ -f "$file.bk" ]; then
    rm "$file.bk"
  fi
}

get_npm_key() {
  cat "$HOME/.npmrc" | grep authToken | sed -e 's/.*_authToken=//'
}

panic() {
  local message=$1
  echo $message
  exit 1
}

main() {
  local github_release_key="$GITHUB_RELEASE_KEY"
  local appveyor_github_key="$APPVEYOR_GITHUB_RELEASE_KEY"

  if [ -z "$(which npm-check)" ]; then
    echo "* installing npm-check"
    npm install --global npm-check
  fi

  if [ -z "$github_release_key" ]; then
    usage
    echo 'Missing env GITHUB_RELEASE_KEY'
    exit 1
  fi

  if [ -z "$appveyor_github_key" ]; then
    usage
    echo 'Missing env APPVEYOR_GITHUB_RELEASE_KEY'
    exit 1
  fi

  if [[ "$appveyor_github_key" =~ ^secure ]]; then
    usage
    echo 'APPVEYOR_GITHUB_RELEASE_KEY must not contain "secure: "'
    exit 1
  fi

  update_connector || panic 'Failed to update connector'
  run_generator || panic 'Failed running generator'
  enable_travis || panic 'Failed travis'
  update_travis "$github_release_key" || panic 'Failed to update travis'
  update_appveyor "$appveyor_github_key" || panic 'Failed to update appveyor'
  npm_check

  echo '* Done'
  echo ' '
  echo ' '
  echo '** WAIT NOW DO THIS **'
  echo '* 1. Make sure the package.json name and repo name are prefixed meshblu-connector-'
  echo '* 2. Add Tests for the module'
  echo '* 3. Run the Module'
  echo '* 4. Remove unnecessary files in your project.'
  echo '* 5. Enable the project in appveyor (you have to do it in the ui)'
  echo '* 6. Update the schemasUrl in package.json with a valid tag (probably the version you are about to commit)'
  echo '* 7. Make sure it builds in appveyor (x86, x64) and publishes to github releases'
  echo '* 8. Make sure it builds in travis (linux x86, x64; and osx x64) and publishes to npm and github releases'
  echo '* 9. ACTUALLY TEST it with the Meshblu Connector Installer (connector-factory.octoblu.com) on all platforms'
  echo '* Probably done now'
}

main "$@"
