var gulp = require('gulp')
var yargs = require('yargs')
var del = require('del')
var chalk = require('chalk')

var standard = require('gulp-standard-bundle')
var mocha = require('gulp-mocha')
var shell = require('gulp-shell')
var cover = require('gulp-coverage')
var coveralls = require('gulp-coveralls')
var bump = require('gulp-bump')
var git = require('gulp-git')
var gitRev = require('git-rev')

var linter = standard.linter
var argv = yargs.argv

gulp.task('lint', function () {
  return gulp.src(['./gulpfile.js', './lib/**/*.js', './tests/**/*.spec.js'])
  .pipe(linter())
  .pipe(linter.reporter('default', {
    breakOnError: true
  }))
})

gulp.task('test', ['lint'], function () {
  return gulp.src('./tests/connect-sequence.spec.js')
  .pipe(mocha())
})

gulp.task('jscover', ['clean-cover'], shell.task('jscover lib lib-cov'))

gulp.task('clean-cover', function () {
  return del(['lib-cov', 'lib-ori'])
})

gulp.task('coverage', ['jscover'], function () {
  return gulp.src('tests/**/*.js', { read: false })
  .pipe(cover.instrument({ pattern: ['lib/**/*.js'], debugDirectory: 'debug' }))
  .pipe(mocha())
  .pipe(cover.gather())
  .pipe(cover.format({ reporter: 'lcov' }))
  .pipe(coveralls())
})

gulp.task('bump', function () {
  var opts = {}
  opts.type = getBumpType()
  return gulp.src('./package.json')
  .pipe(bump(opts))
  .pipe(gulp.dest('./'))
})

gulp.task('release', ['bump'], function (done) {
  gitRev.branch(function (branch) {
    if (branch !== 'master') {
      var errorMsg = 'You must be on the master branch to make a new release!\n'
      errorMsg += 'If you want to make a release candidate (RC), use the `prerelease` task instead.\n'
      errorMsg += 'Tasks `release`... Aborting.'
      console.error(chalk.red.bgWhite(errorMsg))
      return done()
    }
    var pkg = require('./package.json')
    var version = 'v' + pkg.version
    var releaseType = getBumpType()
    var commitMsg = 'Releasing ' + releaseType + ' version: ' + version
    gulp.src('./package.json')
    .pipe(git.add())
    .pipe(git.commit(commitMsg))
    .on('end', function () {
      git.tag(version, commitMsg, function (err) {
        if (err) {
          throw err
        }
        git.push('gh-sirap-group', null, {args: '--tags'}, done)
      })
    })
  })
})

gulp.task('default', ['test'])

gulp.task('watch', function () {
  gulp.watch([ './tests/**/*.js', './lib/**/*.js' ], ['test'])
})

function getBumpType () {
  return argv.patch
  ? 'patch' : (argv.minor)
  ? 'minor' : (argv.major)
  ? 'major' : (argv.prerelease)
  ? 'prerelease' : 'patch'
}

@@ -1234,20 +1234,114 @@
 nction (
+done
 ) %7B%0A
+  releaseIfHeadOnMaster(function (err) %7B%0A    if (err) %7B%0A      done()%0A      return%0A    %7D%0A  
   var op
@@ -1348,16 +1348,18 @@
 ts = %7B%7D%0A
+  
   opts.t
@@ -1380,16 +1380,18 @@
 ype()%0A  
+  
 return g
@@ -1416,16 +1416,18 @@
 .json')%0A
+  
   .pipe(
@@ -1438,16 +1438,18 @@
 (opts))%0A
+  
   .pipe(
@@ -1465,16 +1465,42 @@
 ('./'))%0A
+    .on('end', done)%0A  %7D)%0A
 %7D)%0A%0Agulp
@@ -1550,353 +1550,73 @@
 %7B%0A  
-gitRev.branch(function (branch) %7B%0A    if (branch !== 'master') %7B%0A      var errorMsg = 'You must be on the master branch to make a new release!%5Cn'%0A      errorMsg += 'If you want to make a release candidate (RC), use the %60prerelease%60 task instead.%5Cn'%0A      errorMsg += 'Tasks %60release%60... Aborting.'
+releaseIfHeadOnMaster(function (err) %7B%0A    if (err) %7B
 %0A      
-console.error(chalk.red.bgWhite(errorMsg)
+done(
 )%0A  
@@ -1625,23 +1625,16 @@
   return
- done()
 %0A    %7D%0A 
@@ -2252,16 +2252,467 @@
 '%5D)%0A%7D)%0A%0A
+function releaseIfHeadOnMaster (done) %7B%0A  gitRev.branch(function (branch) %7B%0A    if (branch !== 'master') %7B%0A      var errorMsg = 'You must be on the master branch to make a new release!%5Cn'%0A      errorMsg += 'If you want to make a release candidate (RC), use the %60prerelease%60 task instead.%5Cn'%0A      errorMsg += 'Tasks %60release%60... Aborting.'%0A      console.error(chalk.red.bgWhite(errorMsg))%0A      done(new Error())%0A      return%0A    %7D%0A    done()%0A  %7D)%0A%7D%0A%0A
 function

check if the current branch is master for both of bump and release tasks