# Shell aliases & functions for Zsh, almost all work for Bash too
# zsh helpers {{{1
if test "$ZSH_NAME" = "zsh"
then
	alias -g ___='"$(eval "$(fc -ln -1)" | tail -n 1)"'
	alias -g B="| xargs bat"
	alias -g G="| grp"
	alias -g X="| xargs -d '\n' -L 1"
	alias -g X1="| xargs -d '\n' -n 1"
	alias -g XC="| xclip -selection clipboard"
	alias -g L="--color=always | ${PAGER:-less}"
	alias -g T=" | tree -C --fromfile . | less -F"

	# Edit zsh completion
	edit-completion() {
		local file=$(echo "$1" | sed 's/^\([^_]\)/_\1/')
		filepath="${fpath[-1]}/$file"
		test -f "$filepath" ||
			echo "#compdef $1" >"$filepath"
		$EDITOR "$filepath"
		unfunction "$file" && compinit
	}
	compdef "_files -W ${fpath[-1]}/" edit-completion
	alias comp='edit-completion'
else
	# So bash does not error out on zsh completion definitions
	compdef() { true; }
fi

# data directory aliases {{{1
xdh="$XDG_DATA_HOME"
xch="$XDG_CONFIG_HOME"
xsh="$XDG_STATE_HOME"
if test -d "$DATA"; then
	da=$(builtin cd $DATA/_* && pwd)
	d1=$(builtin cd $DATA/1* && pwd)
	d2=$(builtin cd $DATA/2* && pwd)
	d3=$(builtin cd $DATA/3* && pwd)
	d4=$(builtin cd $DATA/4* && pwd)
	d5=$(builtin cd $DATA/5* && pwd)
fi 2>/dev/null

ulimit -c unlimited # Enable core dumps

which lsb_release >/dev/null &&
	export DIST=$(lsb_release --id | cut -d'	' -f2)

unalias rd 2>/dev/null

# System help {{{1

compdef help=man
alias info='info --vi-keys'

sudos() {
	cmd=$1
	shift
	sudo "$(which $cmd)" "$@"
}

resolvealias() {
	alias "$1" 2>/dev/null | sed -n "s/$1='\?\(noglob \)\?\([-A-z]\+\)'\?\$/\2/p" | grep -m 1 . || echo $1
}

h() {
  arg="$1"
  local alias=$(resolvealias "$arg")
  shift
  help "$alias" "$@" || wh "$arg" "$@"
  # TODO call 'wh' on scripts rather than passing potentially hazardous args (as in clean)
}

xtrace() {
	set -x
	"$@"
	set +x
}

# Shows source code for command, resolving nested aliases
wh() {
	local res=$(which "$@") || return $?
	# only works in zsh, not bash
	if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
	then echo "$res" | bat --style=plain --language=sh &&
		tool="$(echo "$res" | head -1 | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)"
		wh $(test $tool = $1 && echo "-p") $tool
		# use command which for other shells
	else test -r "$res" && b -- --language=sh "$res" || echo "$res" | bat --style=plain --language=sh
	fi
}
compdef wh=which

pathadd() {
	local IFS=":"
	local result="$@"
	unset IFS
	cat /etc/environment | head -1 | cut -d'"' -f2 | tr ":" "\n" | while read v
			do [[ " $@ " =~ " $v " ]] || result+=":$v"
		done
	echo PATH=\"${result}\"\\n$(cat /etc/environment | tail -n +2) | sudo tee /etc/environment
}

# ZSH completion and stuff {{{1
alias rs="reset && source $HOME/.zshenv && exec $SHELL"
alias hist='print -z $(history | grep -v -e "killm " -e "netkeeper " | tac | fzf --tiebreak=index --bind='"'"'del:execute(sed "\;$(echo {4..})$d" -i.bak $HISTFILE)'"'"' | sed "s|^ \+||" | cut -d" " -f5-)'

alias es='edit-shell'
alias ec='edit-config'
alias eb='edit-bin'

CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}"
# Fuzzy find and edit shell config files
# Exit code: 1 - no change, 2 - file not found
edit-shell() {
	local file
	case $1 in
		("") file="$CONFIG_SHELL_FUNCTIONS";;
		(zsh) file="$CONFIG_ZSH/.zshrc";;
		(env) file="$HOME/.zshenv";;
		(-f) term=$2
			grepfile="$(grep --recursive "^	*\($term()\|alias[^=]* $term=\)" $CONFIG_SHELLS -n -m 1)"
			file="$(echo "$grepfile" | cut -d':' -f1)"
			line="$(echo "$grepfile" | cut -d':' -f2)"
			test -f "$file" || return 2;;
		(*) file="$(find $CONFIG_SHELLS -name "$1*" | head -1 | grep . || echo "$CONFIG_SHELLS/$1")";;
	esac
	test -f "$file" && checksum="$(md5sum "$file")"
	$EDITOR "$(test "$line" && case "$EDITOR" in (nvim) echo "+normal! ${line}ggzx";; (*vi*) echo "+$line";; (emacs*|*/emacs*) echo "+${line}";; esac || echo "--")" "${file%:*}"
	test -s "$file" || return 1
	# Reload shell config upon change
	test "$checksum" != "$(md5sum $file)" && rs
}

