#!/usr/bin/env python

'''
Use a FURL to access a single remote Git repository. To set this up, use
git-publish-with-furl on one machine, and git-clone-furl (to make a new repo)
or git-remote-add-furl (to add a remote to an existing repo) on the other
machine. Those tools automate the following overall process:

 1: create a bare Git repository on the remote host:
     % mkdir ~/repo && cd ~/repo && git init --bare
 2: create a flappserver on the remote host:
     % flappserver create ~/.fs
     % flappserver start ~/.fs
 3: use git-publish-with-furl on the remote host to grant access:
     % git-publish-with-furl ~/.fs ~/repo
     Service added, furl is FURL
 4: copy that FURL to your local host
 5: create a local Git repo to track the remote one:
     % mkdir ~/localrepo
     % cd ~/localrepo
     % git init
 6: configure the git "remote" by adding the following to
    ~/localrepo/.git/config (be sure to paste in the FURL that you got from
    above)

[core]
    gitProxy="git-proxy-flappclient" for "furl"
    gitProxy=none
[remote "XYZ"]
    url = git://XYZ.furl/
    furl = FURL
    fetch = +refs/heads/*:refs/remotes/origin/*

 (Note that the XYZ name can be replaced by something else, but you must use
  the same name in both places, because they will be compared later)

Then just do 'git pull XYZ' or 'git push XYZ' to push/pull over the
foolscap-mediated connection. (hint: replacing 'XYZ' with 'origin' will let
you just do 'git pull'/'git push').
'''

import sys, os
from subprocess import Popen, PIPE

# sys.argv[1] will be a hostname, [2] is portnum
assert sys.argv[1].endswith(".furl")
remotename = sys.argv[1][:-len(".furl")]
assert "." not in remotename # but spaces are ok
cmd = ["git", "config", "--get", "remote.%s.furl" % remotename]
furl = Popen(cmd, stdout=PIPE).communicate()[0].strip()
os.execlp("flappclient", "flappclient", "--furl", furl, "run-command")
