1256106Sdes#!/bin/sh
2256106Sdes#-
3256106Sdes# Copyright (c) 2013 Dag-Erling Sm��rgrav
4256106Sdes# All rights reserved.
5256106Sdes#
6256106Sdes# Redistribution and use in source and binary forms, with or without
7256106Sdes# modification, are permitted provided that the following conditions
8256106Sdes# are met:
9256106Sdes# 1. Redistributions of source code must retain the above copyright
10256106Sdes#    notice, this list of conditions and the following disclaimer.
11256106Sdes# 2. Redistributions in binary form must reproduce the above copyright
12256106Sdes#    notice, this list of conditions and the following disclaimer in the
13256106Sdes#    documentation and/or other materials provided with the distribution.
14256106Sdes#
15256106Sdes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16256106Sdes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17256106Sdes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18256106Sdes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19256106Sdes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20256106Sdes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21256106Sdes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22256106Sdes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23256106Sdes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24256106Sdes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25256106Sdes# SUCH DAMAGE.
26256106Sdes#
27256106Sdes# $FreeBSD: stable/11/bin/freebsd-version/freebsd-version.sh.in 330333 2018-03-03 11:18:38Z eadler $
28256106Sdes#
29256106Sdes
30256106Sdesset -e
31256106Sdes
32256106SdesUSERLAND_VERSION="@@REVISION@@-@@BRANCH@@"
33256106Sdes
34256106Sdes: ${ROOT:=}
35256106Sdes: ${LOADER_DIR:=$ROOT/boot}
36256106Sdes: ${LOADER_CONF_FILES:=$LOADER_DIR/defaults/loader.conf $LOADER_DIR/loader.conf $LOADER_DIR/loader.conf.local}
37256106SdesLOADER_RE1='^\([A-Z_a-z][0-9A-Z_a-z]*=[-./0-9A-Z_a-z]\{1,\}\).*$'
38256106SdesLOADER_RE2='^\([A-Z_a-z][0-9A-Z_a-z]*="[-./0-9A-Z_a-z]\{1,\}"\).*$'
39309828SdesKERNEL_RE='^@@TYPE@@ \([-.0-9A-Za-z]\{1,\}\) .*$'
40256106Sdes
41330333Seadlerprogname=${0##*/}
42256106Sdes
43256106Sdes#
44256106Sdes# Print an error message and exit.
45256106Sdes#
46256106Sdeserror() {
47256106Sdes	echo "$progname: $*" >&2
48256106Sdes	exit 1
49256106Sdes}
50256106Sdes
51256106Sdes#
52256106Sdes# Try to get the name of the installed kernel from loader.conf and
53256106Sdes# return the full path.  If loader.conf does not exist or we could not
54256106Sdes# read it, return the path to the default kernel.
55256106Sdes#
56256106Sdeskernel_file() {
57256106Sdes	eval $(sed -n "s/$LOADER_RE1/\\1;/p; s/$LOADER_RE2/\\1;/p" \
58256106Sdes	    $LOADER_CONF_FILES 2>/dev/null)
59256106Sdes	echo "$LOADER_DIR/${kernel:-kernel}/${bootfile:-kernel}"
60256106Sdes}
61256106Sdes
62256106Sdes#
63256106Sdes# Extract the kernel version from the installed kernel.
64256106Sdes#
65256106Sdeskernel_version() {
66256106Sdes	kernfile=$(kernel_file)
67256106Sdes	if [ ! -f "$kernfile" -o ! -r "$kernfile" ] ; then
68256106Sdes		error "unable to locate kernel"
69256106Sdes	fi
70309828Sdes	what -qs "$kernfile" | sed -n "s/$KERNEL_RE/\\1/p"
71256106Sdes}
72256106Sdes
73256106Sdes#
74330333Seadler# Print the version of the currently running kernel.
75330333Seadler#
76330333Seadlerrunning_version() {
77330333Seadler	sysctl -n kern.osrelease
78330333Seadler}
79330333Seadler
80330333Seadler#
81256106Sdes# Print the hardcoded userland version.
82256106Sdes#
83256106Sdesuserland_version() {
84256106Sdes	echo $USERLAND_VERSION
85256106Sdes}
86256106Sdes
87256106Sdes#
88256106Sdes# Print a usage string and exit.
89256106Sdes#
90256106Sdesusage() {
91330333Seadler	echo "usage: $progname [-kru]" >&2
92256106Sdes	exit 1
93256106Sdes}
94256106Sdes
95256106Sdes#
96256106Sdes# Main program.
97256106Sdes#
98256106Sdesmain() {
99256106Sdes	# parse command-line arguments
100330333Seadler	while getopts "kru" option ; do
101256106Sdes		case $option in
102256106Sdes		k)
103256106Sdes			opt_k=1
104256106Sdes			;;
105330333Seadler		r)
106330333Seadler			opt_r=1
107330333Seadler			;;
108256106Sdes		u)
109256106Sdes			opt_u=1
110256106Sdes			;;
111256106Sdes		*)
112256106Sdes			usage
113256106Sdes			;;
114256106Sdes		esac
115256106Sdes	done
116256106Sdes	if [ $OPTIND -le $# ] ; then
117256106Sdes		usage
118256106Sdes	fi
119256106Sdes
120256106Sdes	# default is -u
121330333Seadler	if [ $((opt_k + opt_r + opt_u)) -eq 0 ] ; then
122256106Sdes		opt_u=1
123256106Sdes	fi
124256106Sdes
125330333Seadler	# print installed kernel version
126256106Sdes	if [ $opt_k ] ; then
127256106Sdes		kernel_version
128256106Sdes	fi
129256106Sdes
130330333Seadler	# print running kernel version
131330333Seadler	if [ $opt_r ] ; then
132330333Seadler		running_version
133330333Seadler	fi
134330333Seadler
135256106Sdes	# print userland version
136256106Sdes	if [ $opt_u ] ; then
137256106Sdes		userland_version
138256106Sdes	fi
139256106Sdes}
140256106Sdes
141256106Sdesmain "$@"
142