#!/bin/sh -efu

. gb-sh-functions

id="$1"; shift
cd "$GB_TASKS_DIR/$id"

[ "$(cat task/state)" = DONE ] ||
	exit 0

cd "$GB_TASKS_DIR"
k="_$(($id/1024))"
destdir="$GB_TASKS_DONE_DIR/$k"
if [ ! -d "$destdir" ]; then
	mkdir -- "$destdir"
	if [ -L "$GB_TASKS_DONE_DIR/current" ]; then
		rm -f -- "$GB_TASKS_DONE_DIR/previous"
		mv -- "$GB_TASKS_DONE_DIR/current" \
			"$GB_TASKS_DONE_DIR/previous"
	fi
	ln -s -- "$k" "$GB_TASKS_DONE_DIR/current"

fi
mv -- "$id" "$destdir/"
ln -snf ../../.."${GB_TASKS_DONE_DIR#$GB_TASKS_DIR}/$k/$id" \
	index/"$GB_REPO_NAME"/done/

gb-y-awake-tasks "$id" ||:
