# function join() { 
#    echo ${1%/*}/${2#/}
# }

function join() {
   # Join two URIs as though the former were the base of the latter
   python -c "print __import__('urlparse').urljoin('$1', '$2')"
}

[[ $(join 'foo:xyz' 'bar:abc') = 'bar:abc' ]] || echo fail: 'foo:xyz' 'bar:abc'
[[ $(join 'http://example/x/y/z' '../abc') = 'http://example/x/abc' ]] || echo fail: 'http://example/x/y/z' '../abc'
[[ $(join 'http://example2/x/y/z' 'http://example/x/abc') = 'http://example/x/abc' ]] || echo fail: 'http://example2/x/y/z' 'http://example/x/abc'
[[ $(join 'http://ex/x/y/z' '../r') = 'http://ex/x/r' ]] || echo fail: 'http://ex/x/y/z' '../r'
[[ $(join 'http://ex/x/y' 'q/r') = 'http://ex/x/q/r' ]] || echo fail: 'http://ex/x/y' 'q/r'
[[ $(join 'http://ex/x/y' 'q/r#s') = 'http://ex/x/q/r#s' ]] || echo fail: 'http://ex/x/y' 'q/r#s'
[[ $(join 'http://ex/x/y' 'q/r#s/t') = 'http://ex/x/q/r#s/t' ]] || echo fail: 'http://ex/x/y' 'q/r#s/t'
[[ $(join 'http://ex/x/y' 'ftp://ex/x/q/r') = 'ftp://ex/x/q/r' ]] || echo fail: 'http://ex/x/y' 'ftp://ex/x/q/r'
[[ $(join 'http://ex/x/y' '') = 'http://ex/x/y' ]] || echo fail: 'http://ex/x/y' ''
[[ $(join 'http://ex/x/y/' '') = 'http://ex/x/y/' ]] || echo fail: 'http://ex/x/y/' ''
[[ $(join 'http://ex/x/y/pdq' '') = 'http://ex/x/y/pdq' ]] || echo fail: 'http://ex/x/y/pdq' ''
[[ $(join 'http://ex/x/y/' 'z/') = 'http://ex/x/y/z/' ]] || echo fail: 'http://ex/x/y/' 'z/'
[[ $(join 'file:/swap/test/animal.rdf' '#Animal') = 'file:/swap/test/animal.rdf#Animal' ]] || echo fail: 'file:/swap/test/animal.rdf' '#Animal'
[[ $(join 'file:/e/x/y/z' '../abc') = 'file:/e/x/abc' ]] || echo fail: 'file:/e/x/y/z' '../abc'
[[ $(join 'file:/example2/x/y/z' '/example/x/abc') = 'file:/example/x/abc' ]] || echo fail: 'file:/example2/x/y/z' '/example/x/abc'
[[ $(join 'file:/ex/x/y/z' '../r') = 'file:/ex/x/r' ]] || echo fail: 'file:/ex/x/y/z' '../r'
[[ $(join 'file:/ex/x/y/z' '/r') = 'file:/r' ]] || echo fail: 'file:/ex/x/y/z' '/r'
[[ $(join 'file:/ex/x/y' 'q/r') = 'file:/ex/x/q/r' ]] || echo fail: 'file:/ex/x/y' 'q/r'
[[ $(join 'file:/ex/x/y' 'q/r#s') = 'file:/ex/x/q/r#s' ]] || echo fail: 'file:/ex/x/y' 'q/r#s'
[[ $(join 'file:/ex/x/y' 'q/r#') = 'file:/ex/x/q/r#' ]] || echo fail: 'file:/ex/x/y' 'q/r#'
[[ $(join 'file:/ex/x/y' 'q/r#s/t') = 'file:/ex/x/q/r#s/t' ]] || echo fail: 'file:/ex/x/y' 'q/r#s/t'
[[ $(join 'file:/ex/x/y' 'ftp://ex/x/q/r') = 'ftp://ex/x/q/r' ]] || echo fail: 'file:/ex/x/y' 'ftp://ex/x/q/r'
[[ $(join 'file:/ex/x/y' '') = 'file:/ex/x/y' ]] || echo fail: 'file:/ex/x/y' ''
[[ $(join 'file:/ex/x/y/' '') = 'file:/ex/x/y/' ]] || echo fail: 'file:/ex/x/y/' ''
[[ $(join 'file:/ex/x/y/pdq' '') = 'file:/ex/x/y/pdq' ]] || echo fail: 'file:/ex/x/y/pdq' ''
[[ $(join 'file:/ex/x/y/' 'z/') = 'file:/ex/x/y/z/' ]] || echo fail: 'file:/ex/x/y/' 'z/'
[[ $(join 'file:/devel/WWW/2000/10/swap/test/reluri-1.n3' 'file://meetings.example.com/cal#m1') = 'file://meetings.example.com/cal#m1' ]] || echo fail: 'file:/devel/WWW/2000/10/swap/test/reluri-1.n3' 'file://meetings.example.com/cal#m1'
[[ $(join 'file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3' 'file://meetings.example.com/cal#m1') = 'file://meetings.example.com/cal#m1' ]] || echo fail: 'file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3' 'file://meetings.example.com/cal#m1'
[[ $(join 'file:/some/dir/foo' './#blort') = 'file:/some/dir/#blort' ]] || echo fail: 'file:/some/dir/foo' './#blort'
[[ $(join 'file:/some/dir/foo' './#') = 'file:/some/dir/#' ]] || echo fail: 'file:/some/dir/foo' './#'
[[ $(join 'http://example/x/y%2Fz' 'abc') = 'http://example/x/abc' ]] || echo fail: 'http://example/x/y%2Fz' 'abc'
[[ $(join 'http://example/x/y/z' '/x%2Fabc') = 'http://example/x%2Fabc' ]] || echo fail: 'http://example/x/y/z' '/x%2Fabc'
[[ $(join 'http://example/x/y%2Fz' '/x%2Fabc') = 'http://example/x%2Fabc' ]] || echo fail: 'http://example/x/y%2Fz' '/x%2Fabc'
[[ $(join 'http://example/x%2Fy/z' 'abc') = 'http://example/x%2Fy/abc' ]] || echo fail: 'http://example/x%2Fy/z' 'abc'
[[ $(join 'http://example/x/abc.efg' './') = 'http://example/x/' ]] || echo fail: 'http://example/x/abc.efg' './'