#!/bin/sh

# TODO:
# 1) move exclude arch test into arch code in autorepo_calculate_unmets
# 2) support extra parallelization by arch
# 3) get rid of global logdir in autorepo_set_logdir? or transform into an arch logdir?
# 3) fix global /tm/WD1,2 in arch_autorepo_calculate_unmets?

# for get_apt_conf_for_task
. autorepo-apt-sh-functions

autorepo_set_logdir()
{
    logdir=$AUTOREPO_HOME/logs$hashernumber
}

set_hasher_dir()
{
    local arch
    arch=$1
    current_hasherdir="$AUTOREPO_HASHER_PREFIX$hashernumber.$arch"
}

arch_build()
{
    local arch exit_build_status
    exit_build_status=0
    arch=$1
    shift;
    target=$arch
    aptconf_for_task=
    get_apt_conf_for_task $target build
    autorepo_set_logdir
    set_hasher_dir "$arch"
    mkdir -p "$current_hasherdir"
    echo $arch hsh "$current_hasherdir" ${hashernumber:+--number $hashernumber} $AUTOREPO_HASHERARGS $AUTOREPO_HASHER_BUILD_ARGS --target=$arch --apt-config=$aptconf_for_task "$@"
    $arch hsh "$current_hasherdir" ${hashernumber:+--number $hashernumber} $AUTOREPO_HASHERARGS $AUTOREPO_HASHER_BUILD_ARGS --target=$arch --apt-config=$aptconf_for_task "$@" >"$logdir"/hsh.log.$arch 2>&1
    if [ "$?" -gt 0 ]; then
	if egrep "^error: Architecture is (not in|ex)cluded: $arch" "$logdir"/hsh.log.$arch >/dev/null; then
	    touch "$logdir"/exclude.$arch
	else
	    exit_build_status=3
	fi
    fi
    return $exit_build_status
}

arch_cleanup_chroot()
{
    local arch
    arch=$1
    shift;
    set_hasher_dir "$arch"
    [ -d "$current_hasherdir/chroot" ] && hsh-rmchroot ${hashernumber:+--number $hashernumber} "$current_hasherdir"
}

cleanup_chroot()
{
    for arch in $GB_ARCH; do
	arch_cleanup_chroot $arch
    done
}

arch_autorepo_calculate_unmets()
{
    local target difffile
    target=$1
    difffile=$2

    tmp=$TMPDIR/autorepo_calculate_unmets$hashernumber.$target
    aptconflocal=$tmp/apt.conf.$target
    srclistlocal=$tmp/sources.list.$target
    aptconfremote="$AUTOREPO_LOCAL_APT_DIR/apt.conf.$target"
    srclistremote="$AUTOREPO_LOCAL_APT_DIR/sources.list.$target"
    WORKDIR1=$tmp/WD1
    WORKDIR2=$tmp/WD2

cleanup_tmpdir()
    {
	rm -rf $tmp
    }
    
    cleanup_tmpdir
    mkdir -p $WORKDIR1 $WORKDIR2

    # use APT::Cache-Limit from apt.conf
    sed -e '/^Dir::Etc::sourcelist/d' $aptconfremote > $aptconflocal
    cat >> $aptconflocal <<EOF
Dir::Etc::sourcelist "$srclistlocal";
EOF
    cp $srclistremote $srclistlocal
    set_hasher_dir "$target"
    echo "rpm-dir file:$current_hasherdir/repo ${target} hasher" >> $srclistlocal

    mkaptbox --apt-config=$aptconflocal $WORKDIR2
    $WORKDIR2/aptbox/apt-cache unmet > $tmp/unmets.new
    if [ -s $tmp/unmets.new ]; then
	mkaptbox --apt-config=$aptconfremote $WORKDIR1
	$WORKDIR1/aptbox/apt-cache unmet > $tmp/unmets.old
	diff -U0 $tmp/unmets.{old,new} > $difffile
	if ! egrep -v '^(\+\+\+|@@|-)' $difffile; then
	    # unmets are purely removed, not added
	    rm -f $difffile
	    touch $difffile
	fi
    else
	rm -f $difffile
	touch $difffile
    fi
    cleanup_tmpdir
}

