#!/bin/sh
# Depends: tmux, schroot, debcheckout, git
# DSA-Depends: dd-schroot-cmd
# Recommends: quilt

set -e

abort() { x=$1; shift; echo >&2 "$@"; exit $x; }

varfile=
schroot=sid
bmaster=master
bupstream=upstream
destroy=false
tmuxhist=16384
while getopts 'f:c:d:u:h:x' o; do
	case $o in
	f )	varfile="$OPTARG";;
	c )	schroot="$OPTARG";;
	d )	bmaster="$OPTARG";;
	u )	bupstream="$OPTARG";;
	h )	tmuxhist="$OPTARG";;
	x )	destroy=true;;
	esac
done
shift $(expr $OPTIND - 1)

pkg="$1"
test -n "$pkg" -o -n "$varfile" || abort 2 "either \$1 or -f need to be given"
varfile="${varfile:-$pkg.session}"

runself() { "$@" -- "$SHELL" -c "'$0' -f '$varfile' || '$SHELL'"; }

if $destroy; then
	. "$(readlink -f "$varfile")"
	echo >&2 "$0: -x given; destroying schroot $ssid ..."

	windowname=$(printf '%s' "$varfile" | tr '.' '-') # . is special in tmux syntax :/
	tmux kill-window -t "dd-porter:$windowname" || true
	exec schroot -e -c $ssid

elif [ -z "$TMUX" -a -z "$SCHROOT_SESSION_ID" ]; then
	# TMUX is not set in schroot, even if we're actually in a tmux. so check for SCHROOT_SESSION_ID as well
	echo >&2 "$0: detected not in tmux or schroot ..."

	if ! [ -f ~/.tmux.conf ]; then
		echo "set -g history-limit $tmuxhist" > ~/.tmux.conf
	else
		sed -i -e "s,^set -g history-limit .*,set -g history-limit $tmuxhist,g" ~/.tmux.conf
	fi

	if ! [ -f "$varfile" ]; then
		test -n "$pkg"
		echo pkg="$pkg" > "$varfile"
		echo schroot="$schroot" >> "$varfile"
		echo bmaster="$bmaster" >> "$varfile"
		echo bupstream="$bupstream" >> "$varfile"
		echo ssid="uninitialized" >> "$varfile"
	else
		echo >&2 "$0: reading existing session from $varfile and ignoring command-line options ..."
		cat >&2 "$(readlink -f "$varfile")"
		. "$(readlink -f "$varfile")"
	fi

	if ! schroot --config -c "session:$(sed -ne s/^ssid=//p "$varfile")" >/dev/null 2>&1; then
		echo >&2 "$0: detected obsolete session, creating a new one ..."
		ssid=$(schroot -b -c "$schroot")
		sed -i -e "s,^ssid=.*,ssid=$ssid,g" "$varfile"
	fi

	echo >&2 "$0: entering tmux ..."

	windowname=$(printf '%s' "$varfile" | tr '.' '-') # . is special in tmux syntax :/
	# create a new-session or a new-window if it doesn't exist
	if ! tmux has-session -t dd-porter; then
		runself tmux new-session -d -s dd-porter -n "$windowname"
	elif ! tmux list-windows -t dd-porter -F '#W' | grep -qF "$windowname"; then
		runself tmux new-window -t dd-porter -n "$windowname"
	fi
	# attach to the window
	exec tmux attach -t "dd-porter:$windowname"

elif [ -z "$SCHROOT_SESSION_ID" ]; then
	. "$(readlink -f "$varfile")"
	echo >&2 "$0: detected in tmux but not in schroot; updating and entering schroot $ssid ..."

	dd-schroot-cmd -c "$ssid" apt-get update
	dd-schroot-cmd -c "$ssid" apt-get upgrade -y
	dd-schroot-cmd -c "$ssid" apt-get build-dep -y "$pkg"
	dd-schroot-cmd -c "$ssid" apt-get install -y devscripts equivs quilt nano git git-buildpackage pristine-tar ca-certificates
	runself exec schroot -r -c "$ssid"

else
	. "$(readlink -f "$varfile")"
	echo >&2 "$0: detected in schroot; checking out source of $pkg ..."

	if [ -d "$pkg" ]; then
		cd "$pkg"
		if git diff --quiet; then
			git checkout "$bmaster"
			git pull
			git branch -f "$bupstream" "origin/$bupstream"
			git branch -f pristine-tar origin/pristine-tar
		else
			echo >&2 "$0: not updating git because the working tree is dirty"
		fi
	else
		debcheckout "$pkg"
		cd "$pkg"
		git checkout pristine-tar
		git checkout "$bupstream"
		git checkout "$bmaster"
	fi
	export QUILT_PATCHES=debian/patches
	exec $SHELL

fi
