1#!/bin/sh
2#
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18#
19# buildinfo.sh -- Determine Build Information
20# Initially written by Ralf S. Engelschall <rse@apache.org>
21# for the Apache's Autoconf-style Interface (APACI) 
22
23#
24#   argument line handling
25#
26error=no
27if [ $# -ne 1 -a $# -ne 2 ]; then
28    error=yes
29fi
30if [ $# -eq 2 -a "x$1" != "x-n" ]; then
31    error=yes
32fi
33if [ "x$error" = "xyes" ]; then
34    echo "$0:Error: invalid argument line"
35    echo "$0:Usage: $0 [-n] <format-string>"
36    echo "Where <format-string> can contain:"
37    echo "   %u ...... substituted by determined username    (foo)"
38    echo "   %h ...... substituted by determined hostname    (bar)"
39    echo "   %d ...... substituted by determined domainname  (.com)"
40    echo "   %D ...... substituted by determined day         (DD)"
41    echo "   %M ...... substituted by determined month       (MM)"
42    echo "   %Y ...... substituted by determined year        (YYYYY)"
43    echo "   %m ...... substituted by determined monthname   (Jan)"
44    exit 1
45fi
46if [ $# -eq 2 ]; then
47    newline=no
48    format_string="$2"
49else
50    newline=yes
51    format_string="$1"
52fi
53
54#
55#   initialization
56#
57username=''
58hostname=''
59domainname=''
60time_day=''
61time_month=''
62time_year=''
63time_monthname=''
64
65#
66#   determine username
67#
68username="$LOGNAME"
69if [ "x$username" = "x" ]; then
70    username="$USER"
71    if [ "x$username" = "x" ]; then
72        username="`(whoami) 2>/dev/null |\
73                   awk '{ printf("%s", $1); }'`"
74        if [ "x$username" = "x" ]; then
75            username="`(who am i) 2>/dev/null |\
76                       awk '{ printf("%s", $1); }'`"
77            if [ "x$username" = "x" ]; then
78                username='unknown'
79            fi
80        fi
81    fi
82fi
83
84#
85#   determine hostname and domainname
86#
87hostname="`(uname -n) 2>/dev/null |\
88           awk '{ printf("%s", $1); }'`"
89if [ "x$hostname" = "x" ]; then
90    hostname="`(hostname) 2>/dev/null |\
91               awk '{ printf("%s", $1); }'`"
92    if [ "x$hostname" = "x" ]; then
93        hostname='unknown'
94    fi
95fi
96case $hostname in
97    *.* )
98        domainname=".`echo $hostname | cut -d. -f2-`"
99        hostname="`echo $hostname | cut -d. -f1`"
100        ;;
101esac
102if [ "x$domainname" = "x" ]; then
103    if [ -f /etc/resolv.conf ]; then
104        domainname="`egrep '^[ 	]*domain' /etc/resolv.conf | head -1 |\
105                     sed -e 's/.*domain//' \
106                         -e 's/^[ 	]*//' -e 's/^ *//' -e 's/^	*//' \
107                         -e 's/^\.//' -e 's/^/./' |\
108                     awk '{ printf("%s", $1); }'`"
109        if [ "x$domainname" = "x" ]; then
110            domainname="`egrep '^[ 	]*search' /etc/resolv.conf | head -1 |\
111                         sed -e 's/.*search//' \
112                             -e 's/^[ 	]*//' -e 's/^ *//' -e 's/^	*//' \
113                             -e 's/ .*//' -e 's/	.*//' \
114                             -e 's/^\.//' -e 's/^/./' |\
115                         awk '{ printf("%s", $1); }'`"
116        fi
117    fi
118fi
119
120#
121#   determine current time
122#
123time_day="`date '+%d' | awk '{ printf("%s", $1); }'`"
124time_month="`date '+%m' | awk '{ printf("%s", $1); }'`"
125time_year="`date '+%Y' 2>/dev/null | awk '{ printf("%s", $1); }'`"
126if [ "x$time_year" = "x" ]; then
127    time_year="`date '+%y' | awk '{ printf("%s", $1); }'`"
128    case $time_year in
129        [5-9][0-9]) time_year="19$time_year" ;;
130        [0-4][0-9]) time_year="20$time_year" ;;
131    esac
132fi
133case $time_month in
134    1|01) time_monthname='Jan' ;;
135    2|02) time_monthname='Feb' ;;
136    3|03) time_monthname='Mar' ;;
137    4|04) time_monthname='Apr' ;;
138    5|05) time_monthname='May' ;;
139    6|06) time_monthname='Jun' ;;
140    7|07) time_monthname='Jul' ;;
141    8|08) time_monthname='Aug' ;;
142    9|09) time_monthname='Sep' ;;
143      10) time_monthname='Oct' ;;
144      11) time_monthname='Nov' ;;
145      12) time_monthname='Dec' ;;
146esac
147
148#
149#   create result string
150#
151if [ "x$newline" = "xyes" ]; then
152    echo $format_string |\
153    sed -e "s;%u;$username;g" \
154        -e "s;%h;$hostname;g" \
155        -e "s;%d;$domainname;g" \
156        -e "s;%D;$time_day;g" \
157        -e "s;%M;$time_month;g" \
158        -e "s;%Y;$time_year;g" \
159        -e "s;%m;$time_monthname;g"
160else
161    echo "${format_string}&" |\
162    sed -e "s;%u;$username;g" \
163        -e "s;%h;$hostname;g" \
164        -e "s;%d;$domainname;g" \
165        -e "s;%D;$time_day;g" \
166        -e "s;%M;$time_month;g" \
167        -e "s;%Y;$time_year;g" \
168        -e "s;%m;$time_monthname;g" |\
169    awk '-F&' '{ printf("%s", $1); }'
170fi
171
172