#!/bin/sh
#
# git-issue-init - Configure the current repo for issue tracking
#

set -e

usage() {
	cat <<EOF
usage: git issue init [<remote>]

Configure the current repository for git-issue tracking.
Adds the refs/issues/* fetch refspec to the specified remote.

If no remote is specified, git-issue will auto-detect:
  - If only one remote exists, use that
  - If 'origin' exists, use origin
  - Otherwise, you must specify which remote to use

Note: git issue init is OPTIONAL. Core commands (create, ls, show, comment)
work without it. Init only adds convenience for automatic fetch of issues.
EOF
	exit 1
}

remote=""
remote_specified=0

while test $# -gt 0
do
	case "$1" in
		-h|--help)
			usage
			;;
		--)
			shift
			break
			;;
		-*)
			echo "error: unknown option '$1'" >&2
			usage
			;;
		*)
			remote="$1"
			remote_specified=1
			shift
			;;
	esac
done

# Verify we are inside a git repository
# shellcheck disable=SC2034  # git_dir is used for validation check
git_dir="$(git rev-parse --git-dir 2>/dev/null)" || {
	echo "fatal: not a git repository" >&2
	exit 128
}

# Auto-detect remote if not specified
if test "$remote_specified" -eq 0
then
	all_remotes="$(git remote)"
	remote_count="$(printf '%s' "$all_remotes" | grep -c . || true)"

	if test "$remote_count" -eq 0
	then
		# No remotes at all
		cat >&2 <<EOF
git-issue: No remotes configured.

Note: git issue init is OPTIONAL. You can use git-issue without any remote:
  - All core commands (create, ls, show, comment, edit, state) work locally
  - To sync with a remote later, use manual push/fetch:
    git push <remote> 'refs/issues/*'
    git fetch <remote> 'refs/issues/*:refs/issues/*'

To configure automatic fetch when you add a remote, run:
  git issue init <remote-name>
EOF
		exit 0
	elif test "$remote_count" -eq 1
	then
		# Exactly one remote - use it
		remote="$all_remotes"
		printf "git-issue: Auto-detected remote '%s'\n" "$remote"
	elif printf '%s' "$all_remotes" | grep -qxF "origin"
	then
		# Multiple remotes but origin exists - use origin
		remote="origin"
		printf "git-issue: Auto-detected remote 'origin'\n"
	else
		# Multiple remotes, no origin - ask user to specify
		cat >&2 <<EOF
git-issue: Multiple remotes found. Please specify which one to use:
  $(printf '%s' "$all_remotes" | sed 's/^/  - /')

Usage:
  git issue init <remote-name>

Example:
  git issue init $(printf '%s' "$all_remotes" | head -1)
EOF
		exit 1
	fi
fi

# Verify the remote exists (if we got here, either user specified or auto-detected)
if ! git remote | grep -qxF "$remote"
then
	echo "error: remote '$remote' does not exist" >&2
	echo "       available remotes: $(git remote | paste -sd', ' -)" >&2
	exit 1
fi

# Check if the refspec already exists
existing="$(git config --get-all "remote.$remote.fetch" 2>/dev/null || true)"
if printf '%s\n' "$existing" | grep -q 'refs/issues'
then
	printf 'git-issue: already configured for remote '\''%s'\''.\n' "$remote"
	exit 0
fi

# Add the fetch refspec for issues
git config --add "remote.$remote.fetch" '+refs/issues/*:refs/issues/*'
printf 'git-issue: configured. Issues will be fetched from %s.\n' "$remote"
printf 'Run '\''git fetch %s'\'' to pull existing issues.\n' "$remote"
