###
### This is Mark Jaquith's Bash aliases file, for OS X
### Please share your comments, and edit or addition suggestions!
###

# Flush DNS (OS X)
alias flushdns="dscacheutil -flushcache; sudo killall -HUP mDNSResponder"

# Because I'm an idiot and sometimes type it backwards
alias dnsflush="flushdns"

# WP-PHPtidy
alias wptidy="~/Dropbox/Apps/phptidy/wp-phptidy/wp-phptidy.php"
alias wptidyclean="find . -name *phptidybak* | xargs rm"

# Harness Vagrant commands
function harness() {
	(cd ~/git/harness-wordpress-app/trellis && vagrant $1)
}

# Trips to Discover Vagrant commands
function trips() {
	(cd ~/Sites/tripstodiscover.com && vagrant $1)
}

# Update git repo, and all submodules
alias gitup="git submodule foreach 'git fetch origin --tags; git checkout master; git pull' && git pull && git submodule update --init --recursive"

# Fix and shrink png files. Pass infile and outfile.
alias pngfix="pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB $1 $2"

# Open in current text editor
alias o="s"
alias s="subl"

function subl() {
	if [[ ! -p /dev/stdin ]]; then
		command subl > /dev/null 2>&1
	fi
	command subl "$@"
}

# Edit the hosts file
alias hosts="o /etc/hosts"

# Use Homebrew's Subversion
alias svn="/usr/local/bin/svn"
alias oldsvn="/Library/Developer/CommandLineTools/usr/bin/svn"

# Vagrant
alias v="vagrant"

# Terminal notifier
alias tn="terminal-notifier"
alias notify="terminal-notifier"

# Directory listing
alias ls="ls -G"
alias la="ls -lA"

# Get the IP address for a domain
function ipfor(){ dig +short $1 | grep -E '^[0-9.]+$' | head -1; }

# Directory size
function sizeof(){ du -hs $1 | awk '{print $1}'; }

# alias cloudup="/usr/local/share/npm/bin/up"

alias add_all="/usr/local/bin/svn st | grep '^\?' | awk '{print \$2}' | xargs /usr/local/bin/svn add"
alias rm_all="/usr/local/bin/svn st | grep '^\!' | awk '{print \$2}' | xargs /usr/local/bin/svn rm"
#alias gitmods="subl `git status --porcelain | awk '{print $2}'`"

alias wpcs="phpcs --standard=WordPress-Core,WordPress-Docs"

alias wpcsdiff="phpcs --standard=WordPress-Core,WordPress-Docs --report=diff"

function wpcsfix(){
	phpcs --standard=WordPress-Core,WordPress-Docs --report=diff $1 | patch -p0
}

# alias puphpet-cli="go run ~/Git/puphpet-cli/puphpet.go"

alias gti="git"

sslcert() {
	if [ -z "$1" ]; then
		echo "You must supply a name for the output files, like 'example.com'"
		return 1
	fi
	openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout $1.key -out $1.crt
}

# alias hotairtunnel="ssh -f -L 2222:54.175.110.8:22 flatearth.org -N && ssh awshotairTunnel"

alias phparray="php ~/Applications/php-short-array-syntax-converter/convert.php -w"

alias gist='gist -c'
alias f='find . -name '
alias wpattach='~/bin/attach-patch.sh'
alias wpsearch="find * -type f \! -name *.svn* | xargs fgrep -i $1"
alias staticdynamic="awk '{if ( $7 ~ /\.(jpe?g|JPG|ico|css|js|png|gif|html?)$/ && $7 !~ /\?/) {print \"[static]\"} else {print \"        \",$7}} '"
alias yui="java -jar ~/Applications/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar $1"

