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

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
helperDir='.template-helpers'

# usage: ./update.sh [--namespace NAMESPACE] [[NAMESPACE/]IMAGE ...]
#    ie: ./update.sh
#        ./update.sh debian golang
#        ./update.sh --namespace tianontesting debian golang
#        ./update.sh tianontesting/debian tianontestingmore/golang
#        BASHBREW_ARCH=windows-amd64 BASHBREW_ARCH_NAMESPACES='...' ./update.sh --namespace winamd64

forceNamespace=
if [ "${1:-}" = '--namespace' ]; then
	shift
	forceNamespace="$1"
	shift
fi

images=( "$@" )
if [ ${#images[@]} -eq 0 ]; then
	images=( */ )
fi
images=( "${images[@]%/}" )

if [ -n "$forceNamespace" ]; then
	images=( "${images[@]/#/"$forceNamespace/"}" )
fi

replace_field() {
	targetFile="$1"
	field="$2"
	content="$3"
	extraSed="${4:-}"
	sed_escaped_value="$(echo "$content" | sed 's/[\/&]/\\&/g')"
	sed_escaped_value="${sed_escaped_value//$'\n'/\\n}"
	sed -ri -e "s/${extraSed}%%${field}%%${extraSed}/$sed_escaped_value/g" "$targetFile"
}

for image in "${images[@]}"; do
	repo="${image##*/}"
	namespace="${image%$repo}"
	namespace="${namespace%/}"

	# this is used by subscripts to determine whether we're pushing /_/xxx or /r/ARCH/xxx
	# (especialy for "supported tags")
	export ARCH_SPECIFIC_DOCS=
	if [ -n "$namespace" ] && [ -n "${BASHBREW_ARCH:-}" ]; then
		export ARCH_SPECIFIC_DOCS=1
	fi

	if [ -x "$repo/update.sh" ]; then
		( set -x; "$repo/update.sh" "$image" )
	fi

	if [ -e "$repo/content.md" ]; then
		githubRepo="$(cat "$repo/github-repo")"
		maintainer="$(cat "$repo/maintainer.md")"

		issues="$(cat "$repo/issues.md" 2>/dev/null || cat "$helperDir/issues.md")"
		getHelp="$(cat "$repo/get-help.md" 2>/dev/null || cat "$helperDir/get-help.md")"

		license="$(cat "$repo/license.md" 2>/dev/null || true)"
		licenseCommon="$(cat "$repo/license-common.md" 2>/dev/null || cat "$helperDir/license-common.md")"
		if [ "$license" ]; then
			license=$'\n\n''# License'$'\n\n'"$license"$'\n\n'"$licenseCommon"
		fi

		logo=
		logoFile=
		for f in png svg; do
			if [ -e "$repo/logo.$f" ]; then
				logoFile="$repo/logo.$f"
				break
			fi
		done
		if [ "$logoFile" ]; then
			logoCommit="$(git log -1 --format='format:%H' -- "$logoFile" 2>/dev/null || true)"
			[ "$logoCommit" ] || logoCommit='master'
			logoUrl="https://raw.githubusercontent.com/docker-library/docs/$logoCommit/$logoFile"
			if [ "${logoFile##*.}" = 'svg' ]; then
				# https://stackoverflow.com/a/16462143/433558
				logoUrl+='?sanitize=true'
			fi
			logo="![logo]($logoUrl)"
		fi

		compose=
		composeYaml=
		if [ -f "$repo/compose.yaml" ]; then
			compose="$(cat "$repo/compose.md" 2>/dev/null || cat "$helperDir/compose.md")"
			composeYaml=$'```yaml\n'"$(cat "$repo/compose.yaml")"$'\n```'
		fi

		deprecated=
		if [ -f "$repo/deprecated.md" ]; then
			deprecated="$(< "$repo/deprecated.md")"
			if [ "${deprecated:0:2}" != '# ' ]; then
				deprecated=$'# **DEPRECATION NOTICE**\n\n'"$deprecated"
			fi
			deprecated+=$'\n\n'
		fi

		if ! partial="$("$helperDir/generate-dockerfile-links-partial.sh" "$repo")"; then
			{
				echo
				echo "WARNING: failed to fetch tags for '$repo'; skipping!"
				echo
			} >&2
			continue
		fi

		targetFile="$repo/README.md"

		{
			cat "$helperDir/autogenerated-warning.md"
			echo

			if [ -n "$ARCH_SPECIFIC_DOCS" ]; then
				echo '**Note:** this is the "per-architecture" repository for the `'"$BASHBREW_ARCH"'` builds of [the `'"$repo"'` official image](https://hub.docker.com/_/'"$repo"') -- for more information, see ["Architectures other than amd64?" in the official images documentation](https://github.com/docker-library/official-images#architectures-other-than-amd64) and ["An image'\''s source changed in Git, now what?" in the official images FAQ](https://github.com/docker-library/faq#an-images-source-changed-in-git-now-what).'
				echo
			fi

			echo -n "$deprecated"
			cat "$helperDir/template.md"
		} > "$targetFile"

		echo '  TAGS => generate-dockerfile-links-partial.sh "'"$repo"'"'
		if [ -z "$partial" ]; then
			if [ -n "$ARCH_SPECIFIC_DOCS" ]; then
				partial='**No supported tags found!**'$'\n\n''It is very likely that `%%REPO%%` does not support the currently selected architecture (`'"$BASHBREW_ARCH"'`).'
			else
				# opensuse, etc
				partial='**No supported tags**'
			fi
		fi
		replace_field "$targetFile" 'TAGS' "$partial"

		echo '  ARCHES => arches.sh "'"$repo"'"'
		arches="$("$helperDir/arches.sh" "$repo")"
		[ -n "$arches" ] || arches='**No supported architectures**'
		replace_field "$targetFile" 'ARCHES' "$arches"

		echo '  CONTENT => '"$repo"'/content.md'
		replace_field "$targetFile" 'CONTENT' "$(cat "$repo/content.md")"

		echo '  VARIANT => variant.sh'
		replace_field "$targetFile" 'VARIANT' "$("$helperDir/variant.sh" "$repo")"

		# has to be after CONTENT because it's contained in content.md
		echo "  LOGO => $logo"
		replace_field "$targetFile" 'LOGO' "$logo" '\s*'

		echo '  COMPOSE => '"$repo"'/compose.md'
		replace_field "$targetFile" 'COMPOSE' "$compose"
		echo '  COMPOSE-YAML => '"$repo"'/compose.yaml'
		replace_field "$targetFile" 'COMPOSE-YAML' "$composeYaml"

		echo '  LICENSE => '"$repo"'/license.md'
		replace_field "$targetFile" 'LICENSE' "$license"

		echo '  ISSUES => "'"$issues"'"'
		replace_field "$targetFile" 'ISSUES' "$issues"

		echo '  GET-HELP => "'"$getHelp"'"'
		replace_field "$targetFile" 'GET-HELP' "$getHelp"

		echo '  MAINTAINER => "'"$maintainer"'"'
		replace_field "$targetFile" 'MAINTAINER' "$maintainer"

		echo '  IMAGE => "'"$image"'"'
		replace_field "$targetFile" 'IMAGE' "$image"

		echo '  REPO => "'"$repo"'"'
		replace_field "$targetFile" 'REPO' "$repo"

		echo '  GITHUB-REPO => "'"$githubRepo"'"'
		replace_field "$targetFile" 'GITHUB-REPO' "$githubRepo"

		echo
	else
		echo >&2 "skipping $repo: missing repo/content.md"
	fi
done
