view .bash_overrides @ 592:33b313fa1eb0

STUB: python/install_config.py
author Jeff Hammel <k0scist@gmail.com>
date Sat, 25 Jan 2014 10:19:35 -0800
parents 047a53a8fbad
children 753b1c2bd64c
line wrap: on
line source

#!/bin/bash

### command overrides too elaborate for aliases

cd() {

    ENV=""
    if [ -n "${VIRTUAL_ENV}" ]
    then
	ENV="${VIRTUAL_ENV}"
    fi


    if [ -d "$@" ]
    then
	command cd "$@"
    else
	if [ -e "$@" ]
	then
	    command cd `dirname "$@"`
	else
	    if [[ "$@" == "^"  &&  -n "${ENV}" ]]
	    then
		command cd "${ENV}"
	    else
		command cd "$@"
	    fi
	fi
    fi

    # handle ENVs
    if [ -e "bin/activate" ]
    then
	if [ -n "${ENV}" ]
        then
	    if [ "${ENV}" != "${PWD}" ]
	    then
		if [[ -n "`type -t deactivate`" ]]
		then
		    deactivate
		fi
	    else
		return
	    fi
	fi
	source "bin/activate"
    fi

    ENV=""
    if [ -n "${VIRTUAL_ENV}" ]
    then
	ENV="${VIRTUAL_ENV}"
    fi

    if [ -n "${ENV}" ]
    then
	FULLPWD=$(python -c 'import os; print (os.getcwd())')
	if (( ! `expr match "${FULLPWD}" "${ENV}"` ))
	then
	    if [[ -n "`type -t deactivate`" ]]
	    then
		deactivate
	    fi
	fi
    fi
    unset ENV
}

function emacsclient() {
    command emacsclient $@ > /dev/null &
}

function find {

    if (( ! $# ))
    then
	command find
        return
    fi

    if [ -d $1 ]
    then
	DIR="$1"
	shift
    else
	DIR="$PWD"
    fi

    if [ "$#" == "1" ]
    then
	`which find` -L "$PWD" -not -path '*.svn*' -iname "$1"
	return 0
    fi

    COMMAND="`which find` -L \"$DIR\" -not -path '*.svn*' $@"
#echo $COMMAND # for debugging
    `which find` -L "$DIR" -not -path '*.svn*' "$@"
}

# make hg safer since $HOME is versioned
function hg {
    if [[ "$(command hg root)" == "${HOME}" ]]
    then
        if [[ "$*" == st* ]]
        then
            command hg st -q
            return
        fi
        if [[ "$*" == res* ]] || [[ "$*" == pur* ]]
        then
            echo "Aborting \'hg $*\'; hg root is ${HOME}!"
            return 1
        fi
    fi
    command hg "$@"
}

function lsdiff {
    # TODO: extract this general pattern as a bash "decorator"
    if expr "$1" : 'http[s]\?://.*' &> /dev/null
    then
        curl --location "$1" 2> /dev/null | command lsdiff
    else
        command lsdiff "$@"
    fi

}

function mv {
    num=$#
    if (( num == 0 ))
    then
        command mv "$@"
        return
    fi
    last=${!#}
    if [[ "${last:(-1)}" == "/" ]]
    then
        directory=${last}
    else
        directory=$(dirname ${last})
    fi
    if [[ ! -e "${directory}" ]]
    then
        mkdir -p "${directory}"
    fi
    command mv "$@"
    return

}

function unlink {
    command unlink `echo $@ | sed 's/\/$//g'`
}