#!/usr/bin/env bash

# make xclip work as I would expect. Copy to clipboard if piped data. Paste from otherwise
function clip(){
if type -p /usr/bin/xclip >/dev/null; then
	if [[ -p /dev/stdin ]] ; then
		# stdin is a pipe
		# stdin -> clipboard
		/usr/bin/xclip -i -selection clipboard
	else
		# stdin is not a pipe
		# clipboard -> stdout
		/usr/bin/xclip -o -selection clipboard
	fi
else
	echo "Remember to install xclip"
fi
}

#Swap two files
function swap() {
	[ -e "$1._tmp" ] && return
	mv "$1" "$1._tmp"
	mv "$2" "$1"
	mv "$1._tmp" "$2"
}

# Add old extention
function old() {
	for file in "$@"; do
		mv "$file" "$file.old"
	done
}

# Cd that does what I normally want
function cd() {
	# Try a normal cd
	builtin cd "$@" 2> /dev/null
	if [ $? = 0 ]; then
		# If we get here cd was successful so do a ls
		ls
	else
		# If we get here, cd was not successful
		if [ -f "$1" ]; then
			# If there is a file there, try and open it in vim
			# ToDo: smarter open so it will open in zathura if it's a pdf for
			# example
			$EDITOR "$1"
		else
			# Otherwise fail clearly
			echo "Can't cd"
		fi
	fi
	#updatePath
}

# Make a folder then cd into it
function mkcd() {
	mkdir -p "$1"
	cd "$1"
}

