#!/bin/bash

# Usage: downloader.sh username teamname
REPO_FILE=${1}
DIR=${2}

if [ -d "$DIR" ]
then
    echo "Directory already exists"
    exit 1
fi

mkdir "${DIR}"
cd "${DIR}"

for repo in `cat "../${REPO_FILE}"`
do
    local_dir=.`dirname $repo`
    mkdir -p "$local_dir"
    cd "$local_dir"

    echo "[Cloning $repo]"

    hg clone ssh://hg@bitbucket.org"$repo"

    if [ $? != 0 ]
    then
        echo "[hg failed, trying git instead]"
        git clone git@bitbucket.org:"$repo".git
    fi

    echo ""
    echo ""

    cd ..

    # echo "Cloning" $repo
    # if echo "$repo" | grep -q ".git"; then
    #     command="git"
    # else
    #     command="hg"
    # fi
    # $command clone $repo
done

cd ..
