#!/usr/bin/env zsh
# final usage:
#`clone https://github.com/foo/bar dir` should work, or `clone git@github.com/foo/bar` should work


get_directory() {
    get_directory_return=''
    local url="${@: -1}"
    url=${url##*/}
    get_directory_return=${url%.git}
    return 0
}
get_directory_return=''

#todo parse output instead of this shit
#or copy into tmp dir and move out
clone() {
  get_directory "${@: -1}" &&\
  git clone "$@" &&\
  cd "$get_directory_return"
  return 0
}

# need capital `T` to avoid colission with `[`
Test() {
    get_directory "https://github.com/vegerot/dotfiles"
    if [[ "$get_directory_return" == 'dotfiles' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    get_directory "ssh://max@github.com/vegerot/dotfiles.git"
    if [[ "$get_directory_return" == 'dotfiles' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    # from git-add manual.  Not a priority for me since I don't use this syntax often

    get_directory "git://git.kernel.org/pub/scm/.../linux.git separate_directory"
    if [[ "$get_directory_return" == 'separate_directory' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    get_directory "ssh://max@rocks.xz:22/path/to/sshPort.git/"
    if [[ "$get_directory_return" == 'sshPort' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    get_directory "git://rocks.xz:22/path/to/gitUrl.git"
    if [[ "$get_directory_return" == 'gitUrl' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    get_directory "max@rocks.xz:22/path/to/scp.git"
    if [[ "$get_directory_return" == 'scp' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi

    #Fuck you if you do this
    get_directory "ssh://max@rocks.xz:22/~dickwad/I/hate/you.git/"
    if [[ "$get_directory_return" == 'you' ]]; then
      echo pass $get_directory_return
    else
      echo fail $get_directory_return
    fi
}

if [[ ${1:-} == "test" ]]; then
  Test
fi