# Edit an executable in the PATH
edit-bin() {
	local toedit="$(resolvealias "$1")"
	if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
	then edit "$f"
	else edit-shell -f "$toedit"
		if test $? = 2
		then local script="$HOME/.local/bin/${2:-scripts}/$1"
			edit "$script" &&
			chmod +x "$script"
		fi
	fi
}

# Task management & time tracking {{{1
t() {
	if test "$#" -eq 0 && which tn >/dev/null
	then tn
	else
		if test "$1" = "do"
		then shift && test "$1" -gt 0 2>/dev/null && task mod sched:today "$@" || task add sched:today "$@"
		else task "$@"
		fi
	fi
}
alias tw='timew'
# Create a temporary timewarrior database for testing
alias twtest='( cp -r "$TIMEWARRIORDB" /tmp/tw-bak && TIMEWARRIORDB=/tmp/timewarriordb-test/$(date +%s) && mkdir -p "$TIMEWARRIORDB"/data && :> "$TIMEWARRIORDB"/timewarrior.cfg && $SHELL )'

# Systemd {{{1

alias syslog='less +F /var/log/syslog'

alias sc='sudo systemctl'
alias scd='sudo systemctl disable --now'

sce() {
	sudo systemctl enable --now "$@" ||
		sudo systemctl status "$@"
}
unalias scs 2>/dev/null
scs() {
	(
	export SYSTEMD_COLORS=true
	systemctl --user status "$1" 2>/dev/null ||
		systemctl --user status "*$1*"
	sudo -E systemctl status "$1" 2>/dev/null ||
		sudo -E systemctl status "*$1*"
	) # | less -RF
}
alias scu='systemctl --user'
alias scue='systemctl --user enable --now'
alias scud='systemctl --user disable --now'

# Reload or restart matching systemctl service
unalias scr 2>/dev/null
scr() {
	systemctl --user daemon-reload
	echo -n "User: " && systemctl --user reload-or-restart "$@" ||
		{ echo -n "System: " && sudo systemctl reload-or-restart "$@"; } &&
		echo "Successful reload" ||
			scs "$@"
}

# Restart matching systemctl service with time for adjustments inbetween
scb() {
	systemctl --user stop --show-transaction "$@"
	sudo systemctl stop --show-transaction "$@"
	read
	systemctl --user start --show-transaction "$@"
	sudo systemctl start --show-transaction "$@"
}

alias jch='sudo SYSTEMD_LESS=FRKiM journalctl --no-hostname'
alias jcl='jch --boot --lines 15' # list - quick overview
alias jce='jch --pager-end' # end - all logs
alias jcf='jch --follow' # follow - monitor logs
alias jc='jcl --unit' # list unit - quick overview
alias jcj='jce -o json-pretty --unit' # JSON View

# Applications {{{1

# Shorthands
alias v='edit'
alias st='synct'
command -v dtrx >/dev/null && alias ex='dtrx'
alias expr='noglob expr'
alias get='noglob ='
alias bi='b .*ignore*'

# Shortcuts
alias kc='kdeconnect-cli --refresh && kdeconnect-cli --list-devices'
alias logoff="loginctl terminate-user $USER"
alias calb='rlwrap -a bc -l'
alias dt='python -c "import time;print(time.asctime(time.localtime()))"'

# Process
alias println='printf "\n"'
alias myip="curl ifconfig.me && println && curl icanhazip.com"
alias dedup='awk '"'"'!a[$0]++'"'"
alias lar='last | tac'
alias lst='( last; last -f /var/log/wtmp.1 ) | grep -v "pts/" | tac | less +G'
alias hedgedoc="tmux kill-session -t hedgedoc; builtin cd '$d4/dev/_forks/hedgedoc' && tmux new-session -s hedgedoc -d 'yarn run dev' \; split-window -h 'nodemon --watch app.js --watch lib --watch locales --watch config.json app.js' \; ls"

