1#! /bin/sh
2#
3# Automated Testing Framework (atf)
4#
5# Copyright (c) 2007 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=${0##*/}
39
40GIT=
41ROOT=
42
43#
44# err message
45#
46err() {
47    echo "${Prog_Name}: ${@}" 1>&2
48    exit 1
49}
50
51#
52# call_git args
53#
54call_git() {
55    ( cd "${ROOT}" && "${GIT}" "${@}" )
56}
57
58#
59# generate_from_dist revfile version
60#
61generate_from_dist() {
62    revfile=${1}; shift
63    version=${1}; shift
64
65    >${revfile}
66
67    echo "#define PACKAGE_REVISION_TYPE_DIST" >>${revfile}
68}
69
70#
71# generate_from_git revfile
72#
73generate_from_git() {
74    revfile=${1}
75
76    rev_base_id=$(call_git rev-parse HEAD)
77    rev_branch=$(call_git branch | grep '^\* ' | cut -d ' ' -f 2-)
78    rev_date=$(call_git log -1 | grep '^Date:' | sed -e 's,^Date:[ \t]*,,')
79    if [ -z "$(call_git status -s)" ]; then
80        rev_modified=false
81    else
82        rev_modified=true
83    fi
84
85    >${revfile}
86
87    echo "#define PACKAGE_REVISION_TYPE_GIT" >>${revfile}
88
89    echo "#define PACKAGE_REVISION_BRANCH \"${rev_branch}\"" >>${revfile}
90    echo "#define PACKAGE_REVISION_BASE \"${rev_base_id}\"" >>${revfile}
91
92    if [ ${rev_modified} = true ]; then
93        echo "#define PACKAGE_REVISION_MODIFIED 1" >>${revfile}
94    fi
95
96    echo "#define PACKAGE_REVISION_DATE \"${rev_date}\"" >>${revfile}
97}
98
99#
100# main
101#
102# Entry point.
103#
104main() {
105    outfile=
106    version=
107    while getopts :g:r:o:v: arg; do
108        case ${arg} in
109            g)
110                GIT=${OPTARG}
111                ;;
112            o)
113                outfile=${OPTARG}
114                ;;
115            r)
116                ROOT=${OPTARG}
117                ;;
118            v)
119                version=${OPTARG}
120                ;;
121            *)
122                err "Unknown option ${arg}"
123                ;;
124        esac
125    done
126    [ -n "${ROOT}" ] || \
127        err "Must specify the top-level source directory with -r"
128    [ -n "${outfile}" ] || \
129        err "Must specify an output file with -o"
130    [ -n "${version}" ] || \
131        err "Must specify a version number with -v"
132
133    if [ -n "${GIT}" -a -d ${ROOT}/.git ]; then
134        generate_from_git ${outfile}
135    else
136        generate_from_dist ${outfile} ${version}
137    fi
138}
139
140main "${@}"
141
142# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
143