#!/bin/sh

export LANG=C
export LANGUAGE=C

PROG=`basename $0`

show_help()
{
#  -q, --quiet                       try to be more quiet;
#  -v, --verbose                     print a message for each action;
	cat <<EOF
$PROG - script to cleanup the set of used hasher dirs in parallel.
Usage: $PROG [options]

Options:
  -n <N>,--number <N> number of hasher to clean up
  -K,--keep-hasher-cache keep hasher cache
  -C,--cleanup-hasher-cache	 cleanup hasher cache
  -h, --help	show this text and exit.

Report bugs to http://bugzilla.altlinux.org/
EOF
	exit
}

rm_hashercache=
. autorepo-build-config
hasher_jobs=$AR_BUILDER_DEFAULT_PARLLEL_JOBS

TEMP=`getopt -n $PROG -o Cn:Kh -l number:,keep-hasher-cache,no-keep-hasher-cache,cleanup-hasher-cache,no-cleanup-hasher-cache,help -- "$@"` ||
	show_help
eval set -- "$TEMP"

while :; do
    case "$1" in
	-C|--cleanup-hasher-cache) rm_hashercache=1
		;;
	-K|--keep-hasher-cache) rm_hashercache=
		;;
	--no-cleanup-hasher-cache) rm_hashercache=
		;;
	--no-keep-hasher-cache) rm_hashercache=1
		;;
	-n|--number) hashernumber=$2; shift;
		;;
	-h|--help) show_help
		;;
	--) shift; break
		;;
	*) echo "Error parsing arguments. see --help" ; exit 1
		;;
    esac
    shift
done

. autorepo-build-sh-functions

autorepo_hsh_cleanup()
{
    for arch in $GB_ARCH; do
	set_hasher_dir "$arch"
	if [ -d "$current_hasherdir"/chroot ]; then
	    hsh-rmchroot ${hashernumber:+--number $hashernumber} "$current_hasherdir"
	fi
	rm -rf "$current_hasherdir"
    done
}

if [ -n "$hashernumber" ]; then
    autorepo_hsh_cleanup
fi

if [ -n "$rm_hashercache" ]; then
    hashernumber=
    autorepo_hsh_cleanup
fi
