opnsense-core/src/sbin/opnsense-version

166 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
# Copyright (c) 2018-2021 Franco Fichtner <franco@opnsense.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
DEFAULTS="\${product_name} \${product_version}"
VERSIONDIR="/usr/local/opnsense/version"
OPENSSL="/usr/local/bin/openssl"
PKG="/usr/local/sbin/pkg-static"
TARGET="core"
OUTPUT=
DO_CHECK=
while getopts AacefHiNnsVvw OPT; do
case ${OPT} in
A)
OUTPUT="${OUTPUT} \${product_arch}"
;;
a)
OUTPUT="${OUTPUT} \${product_abi}"
;;
c)
DO_CHECK="-c"
;;
e)
OUTPUT="${OUTPUT} \${product_email}"
;;
f)
OUTPUT="${OUTPUT} \${product_flavour}"
;;
H)
OUTPUT="${OUTPUT} \${product_hash}"
;;
i)
OUTPUT="${OUTPUT} \${product_nickname}"
;;
N)
OUTPUT="${OUTPUT} \${product_name}"
;;
n)
OUTPUT="${OUTPUT} \${product_id}"
;;
s)
OUTPUT="${OUTPUT} \${product_size}"
;;
V)
OUTPUT="${OUTPUT} \${product_series}"
;;
v)
OUTPUT="${OUTPUT} \${product_version}"
;;
w)
OUTPUT="${OUTPUT} \${product_website}"
;;
*)
echo "Usage: man ${0##*/}" >&2
exit 1
;;
esac
done
shift $((${OPTIND} - 1))
if [ -n "${1}" ]; then
TARGET=${1}
shift
fi
VERSION="${VERSIONDIR}/${TARGET}"
if [ -n "${DO_CHECK}" ]; then
RET=0
if [ ! -f ${VERSION} ]; then
RET=1
fi
exit ${RET}
fi
if [ -n "${*}" ]; then
echo "Additional arguments are not supported" >&2
exit 1
fi
if [ ! -f ${VERSION} ]; then
echo "Missing ${VERSION}" >&2
exit 1
fi
if grep -q '[{}]' ${VERSION}; then
eval "$(grep -v '[{}]' ${VERSION} | sed 's/[^"]*"\([^"]*\)"[^"]*"\([^"]*\)".*/\1="\2"/')"
fi
case "${TARGET}" in
core)
if [ -z "${OUTPUT}" ]; then
OUTPUT="${DEFAULTS} \(\${product_arch}/\${product_flavour}\)"
fi
if [ -z "${OUTPUT%%*product_flavour*}" -a -f ${OPENSSL} ]; then
RESULT=$(${OPENSSL} version)
product_flavour=${RESULT%% *}
fi
if [ -z "${OUTPUT%%*product_size*}" -a -f ${PKG} ]; then
RESULT=$(${PKG} info -s ${product_id})
product_size=${RESULT##* }
fi
;;
*)
if [ -z "${OUTPUT}" ]; then
if [ -n "${product_name}" ]; then
OUTPUT=${DEFAULTS}
else
OUTPUT="\${product_version}"
fi
fi
if [ -z "${product_version}" -a -z "${OUTPUT%%*product_version*}" -a -f ${VERSION} ]; then
if [ -z "${product_version}" ]; then
product_version=$(cat ${VERSION})
fi
fi
if [ -z "${OUTPUT%%*product_size*}" ]; then
if [ -n "${product_id}" ]; then
RESULT=$(${PKG} info -s ${product_id})
product_size=${RESULT##* }
elif [ -f ${VERSION}.size ]; then
product_size=$(cat ${VERSION}.size)
fi
fi
if [ -z "${OUTPUT%%*product_arch*}" ]; then
if [ -z "${product_arch}" -a -f ${VERSION}.arch ]; then
product_arch=$(cat ${VERSION}.arch)
fi
fi
if [ -z "${OUTPUT%%*product_id*}" ]; then
if [ -z "${product_id}" ]; then
# mock ID target, there is no .name file
product_id=${TARGET}
fi
fi
;;
esac
eval echo ${OUTPUT}