Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / sysutils / sysbuild / files / sysbuild4cron.sh
blobbdb0924bcb0c381b6a10ead0796c9e7e95861a46
1 # Copyright 2012 Google Inc.
2 # All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Google Inc. nor the names of its contributors
14 # may be used to endorse or promote products derived from this software
15 # without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # \file sysbuild4cron.sh
30 # Entry point and main program logic.
32 shtk_import cli
35 # Paths to installed files.
37 # Can be overriden for test purposes only.
38 : ${SYSBUILD_BINDIR="@SYSBUILD_BINDIR@"}
41 # Sends an email notification upon a failure.
43 # \param logfile File containing the output of the failed command.
44 # \param recipient Who to send the email to.
45 # \param ... Command that failed.
46 sysbuild4cron_email() {
47 local logfile="${1}"; shift
48 local recipient="${1}"; shift
50 local maxlines=100
52 echo "The following command has failed:"
53 echo
54 echo " $*"
55 echo
56 echo "The output of the failed command has been left in:"
57 echo
58 echo " ${logfile}"
59 echo
60 echo "The last ${maxlines} of the log follow:"
61 echo
62 tail -n${maxlines} "${logfile}"
63 } | mail -s "sysbuild failure report" "${recipient}"
67 # Entry point to the program.
69 # \param ... Command-line arguments to be processed.
71 # \return An exit code to be returned to the user.
72 sysbuild4cron_main() {
73 local logdir="${HOME}/sysbuild/log"
74 local recipient="$(id -u -n)"
76 while getopts ':l:r:' arg "${@}"; do
77 case "${arg}" in
78 l) # Location of logs.
79 case "${OPTARG}" in
80 /*) logdir="${OPTARG}" ;;
81 *) logdir="$(pwd)/${OPTARG}" ;;
82 esac
85 r) # Email recipient.
86 recipient="${OPTARG}"
89 \?)
90 shtk_cli_usage_error "Unknown option -${OPTARG}"
92 esac
93 done
94 shift $((${OPTIND} - 1))
96 set -- "${SYSBUILD_BINDIR}/sysbuild" "${@}"
98 if [ ! -d "${logdir}" ]; then
99 mkdir -p "$(dirname "${logdir}")" >/dev/null 2>/dev/null || true
100 mkdir "${logdir}" \
101 || shtk_cli_error "Failed to create directory '${logdir}'"
104 local exit_code=0
105 local logfile="${logdir}/$(shtk_cli_progname).$(date +%Y%m%d%H%M%S).log"
106 "${@}" >"${logfile}" 2>&1 \
107 || sysbuild4cron_email "${logfile}" "${recipient}" "${@}"