#alias sa='ssh aisa -CX'
alias sb="ssh bellona -CX"
alias sd='ssh decibel -CX'
alias san='ssh anemoi -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 gdwb='git diff -w -b'
alias glm='git log --author=jirislaby'
alias ref='run_existing ./refresh_patch ~/kernel-source/scripts/refresh_patch'
alias qd='quilt diff'
alias qdig='echo "vim `quilt next`" | xclip; dig_patch `quilt next`'
alias qpr='quilt push && ref'
alias rapid='./scripts/sequence-patch --quilt --dir=/dev/shm/jslaby/ --rapid'

alias tigl='tig -1000'
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

SL_DIALOG=(dialog --keep-tite --cr-wrap)

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

function run_existing() {
	for FILE; do
		if [ -x "$FILE" ]; then
			"$FILE"
			break
		fi
	done
}

function debugin() {
	zypper -v in --no-recommends $(rpm --qf '%{name}-debuginfo\n' -qf "$@")
}

function fastbuildconfig() {
	sed -E -i -e 's/(CONFIG_(IKCONFIG|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 gf() {
	declare -a FETCH=()
	for BRANCH in "${@:-$(git rev-parse --abbrev-ref HEAD)}"; do
		FETCH+=( "+refs/heads/$BRANCH:refs/remotes/origin/$BRANCH" )
	done
	git fetch origin "${FETCH[@]}"
}

function gfr() {
	local BRANCH="${1:-`git rev-parse --abbrev-ref HEAD`}"
	gf "$BRANCH"
	if git show-ref --quiet --branches "$BRANCH"; then
		git rebase "origin/$BRANCH" "$BRANCH"
	else
		git checkout -b "$BRANCH" "origin/$BRANCH"
	fi
}

function gpu() {
	local FORCE=
	if [ "$1" = -f ]; then
		FORCE=+
		shift
	fi
	declare -a PUSH=()
	for BRANCH in "${@:-$(git rev-parse --abbrev-ref HEAD)}"; do
		PUSH+=( "${FORCE}refs/heads/$BRANCH:refs/heads/users/jslaby/$BRANCH/for-next" )
	done
	git push origin "${PUSH[@]}"
}

function get_maintainer() {
	local WHAT="$1"
	test -z "$WHAT" && WHAT="@^"
	local 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() {
	local 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 sl_ds() {
	diffstat -l -p1 `gd @{u} --name-only -- patches.suse/`
}

function show_patch {
	local SHA="$1"
	local HEAD="$2"
	readarray -t 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 vimconfl() {
	mapfile -d '' -t FILES < <(git diff --name-only -z | sort -zu)
	test "${#FILES[@]}" -eq 0 && return
	vim "${FILES[@]}" '+/<<<<<<<'
	local MSG=$'Add to git?\n\n'"$(join_by $'\n' "${FILES[@]}")"
	if "${SL_DIALOG[@]}" --yesno "$MSG" 0 0; then
		git add "${FILES[@]}"
		if "${SL_DIALOG[@]}" --yesno "Continue rebase?" 0 0; then
			git rebase --continue
		fi
	fi
}

function vimp() {
	local REV="$1"
	if [ -z "$REV" ]; then
		REV="@{u}"
	fi
	readarray -t PATCHES < <(git diff --name-only "$REV" -- 'patches.*' | git grep -ohFf - series.conf)
#	| grep -v '^\s*#'`
	${EDITOR:-vim} "${PATCHES[@]}"
}

alias pstat='EDITOR='\''diffstat -lp1'\'' vimp'
function pstato() {
	pstat "$@" | sed -n 's@c$@o@ p'
}

function tign() {
	tig ^korg/next_master "${1:-@}"
}
function tigu() {
	tig ^@{u} "${1:-@}"
}

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 cdsl='cd_existing ~/my-repos/slaby-scripts/ ~/labs/slaby-scripts/ ~/slaby-scripts/'
alias cdq='cd ~/repos/stable-queue/'
alias cdk='cd_existing ~/kernel-source ~/repos/suse/kernel-source/'
alias cdkk='cd /dev/shm/jslaby/kernel-source'
alias shm='cd_existing /dev/shm/jslaby/ /dev/shm/'
