#!/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.

# Converts package README files to HTML.
#
# This script is called with the following arguments:
#
# * `$1` - destination directory


# VARIABLES #

# Destination directory:
output_dir="$1"
if [[ -z "${output_dir}" ]]; then
	echo 'ERROR: Must provide a destination directory.' >&2
	exit 1
fi

# Determine root directory:
root_dir="$(git rev-parse --show-toplevel)"

# Define the path to a utility for finding packages:
find_packages="${root_dir}/tools/scripts/find_packages"

# Define the path to the source library:
source_dir="${root_dir}/lib/node_modules"

# Define the path to a utility for converting Markdown to HTML:
to_html="${source_dir}/@stdlib/_tools/markdown/to-html/bin/cli"


# FUNCTIONS #

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

# Runs clean-up tasks.
cleanup() {
	echo ''
}

# Creates a directory.
#
# $1 - directory path
create_dir() {
	mkdir -p "$1"
}

# Prints a success message.
print_success() {
	echo 'Success!' >&2
}

# Extracts a package name from an absolute file path.
#
# $1 - file path
package_name() {
	local name

	# Remove the source directory path from the package path (using POSIX shell variable expansion):
	if [[ "$1" == "${root_dir}" ]]; then
		name='@stdlib/stdlib'
	else
		name="${1#${source_dir}/}"
	fi
	echo "${name}"
}

# Converts READMEs to HTML.
#
# $1 - packages
convert_files() {
	local files
	local dest
	local name
	local src

	echo 'Converting README files...' >&2
	echo '' >&2

	for pkg in $(echo "$1"); do
		echo "Package: ${pkg}" >&2

		src="${pkg}/README.md"
		name=$(package_name "${pkg}")

		if [[ -f "${src}" ]]; then
			dest="${output_dir}/${name}"
			create_dir "${dest}"

			convert_file "${src}" "${dest}/index.html"
			if [[ "$?" -ne 0 ]]; then
				echo "Encountered an error when converting package README: ${pkg}.". >&2
				return 1
			fi
			echo 'Successfully converted package README.' >&2
		else
			echo 'No README. Skipping package.' >&2
		fi
		echo '' >&2
	done
	echo 'Finished converting README files.' >&2
	return 0
}

# Converts a README to HTML.
#
# $1 - source file
# $2 - destination
convert_file() {
	cat "$1" | "${to_html}" > "$2"
}

# Main execution sequence:
main() {
	local pkgs

	pkgs=$("${find_packages}")
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi

	convert_files "${pkgs}"
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi

	print_success
	cleanup
	exit 0
}

# Run main:
main
