#!/bin/sh -eu

. girar-sh-functions
. shell-quote

packages_list="$GIRAR_PACKAGES_LIST"

usage()
{
	echo >&2 "$PROG: $*"
	echo >&2 "usage: $PROG <pattern>"
	exit 1
}

[ "$#" -ge 1 ] ||
	usage 'Not enough arguments.'
[ "$#" -eq 1 ] ||
	usage 'Too many arguments.'

[ -n "$1" -a -n "${1##*/*}" ] &&
	repo_name="$(quote_sed_regexp "$(printf %s "$1" |tr -s '*')")" ||
	fatal 'invalid pattern'

# Emulate * globbing.
repo_name="$(printf %s "$repo_name" |sed 's/\\\*/.*/g')"

grep "/packages/$repo_name\(\.git\)\?[[:space:]]" "$packages_list" |
	sort -rn -k2,2
