#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 --match='v*'"
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 "${EDITOR:-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
		local DEST="for-next"
		if [ "$BRANCH" = "stable" ]; then
			DEST="for-merge"
		fi
		PUSH+=( "${FORCE}refs/heads/$BRANCH:refs/heads/users/jslaby/$BRANCH/$DEST" )
	done
	git push origin "${PUSH[@]}"
}

function sl_get_maintainer() {
	local WHAT="${1:-@^}"
	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 git_what_cont() {
	local GIT_DIR=$(git rev-parse --git-dir)
	if [ -f "$GIT_DIR/rebase-apply/applying" ]; then
		echo am
	elif [ -d "$GIT_DIR/rebase-merge/" -o -d "$GIT_DIR/rebase-apply/" ]; then
		echo rebase
	elif [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
		echo cherry-pick
	fi
}

function vimconfl() {
	mapfile -d '' -t FILES < <(git diff --name-only -z | sort -zu)
	test "${#FILES[@]}" -eq 0 && return
	${EDITOR:-vim} "${FILES[@]}" '+/<<<<<<<'
	local MSG=$'Add to git?\n\n'"$(join_by $'\n' "${FILES[@]}")"
	if "${SL_DIALOG[@]}" --yesno "$MSG" 0 0; then
		git add "${FILES[@]}"
		local CMD="$(git_what_cont)"
		if [ -n "$CMD" ] && "${SL_DIALOG[@]}" --yesno "Continue $CMD?" 0 0; then
			git "$CMD" --continue
		fi
	fi
}

function vimp() {
	local REV="${1:-@{u\}}"
	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:-@}"
}

function sl_osc_build() {
	local -a pkgs=( -x vim -x vim-data -x strace )
	local -a opts=( --download-api-only --noverify "${pkgs[@]}" )
	local ccache=1
	local api=
	local dir=
	local rel=101

	local OPTIND opt OPTARG
	while getopts "cd:ir:v" opt; do
		case "$opt" in
			c) ccache=0 ;;
			d) dir="xx$OPTARG" ;;
			i) api='api.suse.de' ;;
			r) rel="$OPTARG" ;;
			v) dir="${dir:-xxvm}"
			   opts+=( '--vm-type=kvm' '--vm-memory=20G' )
			   ;;
		esac
	done
	shift $((OPTIND-1))

	[ $ccache -eq 1 ] && opts+=( "--ccache" )
	[ -n "$api" ] && opts+=( "--apiurl=$api" )
	opts+=( "--root=/dev/shm/jslaby/${dir:-xxx}" )
	opts+=( "--release=$rel" )

	trap 'set +x' RETURN
	set -x
	CCACHE_DIR="" osc build "${opts[@]}" "$@"
}

function __sl_fetch_tar() {
	gf scripts && \
	./scripts/tar-up
}

function sl_upload_rc() {
	__sl_fetch_tar && \
	./scripts/osc_wrapper upload --ibs home:jirislaby:stable-rc
}

function sl_upload_stable() {
	local -a maintainers=( jirislaby kernelbugs michals sthackarajan factory-maintainers )
	local -a opts=( --debug )
	for m in "${maintainers[@]}"; do
		opts+=( "--maintainer=$m" )
	done

	__sl_fetch_tar && \
	./scripts/osc_wrapper upload "${opts[@]}" Kernel:stable
}

alias ping_waiting_net='echo | nc -N "$VEREJNA" 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/'
