#!/bin/ash -efu
#
# mki-create-workdir
#
# This file is part of mkimage
# Copyright (C) 2008-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

include_without_workdir_check=1
. "${0%/*}"/mki-sh-functions

verbose "has started executing."

workroot="${GLOBAL_WORKROOT-}"
pkgboxdir="${PKGBOX:?directory required}"

relocation_warning() {
	message "WARNING: '$dir' will change location. We should cleanup old location."
	"$bindir/mki-clean"
}

if [ -d "$workroot" ]; then
	workroot="$(readlink -ev -- "$workroot")"

	if [ -h "$dir" ]; then
		if [ "$workroot" != "$(readlink -ev -- "$dir/..")" ]; then
			relocation_warning
			target="$(readlink -ev -- "$dir")" ||:
			rm $verbose -rf -- "$target" "$dir"
		fi
	elif [ -d "$dir" ]; then
		relocation_warning
		rm $verbose -rf -- "$dir"
	fi

	if [ ! -h "$dir" ]; then
		tempdir="$(mktemp -d -- "$workroot/mkimage.${curdir##*/}.XXXXXXXXX")" &&
			ln $verbose -sf -- "$tempdir" "$dir"
	fi
else
	if [ -h "$dir" ]; then
		relocation_warning
		target="$(readlink -ev -- "$dir")" ||:
		rm $verbose -rf -- "$target" "$dir"
	fi
	mkdir $verbose -p -- "$dir"
fi

mkdir $verbose -p -- "$pkgboxdir" "$outdir" "$cachedir/mki" "$cachedir/hsh"
