#!/bin/sh -eu
set -o pipefail

if [ -d ~/src/repo/logoved.git/ ]; then
    export PATH=~/src/repo/logoved.git:$PATH
fi

. logoved-iteration-sh-functions

# $1 can be unbound, thus ${1-}
tasktype=${1-}
if [ -z "$tasktype" ]; then
    tasktype=up
fi

if ! [ -d OUT ]; then
    echo "Fatal: input dir ./OUT not found. Exiting..."
    exit 1
fi

prepare_repository

#export LOGOVED_REPORT_OPTIONS=--addon=AUTOIMPORT
set -x

rm -rf TASK.*
rebuild_iteration $tasktype

autorepo-buildhelper-cleanup-hashers ${use_hashercache:+-C} ${keep_hashercache:+-K} -j $nproc

if [ -d OUT ] || stat -t TASK.* >/dev/null 2>&1; then
    echo "Aborted: Found unexpected OUT or TASK.*"
    exit 1
fi
logoved-autorepo-helper-merge-task-goodies
