#!/bin/sh
#
# postfix-get-distribution - script to determine the correct
# distribution, in the form <distribution>-<major>.<minor>
#
# - there appears to be no clear way of doing this which is cross platform
#   compatible between different rpm vendors
#
# With rh9/rhel3 the return value of rpm -q redhat-release does
# not include a minor version.  This is used in several places in
# make-postfix.spec so we generate a minor version of 0 if necessary.
#
# Recognised distributions so far are:
# - RedHat Enterprise Linux
# - Whitebox Linux (reports as RedHat Enterprise Linux for building)
# - CentOS Linux (reports as RedHat Enterprise Linux for building)
# - TaoLinux (reports as RedHat Enterprise Linux for building)
# - RedHat Linux
# - Fedora Core
# - YellowDog Linux
# - Mandrake Linux

[ -n "$DEBUG" ] && set -x
myversion=$(echo '$Revision: 1.13.2.3 $' | sed -e 's/^.*: //g' -e 's/ .*$//g')
myname=$(basename $0)

# Provide a usage message
usage () {
	cat <<-EOF
	$myname version $myversion

	Copyright (C) 2002-2011 - Simon J Mudd <sjmudd@pobox.com>
	This program may be freely redistributed under the terms of the GNU GPL

	Usage: $myname [--help] [--distribution] [--name] [--major] [--minor] [--full]

	Shows the distribution name or one of several different components:
	--distribution  <name>-<major>.<minor>		[default behaviour]
	--name          redhat, rhel, yellowdog, ... (abbreviated format)
	--major         major version (3, 5, 6, 7, 8)
	--minor         minor version (0, ...) [ may have decimal values ]
	--full          Redhat-Release-X.Y, Whitebox-Release-X.Y, ...
	--debug         show all values on separate lines in forma item=value
	--distro-info   Show debug parameter values only,  not the ‘‘name=’’
	                label that normally precedes the debug value.
	EOF
}

# is the given rpm installed? (e.g. 'redhat-release', 'fedora-release')
# - return exit code 0 if it is
# - for debugging we compare against ${debug_release}
is_installed () {
    local package=$1

    if [ -n "${debug_release}" ]; then
        [ "${debug_release}" = "${package}" ]
    else
        rpm -q ${package} 2>&1 >/dev/null
    fi
}

# get the <distro>-release %{version}-%{release}
# - for debugging we return ${debug_full_release}
# Thus 'centos-release-4-3.2' -> '4-3.2'
get_pkg_full () {
    local pkg_name=$1

    if [ -n "${debug_full_release}" ]; then
        echo "${debug_full_release}"
    else
        # if you have installed the XXX-release rpm more than once
        # this will ensure that only one of the instances is
        # recognised.
        # Use --queryformat so we don't break if .rpmmacros redefines 
        # %_query_all_fmt
        rpm --queryformat '%{NAME}-%{VERSION}-%{RELEASE}\n' -q ${pkg_name} | tail -1
    fi
}

# given the pkg_full (%{name}-%{version}-%{release}) return the %version
# Thus '4-3.2' -> '4'
get_pkg_version () {
    local pkg_name=$1
    local pkg_full=$2

    echo "$pkg_full" | sed -e "s/^${pkg_name}-//" -e 's/-[^-]*$//'
}

info=distribution
if [ -n "$1" ]; then
    case $1 in
    --distribution)	info=distribution;;
    --name)		info=name;;
    --major)		info=major;;
    --minor)		info=minor;;
    --full)		info=full;;
    --debug)		info=debug;;
    --distro-info)      info=params;;
    --help)		usage; exit 0;;
    *)			usage; exit 1;;
    esac
fi