alias rm='rm -I'
del() {
	# TODO use current partition and clean on reboot via cron
	trash=/tmp/trash/
	#mkdir $trash
	m "$@" $trash
}

u() {
	# Line below handy for users of netkeeper
	sudo nft list ruleset | grep -q outall && echo "Suspending netkeeper" >&2 | echo sysupgrade | netkeeper 30 2>/dev/null
	if command -v pacman >/dev/null; then
		if test "$(stat /etc/pacman.d/mirrorlist --printf=%y | cut -d'-' -f1-2)" != "$(date +%Y-%m)"
		then
			if command -v pacman-mirrors >/dev/null
			then sudo pacman-mirrors --geoip
			else sudo touch /etc/pacman.d/mirrorlist
			fi
			sudo pacman -Syy --needed base gnupg archlinux-keyring
			sudo pacman-key --populate
			sudo pacman-key --refresh-keys
		fi
	fi
	if command -v topgrade >/dev/null
	then
	  topgrade --disable node emacs remotes
	  if test -d "$XDG_CONFIG_HOME/emacs"
	  then
		  builtin cd $XDG_CONFIG_HOME/emacs
		  git pull --rebase
		  if ! true | doom sync -u
		  then echo "Rebuild doom from scratch?"
			  read result
			  test "$result" = "y" && rm -rf "$DOOMLOCALDIR/straight" && doom sync -u
		  fi
		  pgrep emacsclient && echo -n "^ Emacs client processes. Press enter to restart the emacs server." && read
		  pkill --full "emacs --daemon" && emacs --daemon &
	  fi
	else yadm l
		 sudo apt update && sudo apt upgrade
	fi
}

# Networking
dns() {
	local dig="drill -Q"
	which drill >/dev/null || dig="dig +short"
	local server=1.1.1.1
	# allow changing DNS server with @
	# TODO implement rdns via -x
	for arg; do
		local trimmed="${arg##*//}"
		local cut="${trimmed%%/*}"
		if which host >/dev/null 2>&1
		then host "$cut" $server
		else
			$(echo $dig) A @$server "$cut"
			$(echo $dig) AAAA @$server "$cut"
		fi
	done
}
alias sshk="$(case $TERM in (*-kitty) echo 'TERM=xterm-256color kitty +kitten';; esac) ssh"
sshl() {
	test "$1" = "-a" && shift && local all=true
	lemonade server -allow 127.0.0.1 &
	local authcache="/var/tmp/ssh-keys"
	mkdir -p "$authcache"
	local file="$authcache/$1"
	test "$all" && pass scp ~/.bash_aliases "$@:" # TODO also add sourcing to bashrc if needed
	if ssh -G "$1" | grep --silent "^user root$" && ! [[ "$1" =~ "pve*" ]]
	then pass ssh "$@"
	else
		test ! -e "$file" &&
			ssh-copy-id -i "$(ssh -G "$1" |
				grep --max-count 1 "^identityfile " |
				cut -d " " -f2- |
				sed "s|^~|$HOME|")" "$1" &&
			touch "$file"
		#TERM=xterm-256color
		sshk -R 2489:127.0.0.1:2489 "$@"
	fi
}

alias delta="sudo systemctl restart openvpn-client@deltaPeak.service || jcl --unit openvpn-client@deltaPeak.service"

# Listen to loopback of mic
alias listen='pactl load-module module-loopback; echo "Press Enter to stop"; read; pactl unload-module module-loopback'

alias startMinecraftServer='curl https://ipinfo.io/ip | xclip -sel clip && cd ~/daten/games/sharedgames/minecraft/server && java -jar forge-1.12.2-14.23.5.2768-universal.jar -mx 8G'

alias sqli='rlwrap sqlite3 -column -header -cmd .tables'

alias usergroups="cat /etc/passwd | cut -d':' -f1 | xargs -n 1 id"

p() { pass "$@" || pass edit "$@"; }

alias omd="(echo '#+OPTIONS: tags:nil'; xclip -o -selection clipboard) | pandoc -f org-auto_identifiers -t markdown --wrap preserve | xclip -filter"
alias mdo="pandoc -f gfm-ascii_identifiers-gfm_auto_identifiers -t org-auto_identifiers --wrap preserve"
alias mdox="xclip -o -selection clipboard | mdo | xclip -filter"

alias clr="diffr --colors refine-added:none:background:0x33,0x66,0x33:bold --colors added:none:background:0x33,0x44,0x33 --colors refine-removed:none:background:0x66,0x33,0x33:bold --colors removed:none:background:0x44,0x33,0x33 | less -F"

alias f="fossil"
alias fs="fossil status"
alias fc="fossil commit -v"
fdf() {
	fossil diff "$@" | clr
}