arch_autorepo_install_test_install_file()
{
    local install_status arch rpm logfile initroot_stamp initroot_created
    install_status=0
    arch="$1"
    rpm="$2"
    logfile="$3"
    initroot_stamp="$4"
    shift; shift; shift; shift;
    target=$arch
    aptconf_for_task=
    get_apt_conf_for_task $target build
    initroot_created=
    set_hasher_dir "$arch"
    mkdir -p "$current_hasherdir"
    if ! [ -f "$initroot_stamp" ]; then
	# trying to reuse existing chroot; not doing --initroot-only if already done
	echo $arch hsh --target=$arch --with-stuff ${hashernumber:+--number $hashernumber} "$current_hasherdir" $AUTOREPO_HASHERARGS --apt-config=$aptconf_for_task --initroot-only
	$arch hsh --target=$arch --with-stuff ${hashernumber:+--number $hashernumber} "$current_hasherdir" $AUTOREPO_HASHERARGS --apt-config=$aptconf_for_task --initroot-only > "$logfile" 2>&1
	touch "$initroot_stamp"
	initroot_created=yes
    fi
    echo $arch hsh-install $AUTOREPO_HASHERARGS "$current_hasherdir" "$rpm"
    $arch hsh-install $AUTOREPO_HASHERARGS "$current_hasherdir" "$rpm" >> "$logfile" 2>&1
    if [ "$?" -gt 0 ]; then
	if [ -n "$initroot_created" ]; then
	    install_status=5
	else
	    # reinitializing chroot as installation failed in shared chroot
	    echo $arch hsh --target=$arch --with-stuff ${hashernumber:+--number $hashernumber} "$current_hasherdir" $AUTOREPO_HASHERARGS $AUTOREPO_HSH_ARGS --apt-config=$aptconf_for_task --initroot-only
	    $arch hsh --target=$arch --with-stuff ${hashernumber:+--number $hashernumber} "$current_hasherdir" $AUTOREPO_HASHERARGS $AUTOREPO_HSH_ARGS --apt-config=$aptconf_for_task --initroot-only > "$logfile" 2>&1
	    echo $arch hsh-install $AUTOREPO_HASHERARGS $AUTOREPO_HSH_INSTALL_ARGS "$current_hasherdir" "$rpm"
	    $arch hsh-install $AUTOREPO_HASHERARGS $AUTOREPO_HSH_INSTALL_ARGS "$current_hasherdir" "$rpm" >> "$logfile" 2>&1
	    if [ "$?" -gt 0 ]; then
		install_status=5
	    fi
	fi
    fi
    return $install_status
}

arch_autorepo_install_test()
{
    local arch exit_install_status
    arch=$1
    exit_install_status=0
    autorepo_set_logdir
    initroot_stamp="$logdir"/initroot-${arch}.done
    rm -f "$initroot_stamp"
    if ! [ -e "$logdir"/exclude.$arch ]; then
	set_hasher_dir "$arch"
	for rpm in "$current_hasherdir"/repo/$arch/RPMS.hasher/*.rpm; do
	    if [ -e "$rpm" ]; then # -e because w/o nullglob
		logfile="$logdir"/`basename $rpm`-${arch}-install.log
		if arch_autorepo_install_test_install_file $arch "$rpm" "$logfile" "$initroot_stamp"; then
		    rm -f "$logfile"
		else
		    echo "$rpm: install test failed"
		    exit_install_status=5
		fi
	    fi
	done
    fi
    return $exit_install_status
}



autorepo_build()
{
    local exit_build_status
    exit_build_status=0
    autorepo_set_logdir
    for arch in $GB_ARCH; do
	if ! arch_build $arch "$@"; then
	    exit_build_status=3
	    if [ "$rapid_drop_mode" -gt 0 ]; then
		echo "rebuild failed."
		cleanup_chroot
		exit $exit_build_status
	    fi
	fi
    done
    
    if [ "$exit_build_status" -gt 0 ]; then
	echo "rebuild failed."
	cleanup_chroot
	exit $exit_build_status
    fi

    touch "$logdir"/build.success
}

autorepo_calculate_unmets()
{
    local unmets_status
    unmets_status=0
    autorepo_set_logdir
    for arch in $GB_ARCH; do
	mkdir -p "$logdir"
	rm -f "$logdir"/unmets.diff.$arch
	[ -e "$logdir"/exclude.$arch ] || arch_autorepo_calculate_unmets $arch "$logdir"/unmets.diff.$arch
	[ -s "$logdir"/unmets.diff.$arch ] && unmets_status=4
    done

    if [ "$unmets_status" -gt 0 ]; then
	echo "unmets test failed."
	cleanup_chroot
	exit $unmets_status
    fi

    touch "$logdir"/unmets.success
}

autorepo_install_test()
{
    local exit_install_status
    exit_install_status=0
    autorepo_set_logdir
    for arch in $GB_ARCH; do
	if ! arch_autorepo_install_test $arch; then
	    exit_install_status=5
	fi
    done

    if [ "$exit_install_status" -gt 0 ]; then
	echo "install test failed."
	cleanup_chroot
	exit $exit_install_status
    fi

    touch "$logdir"/install.success
}
