#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 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.

# Script to lint a [Travis CI][1] configuration file.
#
# This script is called with the following arguments:
#
# * `$1` - absolute file path of the configuration file
#
# [1]: https://docs.travis-ci.com/user/customizing-the-build

# shellcheck disable=SC2181


# VARIABLES #

# Configuration file absolute file path:
file="$1"

# Validation URL:
url='http://lint.travis-ci.org/'


# FUNCTIONS #

# Defines an error handler.
#
# $1 - error status
on_error() {
	echo 'ERROR: An error was encountered during execution.' >&2
	exit "$1"
}

# Lints a configuration file.
#
# $1 - file path
lint() {
	# Mimic a web browser form request:
	curl \
		--silent \
		-X POST \
		--header 'Content-Type:application/x-www-form-urlencoded' \
		--data-urlencode yml@"$1" \
		"${url}" 2>&1
}

# Processes an HTML response.
#
# $1 - html
process() {
	# Raw HTML results content may look as follows:
	#
	# Success:
	#
	# ```
	# <p class="result">Hooray, your .travis.yml seems to be solid!</p>
	# ```
	#
	# Errors:
	#
	# ```
	# <ul class="result"><li>in <b class="error">os</b> section: dropping <b class="error">osx</b>, does not support <b class="error">node_js</b></li><li>value for <b class="error">addons</b> section is empty, dropping</li><li>in <b class="error">addons</b> section: unexpected key <b class="error">apt</b>, dropping</li></ul>
	# ```

	# Processing proceeds as follows:
	#
	# 1) Find the lint results.
	# 2) Replace all `<li>` elements with a temporary character `|`.
	# 3) Replace all `|` with newline characters
	# 4) Remove all HTML elements.
	# 5) Remove empty newlines.
	echo "$1" \
	| grep -o '<ul class="result">.*</ul>' \
	| sed 's/<li>/\|/g' \
	| tr '|' '\n' \
	| sed -e 's/<[^>]*>//g' \
	| awk 'NF'
}

# Prints lint errors.
#
# $1 - results
print_errors() {
	echo '' >&2
	echo 'Lint errors:' >&2
	echo '' >&2
	echo "$1" >&2
	echo '' >&2
}

# Prints success message.
print_success() {
	echo 'No lint errors found.' >&2
}

# Main execution sequence.
main() {
	local results
	local errors

	results=$(lint "${file}")
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi
	errors=$(process "${results}")
	if [[ -n "${errors}" ]]; then
		print_errors "${errors}"
		exit 1
	fi
	print_success
	exit 0
}

# Run main:
main
