1#!/bin/sh
2#
3#	Answer the question: what distro are we installing.
4#
5#    Copyright (C) 2010, Cisco Systems Inc.
6#
7#    This program is free software; you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation; either version 2 of the License, or
10#    (at your option) any later version.
11#
12#    This program is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License along
18#    with this program; if not, write to the Free Software Foundation, Inc.,
19#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Ngie Cooper, January 2010
22#
23#
24
25error() {
26	echo "${0##*/}: ERROR: $*" >&2
27}
28
29destdir=
30omit_redhat_minor_version=0
31
32while getopts "d:m" opt; do
33
34	case "$opt" in
35	d)
36		if [ ! -d "$OPTARG" ] ; then
37			error "$OPTARG doesn't exist"
38			exit 1
39		fi
40		destdir=$OPTARG
41		;;
42	m)
43		omit_redhat_minor_version=1
44		;;
45	esac
46done
47
48etc_dir="$destdir/etc"
49
50if [ ! -d "$etc_dir" ] ; then
51	error "$etc_dir doesn't exist"
52	exit 1
53fi
54
55#
56# Precedence list for files to look through for version info...
57#
58# XXX (garrcoop): add more..
59#
60for i in gentoo-release redhat-release; do
61	if [ -f "$etc_dir/$i" ] ; then
62		DISTRO_RELEASE_FILE="$i"
63		break
64	fi
65done
66
67if [ "x$DISTRO_RELEASE_FILE" = x ] ; then
68	error "Couldn't determine distro release file"
69	error "Please send an email with your distro's details, if you believe this is in error"
70	exit 1
71else
72	DISTRO_RELEASE_ABS_FILE="$etc_dir/$DISTRO_RELEASE_FILE"
73
74	case "$i" in
75	gentoo-release)
76		DISTRO=gentoo
77		RELEASE_FORMAT_FILE=1
78		;;
79	redhat-release)
80		RELEASE_FORMAT_FILE=1
81		if grep -q '^Red Hat' "$DISTRO_RELEASE_ABS_FILE"; then
82			DISTRO=redhat
83		elif grep -q '^Fedora' "$DISTRO_RELEASE_ABS_FILE"; then
84			DISTRO=fedora
85		else
86			RELEASE_FORMAT_FILE=0
87		fi
88		;;
89	esac
90
91	if [ $RELEASE_FORMAT_FILE -eq 1 ] ; then
92
93		set -- $(cat "$DISTRO_RELEASE_ABS_FILE")
94
95		while [ 1 ] ; do
96			shift
97			if [ $# -eq 0 -o "x$1" = "xrelease" ] ; then
98				if [ "x$1" = "xrelease" ] ; then
99					shift
100				fi
101				break
102			fi
103		done
104
105		case "$DISTRO" in
106		gentoo)
107			VERSION=$1
108			;;
109		fedora|redhat)
110			MAJOR_VER=$1
111			if [ $omit_redhat_minor_version -eq 0 ] && echo "$@" | grep -q '\(.*Update.*\)'; then
112				MINOR_VER=$(echo "$@" | sed -e 's/[\(\)]//g' -e 's/.*Update //')
113			fi
114			VERSION="$MAJOR_VER${MINOR_VER:+.${MINOR_VER}}"
115			;;
116		esac
117
118	fi
119
120	if [ "x$VERSION" = x ] ; then
121		error "Bad release file: $etc_dir/$DISTRO_RELEASE_FILE"
122		exit 2
123	else
124		echo "$DISTRO-$VERSION"
125	fi
126
127fi
128