#!/bin/sh

###
### This script has been moved to the git-scripts repository:
### https://github.com/plume-lib/git-scripts
###
echo "Please use $(basename "$0") from https://github.com/plume-lib/git-scripts ."
echo "You are using $0,"
echo "which is an obsolete version from https://github.com/plume-lib/plume-scripts ."

# find-git-fork:  finds a fork of a GitHub repository, or returns the upstream
#   repository if the fork does not exist.
# Usage:  find-git-fork ORG UPSTREAM_ORG REPONAME
# Prints the first one of these that exists:
#   https://github.com/${ORG}/${REPO}.git
#   https://github.com/${UPSTREAM_ORG}/${REPO}.git

# Often, you can use the `git-clone-related` script instead of this one.

if [ "$#" -ne 3 ]; then
  script=$(basename -- "$0")
  echo >&2 "Error: $script requires 3 arguments, got $#"
  echo >&2 "Usage: $script ORG UPSTREAM_ORG REPONAME"
  exit 1
fi

ORG=$1
UPSTREAM_ORG=$2
REPONAME=$3

# Problem with "git ls-remote": it may ask for GitHub credentials.
# (It also downloads more information than wget does.)
# export GITEXISTS="git ls-remote"
export GITEXISTS="wget -q --spider"

if (${GITEXISTS} "https://github.com/${ORG}/${REPONAME}.git" > /dev/null); then
  OWNER="${ORG}"
else
  OWNER="${UPSTREAM_ORG}"
fi
echo "https://github.com/${OWNER}/${REPONAME}.git"
