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

# requirements.txt:github-curl
# requirements.txt:github-delete

function github-rename() {
	{ set +x; } 2>/dev/null

	local old="$1"
	local new="$2"

	IFS=/;set $2;IFS=
	local new_name="${!#}"
	local data="{\"name\":\"$new_name\"}"
	local url="https://api.github.com/repos/$old"
	local r="$(set -x; github-curl --request PATCH -d "$data" "$url")"
	[[ $r == *"HTTP/1.1 200 OK"* ]] && return 0
	echo "ERROR: unknown response${NL}$r" && return 1
}

[[ $# != 2 ]] || [[ $1 == "--help" ]] && {
	cat <<EOF
usage: ${BASH_SOURCE[0]##*/} old_name new_name

examples:
	${BASH_SOURCE[0]##*/} old_name new_name
	${BASH_SOURCE[0]##*/} org/old_name org/new_name
EOF
	exit 0
}

github-rename "$@"
