#!/bin/sh -efu

. girar-sh-functions

usage()
{
	echo >&2 "$PROG: $*"
	echo >&2 "usage: $PROG {<binary repository name> | -p <pocket repository name>}"
	exit 1
}

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

pocket_flag=
if [ "$1" = "-p" ]; then
	pocket_flag=1
	shift
fi

[ "$#" -le 1 ] ||
	usage 'Too many arguments.'

if [ -n "$pocket_flag" ]; then
	pocket="$1"; shift

	GIRAR_POCKET_REPO_DIR="$GIRAR_POCKETS/$pocket"

	[ -d "${GIRAR_POCKET_REPO_DIR-}" ] ||
		fatal 'GIRAR_POCKET_REPO_DIR is either undefined or invalid'

	[ -f "$GIRAR_POCKET_REPO_DIR/files/pocket/repo" ] ||
		fatal "$GIRAR_POCKET_REPO_DIR: pocket repo is either undefined or invalid"

	repository="$(cat "$GIRAR_POCKET_REPO_DIR/files/pocket/repo")"
else
	repository="$1"; shift
fi

# Returns:
# 0 - $package exists in $repository;
# 1 - otherwise.
# stdout = version in repository

[ -n "${GIRAR_USER-}" ] ||
	fatal 'GIRAR_USER undefined'

repository="$(girar-normalize-repo-name "$repository")"

conf="$GIRAR_REPO_CONF_DIR/$repository"
GIRAR_REPO_SUFFIX=
if [ -s "$conf" ]; then
	. "$conf"
fi

printf %s "${GIRAR_REPO_SUFFIX-}"
