1100966Siwasaki#!/bin/sh
2100966Siwasaki#-
3100966Siwasaki# Copyright (c) 2013 Dag-Erling Sm��rgrav
4100966Siwasaki# All rights reserved.
5100966Siwasaki#
6100966Siwasaki# Redistribution and use in source and binary forms, with or without
7100966Siwasaki# modification, are permitted provided that the following conditions
8100966Siwasaki# are met:
9100966Siwasaki# 1. Redistributions of source code must retain the above copyright
10100966Siwasaki#    notice, this list of conditions and the following disclaimer.
11193267Sjkim# 2. Redistributions in binary form must reproduce the above copyright
12100966Siwasaki#    notice, this list of conditions and the following disclaimer in the
13100966Siwasaki#    documentation and/or other materials provided with the distribution.
14100966Siwasaki#
15100966Siwasaki# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16100966Siwasaki# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17100966Siwasaki# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18100966Siwasaki# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19100966Siwasaki# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20100966Siwasaki# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21100966Siwasaki# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22100966Siwasaki# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23100966Siwasaki# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24100966Siwasaki# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25100966Siwasaki# SUCH DAMAGE.
26100966Siwasaki#
27100966Siwasaki# $FreeBSD: releng/11.0/bin/freebsd-version/freebsd-version.sh.in 256332 2013-10-11 20:10:18Z des $
28100966Siwasaki#
29100966Siwasaki
30100966Siwasakiset -e
31100966Siwasaki
32100966SiwasakiUSERLAND_VERSION="@@REVISION@@-@@BRANCH@@"
33100966Siwasaki
34100966Siwasaki: ${ROOT:=}
35100966Siwasaki: ${LOADER_DIR:=$ROOT/boot}
36100966Siwasaki: ${LOADER_CONF_FILES:=$LOADER_DIR/defaults/loader.conf $LOADER_DIR/loader.conf $LOADER_DIR/loader.conf.local}
37100966SiwasakiLOADER_RE1='^\([A-Z_a-z][0-9A-Z_a-z]*=[-./0-9A-Z_a-z]\{1,\}\).*$'
38100966SiwasakiLOADER_RE2='^\([A-Z_a-z][0-9A-Z_a-z]*="[-./0-9A-Z_a-z]\{1,\}"\).*$'
39100966SiwasakiKERNEL_RE='^@(#)@@TYPE@@ \([-.0-9A-Za-z]\{1,\}\) .*$'
40100966Siwasaki
41100966Siwasakiprogname=$(basename $0)
42100966Siwasaki
43100966Siwasaki#
44100966Siwasaki# Print an error message and exit.
45100966Siwasaki#
46100966Siwasakierror() {
47100966Siwasaki	echo "$progname: $*" >&2
48100966Siwasaki	exit 1
49100966Siwasaki}
50100966Siwasaki
51100966Siwasaki#
52100966Siwasaki# Try to get the name of the installed kernel from loader.conf and
53100966Siwasaki# return the full path.  If loader.conf does not exist or we could not
54100966Siwasaki# read it, return the path to the default kernel.
55100966Siwasaki#
56100966Siwasakikernel_file() {
57100966Siwasaki	eval $(sed -n "s/$LOADER_RE1/\\1;/p; s/$LOADER_RE2/\\1;/p" \
58100966Siwasaki	    $LOADER_CONF_FILES 2>/dev/null)
59100966Siwasaki	echo "$LOADER_DIR/${kernel:-kernel}/${bootfile:-kernel}"
60100966Siwasaki}
61100966Siwasaki
62100966Siwasaki#
63100966Siwasaki# Extract the kernel version from the installed kernel.
64100966Siwasaki#
65100966Siwasakikernel_version() {
66100966Siwasaki	kernfile=$(kernel_file)
67100966Siwasaki	if [ ! -f "$kernfile" -o ! -r "$kernfile" ] ; then
68100966Siwasaki		error "unable to locate kernel"
69100966Siwasaki	fi
70100966Siwasaki	strings "$kernfile" | sed -n "s/$KERNEL_RE/\\1/p"
71100966Siwasaki}
72100966Siwasaki
73100966Siwasaki#
74100966Siwasaki# Print the hardcoded userland version.
75100966Siwasaki#
76100966Siwasakiuserland_version() {
77100966Siwasaki	echo $USERLAND_VERSION
78100966Siwasaki}
79100966Siwasaki
80100966Siwasaki#
81100966Siwasaki# Print a usage string and exit.
82100966Siwasaki#
83100966Siwasakiusage() {
84100966Siwasaki	echo "usage: $progname [-ku]" >&2
85100966Siwasaki	exit 1
86100966Siwasaki}
87100966Siwasaki
88100966Siwasaki#
89100966Siwasaki# Main program.
90100966Siwasaki#
91100966Siwasakimain() {
92100966Siwasaki	# parse command-line arguments
93100966Siwasaki	while getopts "ku" option ; do
94100966Siwasaki		case $option in
95100966Siwasaki		k)
96100966Siwasaki			opt_k=1
97100966Siwasaki			;;
98100966Siwasaki		u)
99100966Siwasaki			opt_u=1
100100966Siwasaki			;;
101100966Siwasaki		*)
102100966Siwasaki			usage
103100966Siwasaki			;;
104100966Siwasaki		esac
105100966Siwasaki	done
106100966Siwasaki	if [ $OPTIND -le $# ] ; then
107100966Siwasaki		usage
108100966Siwasaki	fi
109100966Siwasaki
110100966Siwasaki	# default is -u
111100966Siwasaki	if [ $((opt_k + opt_u)) -eq 0 ] ; then
112100966Siwasaki		opt_u=1
113100966Siwasaki	fi
114100966Siwasaki
115100966Siwasaki	# print kernel version
116100966Siwasaki	if [ $opt_k ] ; then
117193341Sjkim		kernel_version
118193341Sjkim	fi
119193341Sjkim
120193341Sjkim	# print userland version
121193341Sjkim	if [ $opt_u ] ; then
122193341Sjkim		userland_version
123100966Siwasaki	fi
124100966Siwasaki}
125100966Siwasaki
126100966Siwasakimain "$@"
127102550Siwasaki