# Adds sub folders in by .bin directory to the PATH if they are not already in it
# Also adds .bin to the path adjacent to any public_html folder
function updatePath(){
	# Reset the path
	PATH=$(echo $PATH | sed -E "s/:\/home\/jonathan\/\.bin\/.*//g")
	for d in ~/.bin/*/; do
		if [[ :$PATH: != *:"$d":* ]] ; then
			PATH+=":$d"
		fi
	done
	public_html=${PWD%/public_html*}/public_html
	if [ -d $public_html ]; then
		PATH+=":${PWD%/public_html*}/.bin"
	fi

}

function reset_colourscheme(){
	if [ -L $HOME/.dotfiles/shells/zsh/current-color-scheme ]; then
		source $HOME/.dotfiles/shells/zsh/current-color-scheme
	fi
	exit 2
}

# If running ssh interactively (not tab completion) change the colour scheme
function ssh(){
	if [[ -t 0 ]]; then
		if [ -L $HOME/.dotfiles/shells/zsh/ssh-color-scheme ]; then
			source $HOME/.dotfiles/shells/zsh/ssh-color-scheme
			 trap "reset_colourscheme" 2
		fi
	fi
	command /usr/bin/ssh "$@"
	if [[ -t 0 ]]; then
		if [ -L $HOME/.dotfiles/shells/zsh/current-color-scheme ]; then
			source $HOME/.dotfiles/shells/zsh/current-color-scheme
		fi
	fi
}

# Turn on a proxy
# Note that this is not fool proof. It is up to applications to check the
# environment variables
function proxy_on() {
	export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

	if (( $# > 0 )); then
		valid=$(echo $@ | sed -n 's/\([0-9]\{1,3\}.\?\)\{4\}:\([0-9]\+\)/&/p')
		if [[ $valid != $@ ]]; then
			>&2 echo "Invalid address"
			return 1
		fi
		local proxy=$1
		export http_proxy="$proxy" \
			https_proxy=$proxy \
			ftp_proxy=$proxy \
			rsync_proxy=$proxy
					echo "Proxy environment variable set."
					return 0
	fi

	echo -n "username: "; read username
	if [[ $username != "" ]]; then
		echo -n "password: "
		read -es password
		local pre="$username:$password@"
	fi

	echo -n "server: "; read server
	echo -n "port: "; read port
	local proxy=$pre$server:$port
	export http_proxy="$proxy" \
		https_proxy=$proxy \
		ftp_proxy=$proxy \
		rsync_proxy=$proxy \
		HTTP_PROXY=$proxy \
		HTTPS_PROXY=$proxy \
		FTP_PROXY=$proxy \
		RSYNC_PROXY=$proxy
	}

function proxy_off(){
	unset http_proxy https_proxy ftp_proxy rsync_proxy \
		HTTP_PROXY HTTPS_PROXY FTP_PROXY RSYNC_PROXY
			echo -e "Proxy environment variable removed."
		}

# Sanity check for dd
function dd(){
	local drive="$(echo "$@" | grep -oE 'of=[^ ]+' | cut -d '=' -f 2-)"
	if [ -n "$drive" ]; then
		if mount | grep -q "$drive"; then
			echo "It looks like that's mounted, probably not the drive you're after"
			echo "If you are sure, run /usr/bin/dd ..."
			return 1
		fi
	fi
	/usr/bin/dd "$@"
}

# Man without options will use fzf to select a page
function man(){
	MAN="/usr/bin/man"
	if [ -n "$1" ]; then
		$MAN "$@"
		return $?
	else
		$MAN -k . | fzf --reverse --preview="echo {1,2} | sed 's/ (/./' | sed -E 's/\)\s*$//' | xargs $MAN" | awk '{print $1 "." $2}' | tr -d '()' | xargs -r $MAN
		return $?
	fi
}
#
#######################
#  Wordpress Helpers  #
#######################

#Takes you to the parent theme
function ptheme() {
	public_html=${${PWD%/public_html*}%/wiki*}/public_html
	if [ -d $public_html ]; then
		if [ -d $public_html/wp ]; then
			wpPath=$public_html/wp;
		else
			wpPath=$public_html;
		fi
		domain="$(basename $(dirname $public_html ) ).local.jh"
		theme=$(dirname $(wp --path="$wpPath" --url="$domain" theme path $(wp --path="$wpPath" --url="$domain" theme list 2> /dev/null  | grep "parent" | awk '{print $1}') 2> /dev/null ))
		if [ -d $theme ]; then
			if [ -d "$theme" ]; then
				cd $theme
			else 
				echo " Can't find theme folder "
			fi
		else
			echo " Can't find theme folder "
		fi
	else
		echo " Can't find public_html folder."
	fi
}


#Takes you to the child theme
function theme() {
	if [ ! -z "$1" ]; then
		ctheme=$(getthemepath "$1" 2> /dev/null)
	else
		ctheme=$(getthemepath 2> /dev/null)
	fi
	if [ ! -z "$ctheme" ]; then
		cd $ctheme
	fi
}

#Takes you to the js folder child theme
function js() {
	ctheme=$(getthemepath)
	if [ ! -z "$ctheme" ]; then
		cd $ctheme/js
	fi
}

#Takes you to the css folder child theme
function css() {
	ctheme=$(getthemepath)
	if [ ! -z "$ctheme" ]; then
		cd $ctheme/css
	fi
}

#Takes you to the plugin directory
function plugins() {
	public_html=${${PWD%/public_html*}%/wiki*}/public_html
	if [ -d $public_html ]; then
		if [ -d $public_html/wp ]; then
			wpPath=$public_html/wp;
		else
			wpPath=$public_html;
		fi
		cd $(wp --path="$wpPath" plugin path)
	else
		echo " Can't find public_html folder."
	fi
}

#Takes you to the woocommece plugin directory
function woo() {
	public_html=${${PWD%/public_html*}%/wiki*}/public_html
	if [ -d $public_html ]; then
		if [ -d $public_html/wp ]; then
			wpPath=$public_html/wp;
		else
			wpPath=$public_html;
		fi
		cd $(dirname $(wp --path="$wpPath" plugin path woocommerce))
	else
		echo " Can't find public_html folder."
	fi
}

#Takes you to the woocommerce templates plugin directory
function wooTemplates() {
	public_html=${${PWD%/public_html*}%/wiki*}/public_html
	if [ -d $public_html ]; then
		if [ -d $public_html/wp ]; then
			wpPath=$public_html/wp;
		else
			wpPath=$public_html;
		fi
		cd $(dirname $(wp --path="$wpPath" plugin path woocommerce))/templates
	else
		echo " Can't find public_html folder."
	fi
}

#Takes you to the theme directory
function themes() {
	public_html=${${PWD%/public_html*}%/wiki*}/public_html
	if [ -d $public_html ]; then
		if [ -d $public_html/wp ]; then
			wpPath=$public_html/wp;
		else
			wpPath=$public_html;
		fi
		cd $(wp --path="$wpPath" theme path)
	else
		echo " Can't find public_html folder."
	fi
}

#function todo() {
#	wikidir=${${PWD%/public_html*}%/wiki*}/wiki
#	if [ -d "$wikidir" ]; then
#		$EDITOR "$wikidir/ToDo.md"
#	fi
#}

#This function toggles debug in wp-config
function debugToggle(){
	public_html=${PWD%/public_html*}/public_html
	if [ -d $public_html ]; then
		current=$(egrep "'WP_DEBUG'" "$public_html/wp-config.php" | egrep -o "(true|false)")
		[[ $current == true ]] && newvalue=false || newvalue=true
		sed -i "s/'WP_DEBUG'.*/'WP_DEBUG', $newvalue\)\;/g" "$public_html/wp-config.php"
		echo "WP_DEBUG is now $newvalue";
	fi
}

function cat(){
	local grcConfigs="$HOME/.config/grc/"
	if ! type -p bat > /dev/null; then
		/usr/bin/cat "$@"
		return $?
	fi
	if [ -f "$1" ]; then
		local ext="${1##*.}"
		if [ -f "${grcConfigs}conf.${ext}" ]; then
			/usr/bin/cat "$1" | /usr/bin/grcat "${grcConfigs}conf.${ext}" | /usr/bin/bat --file-name "$1"
		else
			/usr/bin/bat "$@"
		fi
	else
		/usr/bin/bat "$@"
	fi
}
