#!/usr/bin/env bash
#===============================================================================
#
#          FILE:  .bash_functions
#
#         USAGE:  source ~/.bash_functions
#
#   DESCRIPTION:  bash functions for interactive shell
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Don Harper (), duckunix@gmail.com
#       COMPANY:  Don Harper
#       VERSION:  1.2
#       CREATED:  06/11/2007 11:17:14 PM CDT
#      REVISION:  ---
#===============================================================================

function nssh ()
{
	. ~/.bash_ssh
	ssh-keygen -R "${1}"
	ssh  "${@}"
}

function ss ()
{
	tmux neww -n "${1}" ". ~/.bash_ssh ; ssh  ${*}"
}

function mss ()
{
	tmux neww -n "${1}" ". ~/.bash_ssh ; mosh  ${*}"
}

function nss ()
{
	ssh-keygen -R "${1}"
	tmux neww -n "${1}" ". ~/.bash_ssh ; ssh  ${*}"
}

function pss ()
{
	tmux split-window -h ". ~/.bash_ssh ; ssh  ${*}"
}

function pnss ()
{
	ssh-keygen -R "${1}"
	tmux split-window -h ". ~/.bash_ssh ; ssh  ${*}"
}

function yay ()
{
	echo "$(pwd) : yay $*" | plod
	unset LOGFILE
	/usr/bin/yay "$@"
}

function doas ()
{
	echo "$(pwd) : doas $*" | plod
	unset LOGFILE
	/usr/bin/env doas "$@"
}

function sudo ()
{
	echo "$(pwd) : sudo $*" | plod
	unset LOGFILE
	/usr/bin/env sudo "$@"
}

function pushnew ()
{
	plod Pushing configs to "${1}"
	cd "${HOME}" || exit
	if [ -z "${1}" ] 
	then
		echo "usage: pushnew <host> <user>"
		return
	fi
	if [ -z "${2}" ] 
	then
		echo "usage: pushnew <host> <user>"
		return
	fi
	ssh-copy-id "${2}"@"${1}"
	ssh "${2}"@"${1}" mkdir -p .ssh Nextcloud/bin Nextcloud/lib Nextcloud/dotfiles .config
	scp .ssh/{authorized_keys,config,id_*} "${2}"@"${1}":.ssh
	scp -r .dotfilesrc .myapps .tmux.conf "${2}"@"${1}":
	ssh "${2}"@"${1}" which rsync
	if [ $? == 1 ]
	then
		echo "${1} needs rsync.  Press enter when installed"
		read foo
	fi
	rsync -avt .config/polybar "${2}"@"${1}":.config
	rsync -avt Nextcloud/bin "${2}"@"${1}":Nextcloud
	rsync -avt Nextcloud/lib "${2}"@"${1}":Nextcloud
	rsync -avt Nextcloud/dotfiles "${2}"@"${1}":Nextcloud
	rsync -avt Nextcloud/sigs "${2}"@"${1}":Nextcloud
	rsync -avt Nextcloud/ansible "${2}"@"${1}":Nextcloud
	ssh  "${2}"@"${1}" 'for dir in bin lib web sigs ansible; do ln -s ${HOME}/Nextcloud/${dir} ${HOME}/${dir};done'
}

function crontab () 
{
	CRONFILE=$HOME/lib/crontab/$HOSTNAME_SHORT
	if which fcrontab > /dev/null 2>&1 
	then
		CTEXEC=$(which fcrontab)
	elif which crontab > /dev/null 2>&1 
	then
		CTEXEC=$(which crontab)
	fi
	case "${1}" in
		-e)
			"${CTEXEC}" -e ; "${CTEXEC}" -l >| "${CRONFILE}"
			;;
		*)
			cat "${CRONFILE}"
			;;
	esac
}


function add_cxoffice_path ()
{
	if test -d ~/.cxoffice
	then
		OLDLANG=$LANG
		export LANG=C
		for cxdir in ~/.cxoffice/[A-Za-z]*/{desktopdata/cxmenu/StartMenu*/Programs,drive_c/users/*/Start*/Programs}
		do
			for dir in $(find "$cxdir" -type f -exec dirname {} \;)
			do
				buildpath PATH "$dir"
			done
		done
		export LANG=$OLDLANG
	fi
}

cd_func ()
{
	local x2 the_new_dir adir index
	local -i cnt

	if [[ "${1}" ==  "--" ]]; then
		dirs -v
		return 0
	fi

	the_new_dir="${1}"
	[[ -z "${1}" ]] && the_new_dir=$HOME

	if [[ ${the_new_dir:0:1} == '-' ]]; then
		#
		# Extract dir N from dirs
		index=${the_new_dir:1}
		[[ -z $index ]] && index=1
		adir=$(dirs +$index)
		[[ -z $adir ]] && return 1
		the_new_dir=$adir
	fi

	#
	# '~' has to be substituted by ${HOME}
	[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

	#
	# Now change to the new dir and add to the top of the stack
	pushd "${the_new_dir}" > /dev/null
	[[ $? -ne 0 ]] && return 1
	the_new_dir=$(pwd)

	#
	# Trim down everything beyond 11th entry
	popd -n +21 2>/dev/null 1>/dev/null

	#
	# Remove any other occurence of this dir, skipping the top of the stack
	for ((cnt=1; cnt <= 20; cnt++)); do
		x2=$(dirs +${cnt} 2>/dev/null)
		[[ $? -ne 0 ]] && return 0
		[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
		if [[ "${x2}" == "${the_new_dir}" ]]; then
			popd -n +$cnt 2>/dev/null 1>/dev/null
			cnt=$((cnt-1))
		fi
	done

	return 0
}
alias cd=cd_func

tgrep ()
{
	task info ${*} | egrep '(Parent|Due|Last)'
}