function howmuchphp() {
	find . \( -name "*.php" -print0 \) | xargs -0 wc -c | tail -1 | awk '{print $1}'
}
function findends() {
	find . -name "*.$1"
}
function rmends() {
	find . -name "*.$1" -print0 | xargs -0 rm
}
function wpcom_speed() {
	curl http://$1/ 2>/dev/null | grep -oE '(served from batcache in [^ ]+ seconds|Page generated in [^ ]+ seconds)' | head -1
}
function wpcom_speed_with_cookie() {
	curl -H 'Cookie: comment_author_' http://$1/ 2>/dev/null | grep -oE '(served from batcache in [^ ]+ seconds|Page generated in [^ ]+ seconds)' | head -1
}
alias fiximpromptu="rm -rf ~/.impromptu/.compiled; killall node"
alias movielist="~/Project\ Support/movielist.php"
alias finder="open -a finder ."
alias imageslurp="wget --mirror $1"
alias profile="o ~/.profile"
alias sup="svn up --ignore-externals"
alias fixopenwith='/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user'
wpfn() {
find . \( -name "*.php" -print0 \)  | xargs -0 grep -n "function[[:space:]]\+&\{0,1\}$1[[:space:]]*("
}
wpfns() {
find . \( -name "*.php" -print0 \)  | xargs -0 grep -n "function[[:space:]]\+[^ (]*$1"
}
function svntrunksync() {
	svn rm -m 'Removing a path (for recreation)' ^/$1;
	svn cp -m 'Recreating a path' ^/trunk ^/$1;
}
function ff() {
	find . -type f -iname '*'$*'*' -ls ;
}
function wptunnel() {
	alive=`ps aux | grep proxy\.wordpress\.org | grep -v grep | awk '{print $2}'`
	tunnel="ssh -Nf -D 8090 proxy.wordpress.org"
	if [ -n "$alive" -a "restart" == "$1" ]
	then
		kill -9 $alive
		$tunnel
	else
		if [ -z "$alive" ]
		then
			$tunnel
		fi
	fi
}
function gwstest() {
	echo "[1] (Test) Deploying on Pantheon..."
	terminus env:deploy gwshoes.test --cc -n
	echo "[2] (Test) Clearing Redis..."
	terminus remote:wp gwshoes.test -- cache flush
}
function gwslive() {
	echo "[1] (Live) Deploying on Pantheon..."
	terminus env:deploy gwshoes.live --cc -n
	echo "[2] (Live) Clearing Redis..."
	terminus remote:wp gwshoes.live -- cache flush
}
function proxy(){
	if [ -z "$1" ]; then
		echo -n "Ethernet: "
		networksetup -getsocksfirewallproxy 'Ethernet 1' | grep ^Enabled
		networksetup -getautoproxyurl 'Ethernet 1'
		echo ""
		echo -n "SOCKS:    "
		ps -ef | grep ssh.markjaquith@proxy\.wordpress\.org | grep -v grep | awk '{print $2 }'
		echo ""
	else
		proxy
		if [ "$1" == "on" ] && [ -z "$(ps -ef | grep ssh.markjaquith@proxy\.wordpress\.org | grep -v grep | awk '{print $2 }')" ]; then
			echo "Please start Sidestep first."
		else
			networksetup -setsocksfirewallproxystate 'Ethernet 1' $1
			if [ "$1" == "off" ] || [ "$2" == "pac" ]; then
				networksetup -setautoproxyurl 'Ethernet 1' 'https://mc.wordpress.org/proxy-pac.php?port=9050'
				networksetup -setautoproxystate 'Ethernet 1' $1
			fi
			if [ "$1" == "on" ]; then
				echo "You are proxied."
			else
				echo "No longer proxying."
				if [ "$2" == "kill" ]; then
					ps -ef | grep ssh.markjaquith@proxy\.wordpress\.org | grep -v grep | awk '{print $2 }' | xargs kill
				fi
			fi
		fi
	fi
}
mod(){
	svn stat --ignore-externals | grep '^[^?X]'
}
function wpatch() {
	wget -O - $1 | patch -p0
}

function title {
	printf "\033]0;%s\007" "$1"
}

upto () {
	if [ -z "$1" ]; then
		return
	fi
	local upto=$1
	cd "${PWD/\/$upto\/*//$upto}"
}

alias up="upto";
alias back="cd -";