/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var logger = require( 'debug' );
var readFile = require( '@stdlib/fs/read-file' );
var extname = require( '@stdlib/utils/extname' );


// VARIABLES //

var debug = logger( 'lint:file-license-header:linter' );
var FOPTS = {
	'encoding': 'utf8'
};


// MAIN //

/**
* Lints each file in a provided file list.
*
* @private
* @param {StringArray} files - file list
* @param {Object} header - object containing license headers against which to lint
* @param {Callback} clbk - callback to invoke upon completion
* @returns {void}
*/
function lint( files, header, clbk ) {
	var total;
	var errs;
	var idx;

	total = files.length;
	idx = -1;
	errs = [];
	return next();

	/**
	* Processes the next file.
	*
	* @private
	*/
	function next() {
		idx += 1;
		debug( 'Linting file (%d of %d): %s', idx+1, total, files[ idx ] );
		readFile( files[ idx ], FOPTS, onRead );
	} // end FUNCTION next()

	/**
	* Callback invoked upon reading a file.
	*
	* @private
	* @param {(Error|null)} error - error object
	* @param {string} file - file contents
	* @returns {void}
	*/
	function onRead( error, file ) {
		var ext;
		var tmp;
		var re;
		if ( error ) {
			debug( 'Encountered an error when attempting to read file: %s. Error: %s', files[ idx ], error.message );
			return done( error );
		}
		ext = extname( files[ idx ] ).slice( 1 ); // ignore leading `.`
		re = header[ ext ] || header.default || null;
		if ( re ) {
			tmp = re.exec( file );
			if ( tmp === null ) {
				errs.push({
					'name': files[ idx ],
					'error': 'file either has a malformed license header or does not contain a license header.'
				});
			}
		}
		done();
	} // end FUNCTION onRead()

	/**
	* Callback invoked upon linting a file.
	*
	* @private
	* @param {(Error|null)} error - error object
	* @returns {void}
	*/
	function done( error ) {
		if ( error ) {
			return clbk( error );
		}
		debug( 'Finished linting file %d of %d.', idx+1, total );
		if ( idx === total-1 ) {
			return clbk( null, errs );
		}
		next();
	} // end FUNCTION done()
} // end FUNCTION lint()


// EXPORTS //

module.exports = lint;

@@ -2059,20 +2059,21 @@
 p = 
-re.exec( fil
+file.match( r
 e );
@@ -2141,16 +2141,16 @@
  idx %5D,%0A
-
 %09%09%09%09%09'er
@@ -2246,16 +2246,159 @@
 %09%09%09%09%7D);%0A
+%09%09%09%7D else if ( tmp.length %3E 1 ) %7B%0A%09%09%09%09errs.push(%7B%0A%09%09%09%09%09'name': files%5B idx %5D,%0A%09%09%09%09%09'error': 'file contains more than 1 license header.'%0A%09%09%09%09%7D);%0A
 %09%09%09%7D%0A%09%09%7D

Use `match` to test for multiple license headers