Subject: osrelease.sh and param.h - take 2
To: None <tech-kern@netbsd.org>
From: Simon Burge <simonb@netbsd.org>
List: tech-kern
Date: 05/06/1999 13:37:26
Man this looks ugly at first glance.  I'd forgotten what a pain awk was.
Perl in the tree anyone? :-)

I can't spend any more time on this today (real work - dang!), but
here's something that works for all the cases I could think off of the
top of my head.  There's no error checking, so 1.2B.3_ALPHA and other
magically versions are possible.  I'll come back to it tomorrow if no
one has beaten it to a pulp in the meantime...

Simon.
--
#!/bin/sh
#
#	$NetBSD: osrelease.sh,v 1.17 1999/05/05 20:04:23 thorpej Exp $
#
# Copyright (c) 1997 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Luke Mewburn.
#
# 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.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``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 FOUNDATION OR CONTRIBUTORS
# 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.
#

# DO NOT UPDATE THIS FILE WITH THE KERNEL VERSION.
# UPDATE sys/sys/param.h

# Get the release number to use
paramh=`dirname $0`/../sys/param.h

# Parse $1 to the awk script and remove '.'s if it is "-s"

awk '/^#define[ \t]*__NetBSD_Version__/ {
	# Handle releases before NetBSD 10
	if (length($6) < 10)
		$3 = "0" $3

	# Use "."s between fields?
	if (ARGV[2] == "-s")
		sep = ""
	else
		sep = "."

	# XXX GAWKism
	# XXX reassign $0 to get $3 into two char fragments
	FIELDWIDTHS = "2 2 2 2 2"
	$0 = $3

	# assign components to variables (removing leading zeros)
	major = $1 + 0;
	minor = $2 + 0;
	release = $3 + 0;
	patchlevel = $4 + 0;
	# split last component
	released = substr($5, 1, 1)
	level = substr($5, 2, 1)

	# Always major.minor
	rel = major sep minor

	# Add patch level if non-zero
	if (patchlevel > 0) {
		rel = rel sep patchlevel
	}

	# Add release if non-zero
	if (release > 0) {
		release = sprintf("%c", $3 + 64);	# 64 = "A" - 1
		rel = rel release
	}

	# released should be 0 for pre-release
	if (released == 0) {
		tag = ""
		# Need to go further than DELTA?
		if (level == 1)      tag = "_ALPHA"
		else if (level == 2) tag = "_BETA"
		else if (level == 3) tag = "_GAMMA"
		else if (level == 4) tag = "_DELTA"
		rel = rel tag
	}

	print rel
	# Don't process any more...
	exit
}' $paramh $1