1#! /bin/bash
2
3########################################################################
4#
5# File:    reg_periodic
6# Author:  Janis Johnson
7# Date:    2002/12/28
8#
9# Over a range of dates at specified intervals, invoke separate tools to
10# update sources, do a build, and run one or more tests. 
11#
12# Define these in a file whose name is the argument to this script:
13#   LOW_DATE:   Date string recognized by the date command.
14#   HIGH_DATE:  Date string recognized by the date command.
15#   INTERVAL:   Time (in seconds) between dates for which to build.
16#   REG_UPDATE: Pathname of script to update your source tree.
17#   REG_BUILD:  Pathname of script to build enough of the product to run
18#               the test.
19#   REG_TEST:   Pathname of script to run one or more tests.
20# Optional:
21#   VERBOSITY:  Default is 0, to print only errors and final message.
22#   DATE_IN_MSG If set to anything but 0, include the time and date in
23#               messages
24#   REG_STOP    Pathname of a file whose existence says to quit; default
25#               is STOP in the current directory.
26#
27#
28# Copyright (c) 2002, 2003, 2005, 2009, 2010 Free Software Foundation, Inc.
29#
30# This file is free software; you can redistribute it and/or modify
31# it under the terms of the GNU General Public License as published by
32# the Free Software Foundation; either version 3 of the License, or
33# (at your option) any later version.
34#
35# This program is distributed in the hope that it will be useful,
36# but WITHOUT ANY WARRANTY; without even the implied warranty of
37# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38# GNU General Public License for more details.
39#
40# You should have received a copy of the GNU General Public License
41# along with this program; see the file COPYING3.  If not see
42# <http://www.gnu.org/licenses/>.
43#
44########################################################################
45
46########################################################################
47# Functions
48########################################################################
49
50# Issue a message if its verbosity level is high enough.
51
52msg() {
53  test ${1} -gt ${VERBOSITY}  && return
54
55  if [ "x${DATE_IN_MSG}" = "x" ]; then
56    echo "${2}"
57  else
58    echo "`${DATE}`  ${2}"
59  fi
60}
61
62# Issue an error message and exit with a nonzero status.
63
64error() {
65  msg 0 "error: ${1}"
66  exit 1
67}
68
69# Turn seconds since the epoch into a date we can use with source
70# control tools and report to the user.
71
72make_date() {
73  MADE_DATE=`${DATE} -u +"%Y-%m-%d %H:%M %Z" --date "1970-01-01 ${1} seconds"` \
74    || error "make_date: date command failed"
75}
76
77# Build the components to test using sources as of a particular date and
78# run a test case.  Pass each of the scripts the date that we're
79# testing; the first one needs it, the others can ignore it if they want.
80
81process_date() {
82  TEST_DATE="${1}"
83
84  ${REG_UPDATE} "${TEST_DATE}"
85  if [ $? -ne 0 ]; then
86    msg 0 "source update failed for ${TEST_DATE}"
87    return
88  fi
89  ${REG_BUILD} "${TEST_DATE}"
90  if [ $? -ne 0 ]; then
91    msg 0 "build failed for ${TEST_DATE}"
92    return
93  fi
94  ${REG_TEST} "${TEST_DATE}"
95}
96 
97########################################################################
98# Main program (so to speak)
99########################################################################
100
101# If DATE isn't defined, use the default date command; the configuration
102# file can override this.
103
104if [ "x${DATE}" = "x" ]; then
105  DATE=date
106fi
107
108# Process the configuration file.
109
110if [ $# -ne 1 ]; then
111  echo Usage: $0 config_file
112  exit 1
113fi
114
115CONFIG=${1}
116if [ ! -f ${CONFIG} ]; then
117  error "configuration file ${CONFIG} does not exist"
118fi
119
120# OK, the config file exists.  Source it, make sure required parameters
121# are defined and their files exist, and give default values to optional
122# parameters.
123
124. ${CONFIG}
125
126test "x${REG_UPDATE}" = "x" && error "REG_UPDATE is not defined"
127test "x${REG_BUILD}" = "x" && error "REG_BUILD is not defined"
128test "x${REG_TEST}" = "x" && error "REG_TEST is not defined"
129test "x${INTERVAL}" = "x" && error "INTERVAL is not defined"
130test -x ${REG_TEST} || error "REG_TEST is not an executable file"
131test "x${VERBOSITY}" = "x" && VERBOSITY=0
132test "x${REG_STOP}" = "x" && REG_STOP="STOP"
133
134msg 2 "LOW_DATE   = ${LOW_DATE}"
135msg 2 "HIGH_DATE  = ${HIGH_DATE}"
136msg 2 "INTERVAL   = ${INTERVAL}"
137msg 2 "REG_UPDATE = ${REG_UPDATE}"
138msg 2 "REG_BUILD  = ${REG_BUILD}"
139msg 2 "REG_TEST   = ${REG_TEST}"
140msg 2 "VERBOSITY  = ${VERBOSITY}"
141
142# Change the dates into seconds since the epoch.  This uses an extension
143# in GNU date.
144
145LOW_DATE=`${DATE} +%s --date "${LOW_DATE}"` || \
146  error "date command failed for \"${LOW_DATE}\""
147HIGH_DATE=`${DATE} +%s --date "${HIGH_DATE}"` || \
148  error "date command failed for \"${LOW_DATE}\""
149
150# Process each date in the range.
151
152while [ ${LOW_DATE} -le ${HIGH_DATE} ]; do
153
154  # If a file called STOP appears, stop; this allows a clean way to
155  # interrupt a search.
156
157  if [ -f ${REG_STOP} ]; then
158    msg 0 "STOP file detected"
159    rm -f ${REG_STOP}
160    exit 1
161  fi
162
163  # Get a version of the date that is usable by tools and readable
164  # by people, then process it.
165
166  make_date ${LOW_DATE}
167  process_date "${MADE_DATE}"
168  let LOW_DATE=LOW_DATE+INTERVAL
169done
170
171msg 1 "done"
172