1#! /bin/sh
2#
3# Automated Testing Framework (atf)
4#
5# Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
18# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Generates a header file with information about the revision used to
33# build ATF.
34#
35
36set -e
37
38Prog_Name=`echo "${0}" | sed 's;.*/;;'`
39
40MTN=
41ROOT=
42
43#
44# err message
45#
46err() {
47    echo "${Prog_Name}: ${@}" 1>&2
48    exit 1
49}
50
51#
52# call_mtn args
53#
54call_mtn() {
55    ${MTN} --root=${ROOT} "${@}"
56}
57
58# extract_certs rev
59#
60extract_certs() {
61    call_mtn automate certs ${1} | awk '
62BEGIN {
63    current_cert = ""
64}
65
66/^[ \t]*name "([^"]*)"[ \t]*$/ {
67    current_cert = substr($2, 2, length($2) - 2)
68    next
69}
70
71/^[ \t]*value "([^"]*)"[ \t]*$/ {
72    value = substr($2, 2, length($2) - 2)
73    if (current_cert == "branch") {
74        print "rev_branch=\"" value "\""
75    } else if (current_cert == "date") {
76        print "rev_date=\"" value "\""
77    }
78    next
79}
80'
81}
82
83#
84# get_rev_info_into_vars
85#
86# Sets the following variables to describe the current revision of the tree:
87#    rev_base_id: The base revision identifier.
88#    rev_branch: The branch name.
89#    rev_modified: true if the tree has been locally modified.
90#    rev_date: The date of the revision.
91#
92get_rev_info_into_vars() {
93    rev_base_id=`call_mtn automate get_base_revision_id`
94
95    if call_mtn status | grep "no changes" >/dev/null; then
96        rev_modified=false
97    else
98        rev_modified=true
99    fi
100
101    # The following defines several rev_* variables.
102    eval `extract_certs ${rev_base_id}`
103}
104
105#
106# generate_from_dist revfile version
107#
108generate_from_dist() {
109    revfile=${1}; shift
110    version=${1}; shift
111
112    >${revfile}
113
114    echo "#define PACKAGE_REVISION_TYPE_DIST" >>${revfile}
115}
116
117#
118# generate_from_mtn revfile
119#
120generate_from_mtn() {
121    revfile=${1}
122
123    get_rev_info_into_vars
124
125    >${revfile}
126
127    echo "#define PACKAGE_REVISION_TYPE_MTN" >>${revfile}
128
129    echo "#define PACKAGE_REVISION_BRANCH \"${rev_branch}\"" >>${revfile}
130    echo "#define PACKAGE_REVISION_BASE \"${rev_base_id}\"" >>${revfile}
131
132    if [ ${rev_modified} = true ]; then
133        echo "#define PACKAGE_REVISION_MODIFIED 1" >>${revfile}
134    fi
135
136    echo "#define PACKAGE_REVISION_DATE \"${rev_date}\"" >>${revfile}
137}
138
139#
140# main
141#
142# Entry point.
143#
144main() {
145    outfile=
146    version=
147    while getopts :m:r:o:v: arg; do
148        case ${arg} in
149            m)
150                MTN=${OPTARG}
151                ;;
152            o)
153                outfile=${OPTARG}
154                ;;
155            r)
156                ROOT=${OPTARG}
157                ;;
158            v)
159                version=${OPTARG}
160                ;;
161            *)
162                err "Unknown option ${arg}"
163                ;;
164        esac
165    done
166    [ -n "${ROOT}" ] || \
167        err "Must specify the top-level source directory with -r"
168    [ -n "${outfile}" ] || \
169        err "Must specify an output file with -o"
170    [ -n "${version}" ] || \
171        err "Must specify a version number with -v"
172
173    if [ -n "${MTN}" -a -d ${ROOT}/_MTN ]; then
174        generate_from_mtn ${outfile}
175    else
176        generate_from_dist ${outfile} ${version}
177    fi
178}
179
180main "${@}"
181
182# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
183