#!/usr/bin/env sh
#
# git-web-uri - generate a GitHub/GitLab permalink to the given file/line
#
# Flag | Description
# ---- | -----------
# -h   | Show this screen.
# -x   | Enable xtrace debug logging.
# -u   | Use upstream-tracking SHA instead of HEAD (for unpushed branches)
#
# Usage:
#
#     git web-uri
#     git web-uri path/to/file.py
#     git web-uri path/to/file.py 44
#     git web-uri path/to/file.py 44 48

# shellcheck disable=SC2220
while getopts 'hxu' opt; do
  case ${opt} in
    h)
      awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
      exit 0
      ;;
    x) set -x ;;
    u) use_upstream='@{u}' ;;
  esac
done
shift "$((OPTIND - 1))"

# shellcheck disable=SC2086
SHA="$(git rev-parse --short ${use_upstream:-'HEAD'})"
path="$(git ls-files -- "$1" 2>/dev/null)"

# https://git.z3bra.org/scripts/file/xurls.html#l7
# https://movq.de/git/list-files/file/list-files.c.html#l10
# https://git.vuxu.org/necho/tree/necho.c?id=9c56862#n5
# https://git.causal.agency/src/tree/link.sh#n3
if git remote --verbose | grep -q "git://"; then
  git remote -v | awk -v sha="$SHA" -v path="$path" -v ext="$4" -v start="$2" -v end="$3" '
/fetch)$/ {
    if (match($2, "git://")) {
        baseUrl = "https://git"
        pathSuffix = "file"
        pathExt = ".html"
        rangeSep = ""
        startPrefix = "#l"
        endPrefix = ""
    }

    sub(/^.*:\/\//, "", $2)
    sub(/\.git$/, "", $2)

    printf("%s.%s", baseUrl, $2)
    if (path) { printf("/%s/%s%s", pathSuffix, path, pathExt) }
    if (start) { printf("%s%s", startPrefix, start) }
    if (end) { printf("%s%s%s", rangeSep, endPrefix, end) }
    printf("\n")
}
'
  exit 0
fi

if git remote --verbose | grep -q "git@git"; then
  # shellcheck disable=SC2086
  ID="$(git rev-parse ${use_upstream:-'HEAD'})"

  git remote --verbose \
    | awk -v sha="${ID}" -v path="${path}" -v start="$2" -v end="$3" '
/fetch)$/ {
    if (match($2, "git@git")) {
        baseUrl = "http://192.168.50.19:3000"
        pathSuffix = "tree"
        rangeSep = ""
        startPrefix = "#n"
        endPrefix = "n"
    }
    sub(/^.*:/, "", $2)
    sub(/\.git$/, "", $2)

    printf("%s/%s", baseUrl, $2)
    if (path) { printf("/%s/%s?id=%s", pathSuffix, path, sha) }
    if (start) { printf("%s%s", startPrefix, start) }
    if (end) { printf("%s%s%s", rangeSep, endPrefix, end) }
    printf("\n")
}
'
  exit 0
fi

# shellcheck disable=SC2250
git remote --verbose | awk -v sha="$SHA" -v path="$path" -v start="$2" -v end="$3" '
/fetch)$/ {
    if (match($2, "gitlab")) {
        baseUrl = "https://gitlab.com"
        pathSuffix = "-/blob"
        rangeSep = "-"
        startPrefix = "#L"
        endPrefix = ""
    } else if (match($2, "codeberg")) {
        baseUrl = "https://"
        pathSuffix = "blob"
        rangeSep = "-"
        startPrefix = "#L"
        endPrefix = "L"
    } else if (match($2, "github")) {
        baseUrl = "https://"
        pathSuffix = "blob"
        rangeSep = "-"
        startPrefix = "#L"
        endPrefix = "L"
    } else {
        baseUrl = "https://"
        pathSuffix = "tree"
        rangeSep = ""
        startPrefix = "#n"
        endPrefix = ""
    }
    # sub(/^.*:/, "", $2)
    sub(/^.*:\/\//, "", $2)
    sub(/\.git$/, "", $2)

    printf("%s%s", baseUrl, $2)
    if (path) { printf("/%s/%s/%s", pathSuffix, sha, path) }
    if (start) { printf("%s%s", startPrefix, start) }
    if (end) { printf("%s%s%s", rangeSep, endPrefix, end) }
    printf("\n")
}
'
