#!/usr/bin/env bash
set -Eeuo pipefail

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"

repo="${1:-}"

if [ -z "$repo" ]; then
	echo >&2 'error: no repo specified'
	cat >&2 <<EOUSAGE
usage: $0 repo [> README.md]
   ie: $0 php > ../php/README.md

   This script generates a stub README to standard out for the specified repo.
EOUSAGE
	exit 1
fi

gitRepo='https://github.com/docker-library/docs'
hubPage="https://hub.docker.com/_/$repo/"

canonicalRepo="https://github.com/docker-library/$repo"
if [ -s "$repo/github-repo" ]; then
	canonicalRepo="$(< "$repo/github-repo")"
fi
canonicalRepo="$(curl -fsSLI -o /dev/null -w '%{url_effective}\n' "$canonicalRepo")" # follow redirects (http://stackoverflow.com/a/3077316/433558)

maintainer="$(sed -e 's!%%GITHUB-REPO%%!'"$canonicalRepo"'!g' "$repo/maintainer.md")"

if [ -f "$repo/deprecated.md" ]; then
	echo '# DEPRECATED'
	echo
	cat "$repo/deprecated.md"
	echo
fi

case "$repo" in
	buildpack-deps | docker | hello-world | hylang) disclaimer='' ;;
	*) disclaimer=" (not to be confused with any official \`$repo\` image provided by \`$repo\` upstream)" ;;
esac

cat <<EOREADME
# $canonicalRepo

## Maintained by: $maintainer

This is the Git repo of the [Docker "Official Image"](https://github.com/docker-library/official-images#what-are-official-images) for [\`$repo\`]($hubPage)$disclaimer. See [the Docker Hub page]($hubPage) for the full readme on how to use this Docker image and for information regarding contributing and issues.

The [full image description on Docker Hub]($hubPage) is generated/maintained over in [the docker-library/docs repository]($gitRepo), specifically in [the \`$repo\` directory]($gitRepo/tree/master/$repo).

## See a change merged here that doesn't show up on Docker Hub yet?

For more information about the full official images change lifecycle, see [the "An image's source changed in Git, now what?" FAQ entry](https://github.com/docker-library/faq#an-images-source-changed-in-git-now-what).

For outstanding \`$repo\` image PRs, check [PRs with the "library/$repo" label on the official-images repository](https://github.com/docker-library/official-images/labels/library%2F$repo). For the current "source of truth" for [\`$repo\`]($hubPage), see [the \`library/$repo\` file in the official-images repository](https://github.com/docker-library/official-images/blob/master/library/$repo).

<!-- THIS FILE IS GENERATED BY $gitRepo/blob/master/generate-repo-stub-readme.sh -->
EOREADME
