/**
 * Born out of a frustration at the number of times an extra commit
 * is needed when the repo location changes. This is especially true
 * of such 'template' repos.
 *
 * Completely hacky and designed to work with git.
 * Any usage beyond that is likely to result in pain.
 * Large dosage Ibuprofen recommended.
 *
 */
const pkg = require('../../package.json');
const chalk = require('chalk');
const gitcfg = require('parse-git-config');
const path = require('path');

/**
 *
 * 'GIt+https://blah-git.git'.replace(/git\+|\+git|\.git/gi, '')
 *  => 'https://blah-git'
 *
 *  'https://user@github.com/user/repo'.replace(/:\/\/([^@]+@)/, '://')
 *  => 'https://github.com/user/repo'
 *
 *  'https://github.com/user/repo'.replace(/:\/\/([^@]+@)/, '://')
 *  => 'https://github.com/user/repo'
 */
function sanitizeGitUrl(url) {
  return url.replace(/git\+|\+git|\.git/gi, '').replace(/:\/\/([^@]+@)/, '://');
}

function pkgRepo(pkg) {
  const type = toString.call(pkg.repository);
  const svcMap = {
    github: 'https://github.com',
    gitlab: 'https://gitlab.com',
    bitbucket: 'https://bitbucket.org'
  };
  switch (type) {
    case '[object String]':
      const parts = pkg.repository.split('/');
      switch (parts.length) {
        case 1:
          throw new Error(`Unsupportted 'package.json:repository' ${pkg.repository}`)
        case 2:
          const svcid = parts[0].split(':')
          const prop = {
            service: svcid.length === 2 ? svcid[0] : 'github',
            userid: svcid.length === 2 ? svcid[1] : svcid[0],
            repo: parts[1]
          }
          return `${svcMap[prop.service]}/${prop.userid}/${prop.repo}`
        default:
          return pkg.repository
      }
    case '[object Object]':
      return pkg.repository.url
  }
}

exports.validate = () => {

  if (!!__dirname.match(/.*node_modules.*/)) {
    // this is a dependent install, skip validation
    return;
  }

  const config = gitcfg.keys(gitcfg.sync());

  if (!(pkg && pkg.repository)) {
    console.log(
      chalk.red(
        `'package.json:repository' is not set.`
      )
    );
    process.exit(-1);
  }

  if (!(config && config.remote && config.remote.origin && config.remote.origin.url)) {
    console.log(
      chalk.red(
        `git remote url is not configured`
      )
    );
    process.exit(-1);
  }

  const repo = {
    name: path.parse(config.remote.origin.url || '').name
  };

  if (pkg.name !== repo.name) {
    console.log(
      chalk.bold.black.bgYellow(
        `CHECK PACKAGE NAME: 'package.json:${pkg.name}' !== 'repository name: ${repo.name}'`
      ));
  }

  const url = {
    pkg: sanitizeGitUrl(pkgRepo(pkg)),
    repo: sanitizeGitUrl(config.remote.origin.url)
  };

  if (url.pkg !== url.repo) {
    console.log(
      chalk.red.bold(
        `Repository name mismatch. 'package.json: ${pkgRepo(pkg)}' !== 'git config: ${config.remote.origin.url}'`
      )
    );
    process.exit(-1);
  }
}

@@ -1969,166 +1969,8 @@
 );%0A%0A
-  if (!(pkg && pkg.repository)) %7B%0A    console.log(%0A      chalk.red(%0A        %60'package.json:repository' is not set.%60%0A      )%0A    );%0A    process.exit(-1);%0A  %7D%0A%0A
   if
@@ -2082,19 +2082,22 @@
   chalk.
-red
+yellow
 (%0A      
@@ -2099,16 +2099,25 @@
        %60
+WARNING: 
 git remo
@@ -2165,140 +2165,51 @@
     
-process.exit(-1);%0A  %7D%0A%0A  const repo = %7B%0A    name: path.parse(config.remote.origin.url %7C%7C '').name
+return;
 %0A  %7D
-;
 %0A%0A  if 
+(!
 (pkg
-.name !== repo.name
+ && pkg.repository)
 ) %7B%0A
@@ -2241,22 +2241,9 @@
 alk.
-bold.black.bgY
+y
 ello
@@ -2258,147 +2258,100 @@
    %60
-CHECK PACKAGE NAME: 'package.json:$%7Bpkg.name%7D' !== 'repository name: $%7Brepo.name%7D'%60%0A      ));%0A  %7D%0A%0A  const url = %7B%0A    pkg: sanitizeGitUrl(
+WARNING: 'package.json:repository' is not set.%60%0A      )%0A    );%0A    return;%0A  %7D%0A%0A  pkg_url = 
 pkgR
@@ -2362,36 +2362,23 @@
 pkg)
-),%0A  
+;%0A
   repo
-: sanitizeGitUrl(
+_url = 
 conf
@@ -2401,13 +2401,8 @@
 .url
-)%0A  %7D
 ;%0A%0A 
@@ -2410,28 +2410,60 @@
 if (
-url.pkg !== url.
+sanitizeGitUrl(pkg_url) !== sanitizeGitUrl(
 repo
+_url)
 ) %7B%0A
@@ -2514,23 +2514,22 @@
    %60
-Repository name
+ERROR: git url
  mis
@@ -2555,25 +2555,20 @@
 n: $%7Bpkg
-Repo(pkg)
+_url
 %7D' !== '
@@ -2581,37 +2581,21 @@
 nfig: $%7B
-config.remote.origin.
+repo_
 url%7D'%60%0A 

make repo-url check less strict - convert failures to warnings