#!/bin/bash

BUNDLE_NAME=$1
BUNDLE_REPOSITORY="/Library/Application Support/TextMate/Bundles"
SVN_REPOSITORY="http://macromates.com/svn/Bundles/trunk/Bundles"

USERNAME=anon
PASSWORD=$USERNAME


if [ -z "$BUNDLE_NAME" ]; then
    echo "Usage:"
    echo "get_tm_bundle bundle_name"
    exit 1
fi


if [ -d "$BUNDLE_REPOSITORY/$BUNDLE_NAME.tmbundle" ]; then
    echo "Bundle '$BUNDLE_NAME' is already installed"
    exit
else
    cd "$BUNDLE_REPOSITORY"
    
    if svn --username $USERNAME --password $PASSWORD co $SVN_REPOSITORY/$BUNDLE_NAME.tmbundle >/dev/null; then
        echo "Bundle $BUNDLE_NAME installed"
        
        # if TextMate is running, tell it to reload the bundles
        if ps -x | grep "TextMate" >/dev/null; then
            osascript -e 'tell app "TextMate" to reload bundles'
            echo "Bundles reloaded"
        fi
    else
        echo "Bundle $BUNDLE_NAME not found"
    fi
fi