buildinfo.sh 5.11 KB
Newer Older
powelld's avatar
powelld committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# buildinfo.sh -- Determine Build Information
# Initially written by Ralf S. Engelschall <rse@apache.org>
# for the Apache's Autoconf-style Interface (APACI) 

#
#   argument line handling
#
error=no
if [ $# -ne 1 -a $# -ne 2 ]; then
    error=yes
fi
if [ $# -eq 2 -a "x$1" != "x-n" ]; then
    error=yes
fi
if [ "x$error" = "xyes" ]; then
    echo "$0:Error: invalid argument line"
    echo "$0:Usage: $0 [-n] <format-string>"
    echo "Where <format-string> can contain:"
    echo "   %u ...... substituted by determined username    (foo)"
    echo "   %h ...... substituted by determined hostname    (bar)"
    echo "   %d ...... substituted by determined domainname  (.com)"
    echo "   %D ...... substituted by determined day         (DD)"
    echo "   %M ...... substituted by determined month       (MM)"
    echo "   %Y ...... substituted by determined year        (YYYYY)"
    echo "   %m ...... substituted by determined monthname   (Jan)"
    exit 1
fi
if [ $# -eq 2 ]; then
    newline=no
    format_string="$2"
else
    newline=yes
    format_string="$1"
fi

#
#   initialization
#
username=''
hostname=''
domainname=''
time_day=''
time_month=''
time_year=''
time_monthname=''

#
#   determine username
#
username="$LOGNAME"
if [ "x$username" = "x" ]; then
    username="$USER"
    if [ "x$username" = "x" ]; then
        username="`(whoami) 2>/dev/null |\
                   awk '{ printf("%s", $1); }'`"
        if [ "x$username" = "x" ]; then
            username="`(who am i) 2>/dev/null |\
                       awk '{ printf("%s", $1); }'`"
            if [ "x$username" = "x" ]; then
                username='unknown'
            fi
        fi
    fi
fi

#
#   determine hostname and domainname
#
hostname="`(uname -n) 2>/dev/null |\
           awk '{ printf("%s", $1); }'`"
if [ "x$hostname" = "x" ]; then
    hostname="`(hostname) 2>/dev/null |\
               awk '{ printf("%s", $1); }'`"
    if [ "x$hostname" = "x" ]; then
        hostname='unknown'
    fi
fi
case $hostname in
    *.* )
        domainname=".`echo $hostname | cut -d. -f2-`"
        hostname="`echo $hostname | cut -d. -f1`"
        ;;
esac
if [ "x$domainname" = "x" ]; then
    if [ -f /etc/resolv.conf ]; then
        domainname="`egrep '^[ 	]*domain' /etc/resolv.conf | head -1 |\
                     sed -e 's/.*domain//' \
                         -e 's/^[ 	]*//' -e 's/^ *//' -e 's/^	*//' \
                         -e 's/^\.//' -e 's/^/./' |\
                     awk '{ printf("%s", $1); }'`"
        if [ "x$domainname" = "x" ]; then
            domainname="`egrep '^[ 	]*search' /etc/resolv.conf | head -1 |\
                         sed -e 's/.*search//' \
                             -e 's/^[ 	]*//' -e 's/^ *//' -e 's/^	*//' \
                             -e 's/ .*//' -e 's/	.*//' \
                             -e 's/^\.//' -e 's/^/./' |\
                         awk '{ printf("%s", $1); }'`"
        fi
    fi
fi

#
#   determine current time
#
time_day="`date '+%d' | awk '{ printf("%s", $1); }'`"
time_month="`date '+%m' | awk '{ printf("%s", $1); }'`"
time_year="`date '+%Y' 2>/dev/null | awk '{ printf("%s", $1); }'`"
if [ "x$time_year" = "x" ]; then
    time_year="`date '+%y' | awk '{ printf("%s", $1); }'`"
    case $time_year in
        [5-9][0-9]) time_year="19$time_year" ;;
        [0-4][0-9]) time_year="20$time_year" ;;
    esac
fi
case $time_month in
    1|01) time_monthname='Jan' ;;
    2|02) time_monthname='Feb' ;;
    3|03) time_monthname='Mar' ;;
    4|04) time_monthname='Apr' ;;
    5|05) time_monthname='May' ;;
    6|06) time_monthname='Jun' ;;
    7|07) time_monthname='Jul' ;;
    8|08) time_monthname='Aug' ;;
    9|09) time_monthname='Sep' ;;
      10) time_monthname='Oct' ;;
      11) time_monthname='Nov' ;;
      12) time_monthname='Dec' ;;
esac

#
#   create result string
#
if [ "x$newline" = "xyes" ]; then
    echo $format_string |\
    sed -e "s;%u;$username;g" \
        -e "s;%h;$hostname;g" \
        -e "s;%d;$domainname;g" \
        -e "s;%D;$time_day;g" \
        -e "s;%M;$time_month;g" \
        -e "s;%Y;$time_year;g" \
        -e "s;%m;$time_monthname;g"
else
    echo "${format_string}&" |\
    sed -e "s;%u;$username;g" \
        -e "s;%h;$hostname;g" \
        -e "s;%d;$domainname;g" \
        -e "s;%D;$time_day;g" \
        -e "s;%M;$time_month;g" \
        -e "s;%Y;$time_year;g" \
        -e "s;%m;$time_monthname;g" |\
    awk '-F&' '{ printf("%s", $1); }'
fi