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$
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,\}"\).*$'
39256106SdesKERNEL_RE='^@(#)@@TYPE@@ \([-.0-9A-Za-z]\{1,\}\) .*$'
40256106Sdes
41256106Sdesprogname=$(basename $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
70256106Sdes	strings "$kernfile" | sed -n "s/$KERNEL_RE/\\1/p"
71256106Sdes}
72256106Sdes
73256106Sdes#
74256106Sdes# Print the hardcoded userland version.
75256106Sdes#
76256106Sdesuserland_version() {
77256106Sdes	echo $USERLAND_VERSION
78256106Sdes}
79256106Sdes
80256106Sdes#
81256106Sdes# Print a usage string and exit.
82256106Sdes#
83256106Sdesusage() {
84256336Sdes	echo "usage: $progname [-ku]" >&2
85256106Sdes	exit 1
86256106Sdes}
87256106Sdes
88256106Sdes#
89256106Sdes# Main program.
90256106Sdes#
91256106Sdesmain() {
92256106Sdes	# parse command-line arguments
93256106Sdes	while getopts "ku" option ; do
94256106Sdes		case $option in
95256106Sdes		k)
96256106Sdes			opt_k=1
97256106Sdes			;;
98256106Sdes		u)
99256106Sdes			opt_u=1
100256106Sdes			;;
101256106Sdes		*)
102256106Sdes			usage
103256106Sdes			;;
104256106Sdes		esac
105256106Sdes	done
106256106Sdes	if [ $OPTIND -le $# ] ; then
107256106Sdes		usage
108256106Sdes	fi
109256106Sdes
110256106Sdes	# default is -u
111256106Sdes	if [ $((opt_k + opt_u)) -eq 0 ] ; then
112256106Sdes		opt_u=1
113256106Sdes	fi
114256106Sdes
115256106Sdes	# print kernel version
116256106Sdes	if [ $opt_k ] ; then
117256106Sdes		kernel_version
118256106Sdes	fi
119256106Sdes
120256106Sdes	# print userland version
121256106Sdes	if [ $opt_u ] ; then
122256106Sdes		userland_version
123256106Sdes	fi
124256106Sdes}
125256106Sdes
126256106Sdesmain "$@"
127