#!/bin/sh

#PATH=$HOME/bin:/bin:/usr/bin
#export PATH

. autorepo-config
. autorepo-lock-sh-functions
. autorepo-mail-sh-functions

autorepo_lock_workdir_or_exit

autorepo_daily_managed=
case $0 in
    *-managed)
	autorepo_daily_managed=1
	;;
esac

VERBOSE=
[ "$1" == "-v" ] && VERBOSE=-v

if [ -z "$autorepo_daily_managed" ]; then
    logfile=$AUTOREPO_HOME/autorepo-daily.log
    [ -z "$VERBOSE" ] || echo "Running autorepo-run ..."
    if ! autorepo-run > $logfile 2>&1; then
	send_failure_mail $logfile
    fi
elif [ -n "$AUTOREPO_MERGE_ROOT" ]; then
    cmd=$AUTOREPO_HOME/bin/autorepo-rsync-local
    [ -x $cmd ] && $cmd
    [ -z "$VERBOSE" ] || echo "Running autorepo-merge-repositories ..."
    autorepo-merge-repositories >/dev/null
elif [ -z "$AUTOREPO_NO_REBUILD_UNMETS" ]; then
    [ -z "$VERBOSE" ] || echo "Running autorepo-try-rebuild-unmets ..."
    autorepo-repo-unmets
    autorepo-try-rebuild-unmets
fi

[ -z "$VERBOSE" ] || echo "Running autorepo-cybertalk ..."
autorepo-cybertalk
[ -z "$VERBOSE" ] || echo "Running autorepo-archive ..."
autorepo-archive
[ -z "$VERBOSE" ] || echo "Running autorepo-care ..."
autorepo-care $VERBOSE

cmd=/usr/bin/autorepo-altnode-builder-statistics
[ -x $cmd ] && $cmd
autorepo_unlock_workdir_safe
