#!/usr/bin/env bash
{ set +x; } 2>/dev/null

function git-url-fullname() {
	{ set +x; } 2>/dev/null

	local url="$1"
	local fullname=
	local owner=
	local repo=
	# git@github.com:owner/repo.git
	# git@gitlab.com:owner/repo.git
	# ssh://hg@bitbucket.org/owner/repo
	[[ $url == *"@"* ]] && {
		IFS="@";set $url
		local path="$2"
		[[ $path == *":"* ]] && { IFS=:;set $path;IFS=; }
		[[ $path != *":"* ]] && { IFS=/;set $path;IFS=; }
		shift
		IFS=/;set -- "$*"
		IFS=/;fullname="$@";IFS=
		IFS=/;set $fullname;IFS=
		owner="$1"
		repo="$2"
		[[ $url == *.git ]] && repo="${repo::${#repo}-4}"
	}
	# https://github.com/owner/repo.git
	# https://gitlab.com/owner/repo.git
	[[ $url == "https://"*".git" ]] && {
		IFS=/;set $url;IFS=
		owner="$4"
		repo="${5::${#5}-4}"
	}
	# https://github.com/owner/repo
	# https://username@bitbucket.org/owner/repo
	[[ $url == "https://"* ]] && [[ $url != *.git ]] && {
		IFS=/;set $url;IFS=
		owner="$4"
		repo="$5"
	}
	echo "$owner/$repo"
}

[[ $# == 0 ]] && [ -s /dev/stdin ] && set -- "$(cat -)" # stdin
[[ $# != 1 ]] || [[ $1 == "--help" ]] && {
	echo "usage: ${BASH_SOURCE[0]##*/} url"
	exit 1
} 

git-url-fullname "$@"