# default values
full="unknown-release-?.?"
name=unknown
version=0.0
major=0
minor=0
#
# Determine the distribution we are running on
#
if is_installed redhat-release; then
    # We are using RedHat Linux OR RedHat Enterprise Linux
    # redhat-release-5Server-5.0.0.9 (yet another versioning style!)
    # redhat-release-3ES-1
    # redhat-release-3ES-7.2
    # redhat-release-2.9.5AS-7 (taroon beta)
    # redhat-release-9-3
    # redhat-release-8.0-8
    # redhat-release-7.3-1
    # redhat-release-7.2-1
    # redhat-release-6.2-1
    pkg_name=redhat-release
elif is_installed centos-release; then
    # CentOS SHOULD behave like rhel, so we name it the same
    # centos-release-4-3.2
    # centos-release-6-0.el6.centos.5
    pkg_name=centos-release
elif is_installed whitebox-release; then
    # whitebox-release-3.0-6_i386
    # whitebox-release-4-2.WB2
    # - whitebox SHOULD behave like rhel, so we name it the same
    pkg_name=whitebox-release
elif is_installed tao-release; then
    # tao-release-1.0-TL16
    # Tao-Linux SHOULD behave like rhel, so we name it the same
    pkg_name=tao-release
elif is_installed sl-release; then
    # Warning: it has not been confirmed that this actually works.
    pkg_name=sl-release
elif is_installed fedora-release; then
    pkg_name=fedora-release
elif is_installed redhat-release-es; then
    pkg_name=redhat-release-es
elif is_installed redhat-release-as; then
    pkg_name=redhat-release-as
elif is_installed mandrake-release; then
    pkg_name=mandrake-release
elif is_installed yellowdog-release; then
    pkg_name=yellowdog-release
else
    # give up if we can not proceed
    echo "ERROR: $myname - unable to determine distribution, exiting"
    exit 1
fi

# Get the full package version (uncleaned)
full=$(get_pkg_full ${pkg_name})
version=$(get_pkg_version ${pkg_name} "$full")

# Tidy up the version information into something more usable.
case $pkg_name in
redhat-release)
    if [ -n "$(echo "$full" | egrep '([EWA]S|Server)')" ]; then
        name=rhel
    else
        name=redhat
    fi
    # trim off the ES/WS/AS/Server stuff
    version=$(echo "$version" | sed -e 's;[EWA]S;;' -e 's;Server;;')
    ;;
centos-release)
    name=rhel
    ;;
whitebox-release)
    name=rhel
    ;;
tao-release)
    name=rhel
    ;;
sl-release)
    name=rhel
    ;;
fedora-release)
    name=fedora
    ;;
redhat-release-es|redhat-release-as)
    name=rhel
    version=$(echo "$version" | sed -e 's;[A-Z]$;;')
    ;;
mandrake-release)
    name=mandrake
    ;;
yellowdog-release)
    name=yellowdog
    ;;
*)
esac

# from version determine the major and minor parts
if echo "$version" | grep -q '\.'; then
    # there's one or more '.'s in release (e.g. 8.0 OR 2.9.5)
    major=$(echo "$version" | sed -e 's;\.[0-9.]*$;;')
    minor=$(echo "$version" | sed -e 's;^[0-9]*\.;;')
else
    # there's no '.' in release (e.g. 9)
    major=$version
fi

# tao-release fix
# - tao-linux 1.0 is equivalent to rhel-3.0 so adjust the version numbering
#   accordingly.
[ "$pkg_name" = "tao-release" -a "$major" = 1 ] && major=3

case $info in
distribution)
    echo "${name}-${major}.${minor}"
    ;;
name)
    echo "${name}"
    ;;
major)
    echo "${major}"
    ;;
minor)
    echo "${minor}"
    ;;
full)
    echo "${full}"
    ;;
debug)
    echo "name=$name"
    echo "release=$version"
    echo "major=$major"
    echo "minor=$minor"
    echo "full=$full"
    echo "rpm_name=$pkg_name"	# maintain rpm_name for backwards compatibility
    ;;
params)
    # maintain the same order as the previous debug output
    echo "$name $version $major $minor $full $pkg_name"
    ;;
*)
    echo "Internal error info=$info" >&2
    exit 1
esac
