#!/bin/sh
#
# Update all sbuild source chroots.
#
# Written by Russ Allbery <eagle@eyrie.org>
#
# The authors hereby relinquish any claim to any copyright that they may have
# in this work, whether granted under contract or by operation of law or
# international treaty, and hereby commit to the public, at large, that they
# shall not, at any time in the future, seek to enforce any copyright in this
# work against any person or entity, or prevent any person or entity from
# copying, publishing, distributing or creating derivative works of this work.

set -e

# Ensure a sane PATH.
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Support a delay.  This really shouldn't be necessary, but I can't figure out
# how to configure systemd to not trigger the timer after resuming from
# hibernate until the network is up.  So add a way to delay the update by a
# given number of seconds.
delay=
if [ "$1" = "-d" ]; then
    if [ -z "$2" ]; then
        echo 'sbuild-update-all: -d requires an argument' >&2
        exit 1
    fi
    delay="$2"
    shift
    shift
fi

# Address to which to email the results.
if [ -z "$1" ]; then
    echo 'sbuild-update-all: no email address given' >&2
    exit 1
fi
MAIL="$1"

# Delay if requested.
if [ -n "$delay" ]; then
    sleep "$delay"
fi

# Run sbuild-update on every source chroot.
for chroot in $(schroot -l --exclude-aliases | grep ^source:); do
    (sbuild-update -udcar "$chroot" 2>&1) \
        | mail -s "sbuild $chroot update" "$MAIL"
done

# All done.
exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Allbery sbuild sbuild-update-all sbuild-update systemd

=head1 NAME

sbuild-update-all - Update all sbuild source chroots

=head1 SYNOPSIS

B<sbuild-update-all> [B<-d> I<delay>] I<email>

=head1 DESCRIPTION

B<sbuild-update-all> finds all source sbuild chroots and then runs
C<sbuild-update -udcar> on each one, mailing the results to the provided email
address.

=head1 OPTIONS

=over 4

=item B<-d> I<delay>

Delay by I<delay> before doing the update.  I<delay> can be any argument
understood by B<sleep>.  This is a hack to wait for a laptop to finish
bringing the network up after resuming from hibernation.  There should be some
better way to do this with systemd timers and dependencies, but I wasn't able
to find one.

=back

=head1 SEE ALSO

sbuild-update(1), schroot(1), sleep(1)

=head1 AUTHOR

Russ Allbery <eagle@eyrie.org>

=cut
__END_OF_DOCS__