alias gdiff='git diff --word-diff=color --word-diff-regex=. --no-index'

# Default grep with some convenience flags
alias grpc='grep --color=auto --line-number --binary-files=without-match --directories=skip'
# Default grep with some niceties and ignoring case
alias grp='grpc --ignore-case'
# Grep recursively and paginate
# TODO remove some prefixes \([^ ]*/\)\?
grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; }
# Grep in shell config files
grsh() { grpr --no-ignore-case "$@" $HOME/.{ba,z}sh* $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH; }

# Recover stray swap files from neovim
vrec() {
	find "$XDG_DATA_HOME/nvim/swap" -name "*$1*" |
		sed 's/\%/\//g' | sed 's|\(.*\)\..*|\1|' |
		xargs --no-run-if-empty nvim
}
alias vrecd="ls $XDG_DATA_HOME/nvim/swap | head -1 | xargs -r -i mv {} /tmp"

# I think this was something about recovering backup files
unv() { strings $1 | sed 's/5$//' | dedup; }

alias hx='sudo hexedit --maximize --color'
# Paginated hexyl
hex() { hexyl "$@" | "${PAGER:-less}"; }

export DICT="$XDG_DATA_HOME/dictcc"
dic() {
	result=$(cat $DICT/dict.txt | sed '/#/d;/&/d;/^$/d' | fzf --tiebreak=chunk,length --bind='alt-bspace:clear-query' --print-query --query="$1") &&
		dic "$(echo "$result" | tail -1 | sed 's/\t/\n/g' | grep --invert-match --ignore-case "$(echo "$result" | head -1)" | head -1 | sed 's/ \W.*//')"
}
#fzf --tiebreak=length --bind='alt-bspace:clear-query'
alias dict="rlwrap rdictcc --directory $DICT"
dict_update() {
	local dictfile="$DICT/dict.txt"
	test $# -gt 0 && mv -v $1 "$dictfile"
	echo "Reading in $dictfile..."
	unbuffer rdictcc --directory $DICT -i "$dictfile" | rewrite
}

npm-reinstall() {
	rm -rf $TMPDIR/react-*
	rm -rf node_modules/
	npm cache verify
	npm install
}

# Reconnect to ONKYO since it is buggy
alias onkyo='bluetoothctl disconnect 00:09:B0:1D:DC:98 && sleep 1 && bluetoothctl connect 00:09:B0:1D:DC:98'

# Custom tools {{{1

sedcomment() { sed -i "s/$1/#\0/" "${@:2}"; }
seduncomment() { sed -i "s/#\($1\)/\0/" "${@:2}"; }

updateDeps() {
	name="$1"
	pattern="$2"
	depth="4"
	#test $# -gt 2 || echo "Please specify a new version!"
	case $name in
		(gradle-wrapper.properties) depth=6;;
	esac
	shift 2
	oldversion="[0-9.]\+"
	while test $# -gt 1; do
		case "$1" in
			(--pattern) oldversion="$2";;
			(--version) version="$2";;
		esac
		shift 2
	done
	echo name "'$name'" depth "'$depth'" pattern "'$oldversion'" version "'$version'"
	find -maxdepth $depth -not \( -name "*forks" -prune \) -type f -name $name | while read f
	do	highlight $f
		sed -i "s/\($pattern\)\($oldversion\)/\1${version:-\2}/gw /dev/stdout" $f
	done
}
alias updateKotlin="updateDeps build.gradle.kts 'kotlin(\"jvm\") version \"' --version"
alias updateGradle='updateDeps gradle-wrapper.properties "services.gradle.org\/distributions\/gradle-" --version'
alias updateUtils="updateDeps build.gradle.kts '"'"com.github.Xerus2000.util", ".*", "'" --pattern '[^\"]\+' --version"

# Kill all shell background processes
alias killbg='kill ${${(v)jobstates##*:*:}%=*}'

# Kill all processes that match
killm() {
	ps ax | grep "$1" | grep -v grep | sed 's/\([^ ]\) .*/\1/' |
		xargs --no-run-if-empty kill --verbose "${@:2}"
}

# Kil all Java processes except IDEA
# Pass "-9" to force-kill and "-q" to not output what has been killed
killJava() {
	pgrep -f 'java' | while read id
		do if [[ $(ps --no-headers -o cmd $id) != *"idea"* ]]; then
			[[ "$@" == "-q" ]] || echo "killing $(ps -p $id -o pid,cmd --width 350 --no-headers)"
			if [[ "$@" == "-9" ]]
			then kill -9 $id
			else kill $id
			fi
		fi; done
}

