#alias sa='ssh aisa -CX'
alias sb="ssh bellona -CX"
alias sd='ssh decibel -CX'
alias san='ssh anemoi -X'
alias san2='ssh anemoi2-e -X'

alias osci='osc -A https://api.suse.de'
alias oscp='osc -A https://pmbs-api.links2linux.org'

alias gde='git describe --contains --exact-match'
alias gpr='git pull --rebase'
alias gru='git remote update'
alias gd='git diff'
alias gdw='git diff --word-diff'
alias glm='git log --author=jslaby'
alias ref='~/kernel-source/scripts/refresh_patch.sh'
alias qd='quilt diff'
alias qdig='echo "vim `quilt next`" | xclip; dig_patch `quilt next`'
alias qpr='quilt push && ref'

alias tigl='tig -1000'
alias tign='tig korg/next_master..'
alias tigu='tig @{u}..'
alias ip='ip $IP_COLOR'

alias awrap='echo -ne "\x1b[?7h"'

IP_COLOR='--color=auto'
ip $IP_COLOR l &>/dev/null
if [ $? != 0 ]; then
	IP_COLOR='--color'
fi

function cd_existing() {
	for DIR; do
		if [ -d "$DIR" ]; then
			cd "$DIR"
			break
		fi
	done
}

function fastbuildconfig() {
	sed -E -i -e 's/(CONFIG_(LIVEPATCH_IPA_CLONES|DEBUG_INFO(_DWARF[0-9])?))=y/# \1 is not set/' \
		-e 's/# (CONFIG_DEBUG_INFO_NONE) is not set/\1=y/' \
		"$@"
}

function get_maintainer() {
	WHAT="$1"
	test -z "$WHAT" && WHAT="@^"
	tmpfile=$(mktemp /tmp/maintainers.XXXXXX)
	git diff "$WHAT" | ./scripts/get_maintainer.pl --git-min-signatures 1000 | tee "$tmpfile"
	echo
	echo
	sed 's@ (.*@@; s#^#Cc: #' "$tmpfile"
	rm -f "$tmpfile"
}

function join_by() {
	local IFS="$1"
	shift
	echo "$*"
}

function loop_waiting_net() {
	PORT=60000
	echo "waiting for ping on port $PORT"
	nc -dkl 0.0.0.0 "$PORT" | while read; do
		set -x
		eval "$@"
		set +x
		echo waiting for another ping
	done
}

function show_patch {
	SHA="$1"
	HEAD="$2"
	declare -a PATCHES
	PATCHES=( $(git grep -l "Git-commit: $SHA" $HEAD -- 'patches.*') )
	if [ ${#PATCHES[*]} -eq 0 ]; then
		echo no patch found >&2
	elif [ ${#PATCHES[*]} -gt 1 ]; then
		echo more than one patch found: >&2
		echo `join_by $'\n' "${PATCHES[*]}"` >&2
	elif [ -n "$HEAD" ]; then
		git show ${PATCHES[0]} | view '+set ft=diff' -
	else
		${EDITOR:-vim} ${PATCHES[0]}
	fi
}

function stable_dry() {
	patch --dry-run -p1 -i ~/repos/stable-queue/queue-4.13/"$1"
}

function vimconfl() {
	vim `git diff --name-only|sort -u` '+/<<<<<<<'
}

function vimp() {
	REV="$1"
	if [ -z "$REV" ]; then
		REV="@{u}"
	fi
	declare -a PATCHES
	PATCHES=( $(gd --name-only "$REV" -- 'patches.*') )
	PATCHES=( $(git grep -hE "`join_by '|' \"${PATCHES[@]}\"`" series.conf) )
#	| grep -v '^\s*#'`
	${EDITOR:-vim} "${PATCHES[@]}"
}

alias ping_waiting_net='echo | nc -N 185.219.167.24 60000'

alias cdl='cd_existing ~/linux /dev/shm/jslaby/linux ~/linux.git ~/labs/linux.git'
alias cds='cd ~/tmp/stable-4.12'
alias cdsl='cd_existing ~/repos/slaby-scripts/ ~/labs/slaby-scripts/ ~/slaby-scripts/'
alias cdq='cd ~/repos/stable-queue/'
alias cdx='cd ~/repos/xf86-video-intel/'
alias cdk='cd_existing ~/kernel-source ~/repos/suse/kernel-source/'
alias shm='cd_existing /dev/shm/jslaby/ /dev/shm/'
