#!/bin/bash
set -e

repo="$1"
if [ -z "$repo" ]; then
	echo >&2 "usage: $0 repo"
	echo >&2 "   ie: $0 hylang"
	exit 1
fi

IFS=$'\n'
lines=( $(curl -sSL 'https://raw.githubusercontent.com/docker-library/official-images/master/library/'"$repo" | grep -vE '^$|^#') )
unset IFS

repoDirs=()
declare -A repoDirTags=()

for line in "${lines[@]}"; do
	tag="${line%%: *}"
	repoDir="${line#*: }"
	if [ -z "${repoDirTags[$repoDir]}" ]; then
		repoDirTags["$repoDir"]="$tag"
		repoDirs+=( "$repoDir" )
	else
		repoDirTags["$repoDir"]+=', '"$tag"
	fi
done

echo '# Tags and `Dockerfile` links'
echo

for repoDir in "${repoDirs[@]}"; do
	if [[ "$repoDir" != *github.com* ]]; then
		# skip non-github.com for now
		continue
	fi
	
	# split out some data
	gitUrl="${repoDir%%@*}"
	commitDir="${repoDir#*@}"
	commit="${commitDir%% *}"
	dir="${commitDir#* }"
	if [ "$dir" = "$commitDir" ]; then
		dir=''
	fi
	
	# sanitize some data
	gitUrl="${gitUrl#git://}"
	gitUrl="${gitUrl%/}"
	gitUrl="${gitUrl%.git}"
	dir="${dir#/}"
	dir="${dir%/}"
	[ -z "$dir" ] || dir="$dir/"
	
	url="https://$gitUrl/blob/$commit/${dir}Dockerfile"
	
	echo '- ['"${repoDirTags["$repoDir"]}"']('"$url"')'
done

echo

Add script to autogenerate a "Tags and `Dockerfile` links" section for each repo
Add script to autogenerate a "Tags and `Dockerfile` links" section for each repo
