#!/usr/bin/env cram

  $ : "${domain:=test.NetBSD.org}"
  $ domainpat=$(printf %s "$domain" | sed -e 's/\./\\./g' | tr A-Z a-z)

  $ cat <<EOF >sshconfig
  > StrictHostKeyChecking=no
  > UserKnownHostsFile=$(pwd)/known_hosts
  > GlobalKnownHostsFile=/dev/null
  > EOF
  $ cat <<EOF >rsh.sh
  > #!/bin/sh
  > exec ssh -F "$(pwd)/sshconfig" "\$@"
  > EOF
  $ chmod +x rsh.sh
  $ export CVS_RSH="$(pwd)/rsh.sh"
  $ export GIT_SSH_COMMAND="$(pwd)/rsh.sh"

  $ cat <<EOF >hgrc
  > [ui]
  > ssh = $(pwd)/rsh.sh
  > EOF
  $ export HGRCPATH="$(pwd)/hgrc"
  $ cat <<EOF >gitconfig
  > EOF
  $ export GIT_CONFIG_GLOBAL="/dev/null"
  $ export GIT_CONFIG_SYSTEM="/dev/null"
  $ export GIT_CONFIG="$(pwd)/gitconfig"

  $ testurl() {
  >     url=$1
  >     shift
  >     url=${url#http://}; url=${url#https://}
  >     for scheme in http https; do
  >          echo "$scheme://$url" >/dev/tty
  >          echo '-->' "$scheme://$url" \
  >          | tr A-Z a-z | sed -e "s/$domainpat/\$domain/g"
  >          curl -s -i "$@" "$scheme://$url" \
  >          | domain="$domain" scheme="$scheme" awk '
  >              BEGIN   { domain = ENVIRON["domain"]
  >                        domainpat = gensub(/\./, "\\.", "g",
  >                            tolower(domain))
  >                        scheme = ENVIRON["scheme"] }
  >                      { $0 = tolower($0)
  >                        sub(/\r$/, "")
  >                        gsub(domainpat, "$domain") }
  >              NR == 1 { header = 1; print $2; next }
  >              /^$/    { header = 0; body = 1; next }
  >              header && $1 == "location:" {
  >                        sub("^"scheme"://", "//", $2); print; next }
  >              header && $1 ~ /^x-quagga/ { print; next }
  >              body && /<base href=/ { print; next }
  >              body && /<link rel='\''alternate'\''/ { print; next }
  >          '
  >     done
  > }

  $ testurl http://anongit.$domain
  --> http://anongit.$domain
  301
  location: //gitweb.$domain/
  --> https://anongit.$domain
  301
  location: //gitweb.$domain/
  $ testurl http://anonhg.$domain
  --> http://anonhg.$domain
  301
  location: //hgweb.$domain/
  --> https://anonhg.$domain
  301
  location: //hgweb.$domain/
  $ testurl http://gitweb.$domain
  --> http://gitweb.$domain
  302
  location: /cgit/
  --> https://gitweb.$domain
  302
  location: /cgit/
  $ testurl http://hgweb.$domain
  --> http://hgweb.$domain
  200
  --> https://hgweb.$domain
  200

  $ testurl http://anongit.$domain/robots.txt
  --> http://anongit.$domain/robots.txt
  200
  --> https://anongit.$domain/robots.txt
  200
  $ testurl http://anonhg.$domain/robots.txt
  --> http://anonhg.$domain/robots.txt
  200
  --> https://anonhg.$domain/robots.txt
  200
  $ testurl http://gitweb.$domain/robots.txt
  --> http://gitweb.$domain/robots.txt
  200
  --> https://gitweb.$domain/robots.txt
  200
  $ testurl http://hgweb.$domain/robots.txt
  --> http://hgweb.$domain/robots.txt
  200
  --> https://hgweb.$domain/robots.txt
  200

  $ testurl http://anongit.$domain/favicon.ico
  --> http://anongit.$domain/favicon.ico
  403
  --> https://anongit.$domain/favicon.ico
  403
  $ testurl http://anonhg.$domain/favicon.ico
  --> http://anonhg.$domain/favicon.ico
  403
  --> https://anonhg.$domain/favicon.ico
  403
  $ testurl http://gitweb.$domain/robots.txt
  --> http://gitweb.$domain/robots.txt
  200
  --> https://gitweb.$domain/robots.txt
  200
  $ testurl http://hgweb.$domain/robots.txt
  --> http://hgweb.$domain/robots.txt
  200
  --> https://hgweb.$domain/robots.txt
  200

  $ testurl http://gitweb.$domain/cgit
  --> http://gitweb.$domain/cgit
  301
  location: /cgit/
  --> https://gitweb.$domain/cgit
  301
  location: /cgit/
  $ testurl http://gitweb.$domain/cgit/
  --> http://gitweb.$domain/cgit/
  200
  --> https://gitweb.$domain/cgit/
  200
  $ testurl http://gitweb.$domain/cgit.js
  --> http://gitweb.$domain/cgit.js
  200
  --> https://gitweb.$domain/cgit.js
  200
  $ testurl http://gitweb.$domain/static/cgit/cgit.css
  --> http://gitweb.$domain/static/cgit/cgit.css
  200
  --> https://gitweb.$domain/static/cgit/cgit.css
  200
  $ testurl http://gitweb.$domain/cgit/testsrc
  --> http://gitweb.$domain/cgit/testsrc
  301
  location: /cgit/testsrc/
  --> https://gitweb.$domain/cgit/testsrc
  301
  location: /cgit/testsrc/
  $ testurl http://gitweb.$domain/cgit/testsrc/
  --> http://gitweb.$domain/cgit/testsrc/
  200
  <link rel='alternate' title='atom feed' href='http://gitweb.$domain/cgit/testsrc/atom/?h=default' type='application/atom+xml'/>
  --> https://gitweb.$domain/cgit/testsrc/
  200
  <link rel='alternate' title='atom feed' href='https://gitweb.$domain/cgit/testsrc/atom/?h=default' type='application/atom+xml'/>
  $ testurl http://gitweb.$domain/cgit/testsrc/log
  --> http://gitweb.$domain/cgit/testsrc/log
  301
  location: /cgit/testsrc/log/
  --> https://gitweb.$domain/cgit/testsrc/log
  301
  location: /cgit/testsrc/log/
  $ testurl http://gitweb.$domain/cgit/testsrc/log/
  --> http://gitweb.$domain/cgit/testsrc/log/
  200
  <link rel='alternate' title='atom feed' href='http://gitweb.$domain/cgit/testsrc/atom/?h=default' type='application/atom+xml'/>
  --> https://gitweb.$domain/cgit/testsrc/log/
  200
  <link rel='alternate' title='atom feed' href='https://gitweb.$domain/cgit/testsrc/atom/?h=default' type='application/atom+xml'/>
  $ testurl http://gitweb.$domain/cgit/testsrc/tree/README.md
  --> http://gitweb.$domain/cgit/testsrc/tree/readme.md
  200
  <link rel='alternate' title='atom feed' href='http://gitweb.$domain/cgit/testsrc/atom/readme.md?h=default' type='application/atom+xml'/>
  --> https://gitweb.$domain/cgit/testsrc/tree/readme.md
  200
  <link rel='alternate' title='atom feed' href='https://gitweb.$domain/cgit/testsrc/atom/readme.md?h=default' type='application/atom+xml'/>
  $ testurl http://gitweb.$domain/cgit/testsrc/tree/README.md/
  --> http://gitweb.$domain/cgit/testsrc/tree/readme.md/
  301
  location: /cgit/testsrc/tree/readme.md
  --> https://gitweb.$domain/cgit/testsrc/tree/readme.md/
  301
  location: /cgit/testsrc/tree/readme.md

  $ testurl http://gitweb.$domain/gitweb
  --> http://gitweb.$domain/gitweb
  200
  <base href="http://gitweb.$domain/gitweb" />
  --> https://gitweb.$domain/gitweb
  200
  <base href="https://gitweb.$domain/gitweb" />
  $ testurl http://gitweb.$domain/gitweb/
  --> http://gitweb.$domain/gitweb/
  301
  location: /gitweb
  --> https://gitweb.$domain/gitweb/
  301
  location: /gitweb
  $ testurl http://gitweb.$domain/static/gitweb.css
  --> http://gitweb.$domain/static/gitweb.css
  200
  --> https://gitweb.$domain/static/gitweb.css
  200
  $ testurl http://gitweb.$domain/static/gitweb.js
  --> http://gitweb.$domain/static/gitweb.js
  200
  --> https://gitweb.$domain/static/gitweb.js
  200
  $ testurl http://gitweb.$domain/static/git-favicon.png
  --> http://gitweb.$domain/static/git-favicon.png
  200
  --> https://gitweb.$domain/static/git-favicon.png
  200
  $ testurl http://gitweb.$domain/static/git-logo.png
  --> http://gitweb.$domain/static/git-logo.png
  200
  --> https://gitweb.$domain/static/git-logo.png
  200

  $ testurl http://hgweb.$domain/testsrc
  --> http://hgweb.$domain/testsrc
  200
  --> https://hgweb.$domain/testsrc
  200
  $ testurl http://hgweb.$domain/testsrc/
  --> http://hgweb.$domain/testsrc/
  301
  location: /testsrc
  --> https://hgweb.$domain/testsrc/
  301
  location: /testsrc
  $ testurl http://hgweb.$domain/testsrc@draft
  --> http://hgweb.$domain/testsrc@draft
  200
  --> https://hgweb.$domain/testsrc@draft
  200
  $ testurl http://hgweb.$domain/testsrc@draft/
  --> http://hgweb.$domain/testsrc@draft/
  301
  location: /testsrc@draft
  --> https://hgweb.$domain/testsrc@draft/
  301
  location: /testsrc@draft
  $ testurl http://hgweb.$domain/static/style.css
  --> http://hgweb.$domain/static/style.css
  200
  --> https://hgweb.$domain/static/style.css
  200

XXX Verify /static/style.css is served directly, not via script.

  $ testurl "http://anongit.$domain/testsrc/info/refs"
  --> http://anongit.$domain/testsrc/info/refs
  200
  --> https://anongit.$domain/testsrc/info/refs
  200
  $ testurl "http://anonhg.$domain/testsrc?cmd=capabilities"
  --> http://anonhg.$domain/testsrc?cmd=capabilities
  200
  --> https://anonhg.$domain/testsrc?cmd=capabilities
  200

  $ testurl "http://gitweb.$domain/testsrc/info/refs"
  --> http://gitweb.$domain/testsrc/info/refs
  403
  --> https://gitweb.$domain/testsrc/info/refs
  403
  $ testurl "http://gitweb.$domain/cgit/testsrc/info/refs"
  --> http://gitweb.$domain/cgit/testsrc/info/refs
  301
  location: //anongit.$domain/testsrc/info/refs
  --> https://gitweb.$domain/cgit/testsrc/info/refs
  301
  location: //anongit.$domain/testsrc/info/refs
  $ testurl "http://gitweb.$domain/gitweb/testsrc/info/refs"
  --> http://gitweb.$domain/gitweb/testsrc/info/refs
  301
  location: //anongit.$domain/testsrc/info/refs
  --> https://gitweb.$domain/gitweb/testsrc/info/refs
  301
  location: //anongit.$domain/testsrc/info/refs

  $ testurl "http://hgweb.$domain/testsrc?cmd=capabilities"
  --> http://hgweb.$domain/testsrc?cmd=capabilities
  301
  location: //anonhg.$domain/testsrc?cmd=capabilities
  --> https://hgweb.$domain/testsrc?cmd=capabilities
  301
  location: //anonhg.$domain/testsrc?cmd=capabilities

  $ git clone -q http://anongit.$domain/testsrc http-anongit-testsrc
  $ git -C http-anongit-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttp://anongit.$domain/testsrc (fetch) (esc)
  origin\thttp://anongit.$domain/testsrc (push) (esc)
  $ git clone -q http://gitweb.$domain/cgit/testsrc http-cgit-testsrc
  $ git -C http-cgit-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttp://gitweb.$domain/cgit/testsrc (fetch) (esc)
  origin\thttp://gitweb.$domain/cgit/testsrc (push) (esc)
  $ git clone -q http://gitweb.$domain/gitweb/testsrc http-gitweb-testsrc
  $ git -C http-gitweb-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttp://gitweb.$domain/gitweb/testsrc (fetch) (esc)
  origin\thttp://gitweb.$domain/gitweb/testsrc (push) (esc)

git doesn't respect the permanent redirect, unfortunately.

  $ hg -q clone http://anonhg.$domain/testsrc http-anonhg-testsrc
  $ hg -R http-anonhg-testsrc config paths.default \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  http://anonhg.$domain/testsrc
  $ hg -q clone http://hgweb.$domain/testsrc http-hgweb-testsrc
  $ hg -R http-hgweb-testsrc config paths.default \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  http://hgweb.$domain/testsrc

hg doesn't respect the permanent redirect, unfortunately.

Let's try https access:

  $ git clone -q https://anongit.$domain/testsrc https-anongit-testsrc
  $ git -C https-anongit-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttps://anongit.$domain/testsrc (fetch) (esc)
  origin\thttps://anongit.$domain/testsrc (push) (esc)
  $ git clone -q https://gitweb.$domain/cgit/testsrc https-cgit-testsrc
  $ git -C https-cgit-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttps://gitweb.$domain/cgit/testsrc (fetch) (esc)
  origin\thttps://gitweb.$domain/cgit/testsrc (push) (esc)
  $ git clone -q https://gitweb.$domain/gitweb/testsrc https-gitweb-testsrc
  $ git -C https-gitweb-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\thttps://gitweb.$domain/gitweb/testsrc (fetch) (esc)
  origin\thttps://gitweb.$domain/gitweb/testsrc (push) (esc)

  $ hg -q clone https://anonhg.$domain/testsrc https-anonhg-testsrc
  $ hg -R https-anonhg-testsrc config paths.default \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  https://anonhg.$domain/testsrc
  $ hg -q clone https://hgweb.$domain/testsrc https-hgweb-testsrc
  $ hg -R https-hgweb-testsrc config paths.default \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  https://hgweb.$domain/testsrc

Let's try ssh access:

  $ git clone -q anongit@anongit.$domain:/git/testsrc ssh-anongit-testsrc
  Warning: Permanently added 'anongit.*,*' (ECDSA) to the list of known hosts.\r (esc) (glob)
  $ git -C ssh-anongit-testsrc remote -v \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  origin\tanongit@anongit.$domain:/git/testsrc (fetch) (esc)
  origin\tanongit@anongit.$domain:/git/testsrc (push) (esc)
  $ hg -q clone ssh://anonhg@anonhg.$domain/testsrc ssh-anonhg-testsrc
  remote: Warning: Permanently added 'anonhg.* (ECDSA) to the list of known hosts. (glob)
  $ hg -R ssh-anonhg-testsrc config paths.default \
  > | tr A-Z a-z | sed -e "s/$domainpat/\$domain/"
  ssh://anonhg@anonhg.$domain/testsrc
  $ cvs -q -d anoncvs@anoncvs.$domain:/cvsroot \
  > co -P -d ssh-anoncvs-testsrc testsrc
  Warning: Permanently added 'anoncvs.* (ECDSA) to the list of known hosts.\r (esc) (glob)
  cvsmirror: waiting for reader access
  cvs checkout: warning: cannot open history file `/cvsroot/CVSROOT/history' for write: Read-only file system
  U ssh-anoncvs-testsrc/README.md
  U ssh-anoncvs-testsrc/sandbox.txt