# Files {{{1

alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"

if which fd >/dev/null
then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore
	alias ff="noglob fn --color=always " # [F]ind [F]ile
	compdef ff=fd
	compdef fn=fd
else
	alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
	alias f1='find -mindepth 1 -maxdepth 1'
fi

lowercase() {
	#$(which perl-rename || echo rename) -iv 'y/A-Z /a-z-/' "$@"
	find "$@" -exec sh -c 'mv -iv {} "$(echo {} | tr "A-Z " "a-z-" | sed "s|---|_|;s|\.-|.|")" 2>/dev/null' \;
}

# TODO replace cp by rsync, automatically use compression for remote transfers
# rsync directory properly - suffix both dirs with / to act on contents
alias rcn='rsync -v --recursive --human-readable --links --dry-run'
rcd() { rcn --size-only "$@" | tail +2 | tree --fromfile . | less -F; }
alias rc='rcs --links --times'
alias rcu='rc --existing --size-only'
alias rcs='rsync --recursive --info=progress2,remove,symsafe,flist,del --human-readable'
alias dsync='rc --delete --specials'
alias move='rc --remove-source-files'
alias rdiff='rsync --recursive --progress --delete --links --dry-run'
alias rdiffe='rdiff --existing --size-only'
compdef rcd=rsync

# Swap the names of two files
swap() {
	test $# -eq 2 || exit 1
	mv -n $1 $1.tmp
	mv -n $2 $1
	mv -n $1.tmp $2
}
compdef swap=mv

# mv with automatic sudo if neccessary
smv() {
	test -w "$1" && test -w "$(dirname $2)" && mv "$@" || sudo mv "$@"
}
compdef smv=mv

# Rename the file inside its directory
mvf() {
	smv "$1" "$(dirname $1)/$2"
}

# Move from $1 to $2 and create a relative symlink
mvln() {
	local file=$(test -f "$1" && echo 1 || echo 0)
	if test -d $1; then
		mkdir -p "$2"
		mv -vT $1 $2
	else
		m $1 $2
	fi
	[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
	echo -n "Linking: " && ln -vsr "$2" "$1"
}
compdef mvln=mv

# Move the given file into an xdg dir (default XDG_DATA_HOME) and create a symlink
mvx() {
	mvln "$1" "${2:-$XDG_DATA_HOME}/${1#.}"
	yadm add "$1" ".config/$1" 2>/dev/null
}

# Create directory and move into it
mkcd() {
	mkdir -p "$1" && cd "$1"
}
compdef mkcd=mkdir

# Other stuff {{{1

# This is a function rather than a script as it potentially needs to cd out of the directory
umoul() {
	local arg
	# get the last arg
	for arg; do true; done
	if test "$arg"
	then mountpoint="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")"
		mountpoint "$mountpoint" 2>/dev/null || test -b "$mountpoint" ||
			mountpoint="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
		test "$mountpoint" || return 1
	else mountpoint="$PWD"
		while test "$mountpoint" != "/"; do
			mountpoint "$mountpoint" >/dev/null && break
			mountpoint="$(dirname "$mountpoint")"
		done
		test "$(dirname "$mountpoint")" != "/" ||
			mountpoint="$(mount |
				grep --invert-match -e " /[^m][^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" |
				fzf --select-1 --exit-0 | awk '{print $3}')" ||
			return $?
	fi
	while true; do
		case "$PWD" in
			("$mountpoint"*) popd || builtin cd "$(dirname $mountpoint)";;
			(*) break;;
		esac
	done
	# pass on all args except last
	moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
}

resetdocker() {
	#aptremove docker-ce
	kill $(ps -e | grep docker | cut -d' ' -f2)
	sudo rm -rf /var/run/docker /var/lib/docker
	sudo rm /var/run/docker.*
	#aptinstall docker-ce
}

function zipdiff() {
	diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8) #--suppress-common-lines
}

# SWAP
alias memstat='free -h | awk '"'"'NR==2 {printf "Free memory:\t %s/%s\t(%d%)\n",$7,$2,$7*100/$2} NR==3 {if($2 != "0B") printf "Used swap:\t%s/%s\t(%d%)\n",$3,$2,$2*100/$3}'"'"

stopswap() {
	memstat
	swap_used=$(cat /proc/meminfo | grep SwapFree | awk '{print $2}')
	mem_available=$(cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
	if test $swap_used = 0
	then echo "No swap is in use."
	elif test $swap_used + 100000 < $mem_available
	then echo "Freeing swap..."
		sudo swapoff -a
		sudo swapon -a
		memstat
	else
		echo "Not enough free memory!"
	fi
}
