Elliot Lee 259bb72
#!/bin/bash
Elliot Lee 259bb72
# dist.sh
Elliot Lee 259bb72
# Author: Tom "spot" Callaway <tcallawa@redhat.com>
Elliot Lee 259bb72
# License: GPL
Elliot Lee 259bb72
# This is a script to output the value for the %{dist}
Elliot Lee 259bb72
# tag. The dist tag takes the following format: .$type$num
Elliot Lee 259bb72
# Where $type is one of: el, fc, rh
Elliot Lee 259bb72
# (for RHEL, Fedora Core, and RHL, respectively)
Elliot Lee 259bb72
# And $num is the version number of the distribution.
Elliot Lee 259bb72
# NOTE: We can't detect Rawhide or Fedora Test builds properly.
Elliot Lee 259bb72
# If we successfully detect the version number, we output the
Elliot Lee 259bb72
# dist tag. Otherwise, we exit with no output.
Elliot Lee 259bb72
Elliot Lee 259bb72
RELEASEFILE=/etc/redhat-release
Elliot Lee 259bb72
Elliot Lee 259bb72
function check_num {
Elliot Lee 259bb72
    MAINVER=`cut -d "(" -f 1 < $RELEASEFILE | \
Elliot Lee 259bb72
	sed -e "s/[^0-9.]//g" -e "s/$//g" | cut -d "." -f 1`
Elliot Lee 259bb72
Elliot Lee 259bb72
    echo $MAINVER | grep -q '[0-9]' && echo $MAINVER
Elliot Lee 259bb72
}
Elliot Lee 259bb72
Elliot Lee 259bb72
function check_rhl {
Elliot Lee 259bb72
    grep -q "Red Hat Linux" $RELEASEFILE && ! grep -q "Advanced" $RELEASEFILE && echo $DISTNUM
Elliot Lee 259bb72
}
Elliot Lee 259bb72
Elliot Lee 259bb72
function check_rhel {
Elliot Lee 259bb72
    egrep -q "(Enterprise|Advanced)" $RELEASEFILE && echo $DISTNUM
Elliot Lee 259bb72
}
Elliot Lee 259bb72
Elliot Lee 259bb72
function check_fedora {
Elliot Lee 259bb72
    grep -q Fedora $RELEASEFILE && echo $DISTNUM
Elliot Lee 259bb72
}
Elliot Lee 259bb72
Elliot Lee 259bb72
DISTNUM=`check_num`
Elliot Lee 259bb72
DISTFC=`check_fedora`
Elliot Lee 259bb72
DISTRHL=`check_rhl`
Elliot Lee 259bb72
DISTRHEL=`check_rhel`
Elliot Lee 259bb72
if [ -n "$DISTNUM" ]; then
Elliot Lee 259bb72
    if [ -n "$DISTFC" ]; then
Elliot Lee 259bb72
	DISTTYPE=fc
Elliot Lee 259bb72
    elif [ -n "$DISTRHEL" ]; then
Elliot Lee 259bb72
	DISTTYPE=el
Elliot Lee 259bb72
    elif [ -n "$DISTRHL" ]; then
Elliot Lee 259bb72
	DISTTYPE=rhl
Elliot Lee 259bb72
    fi
Elliot Lee 259bb72
fi
Elliot Lee 259bb72
[ -n "$DISTTYPE" -a -n "$DISTNUM" ] && DISTTAG=".${DISTTYPE}${DISTNUM}"
Elliot Lee 259bb72
Elliot Lee 259bb72
case "$1" in
Elliot Lee 259bb72
    --el) echo -n "$DISTRHEL" ;;
Jeremy Katz 1b84f70
    --fc) echo -n "$DISTFC" ;;
Elliot Lee 259bb72
    --rhl) echo -n "$DISTRHL" ;;
Elliot Lee 259bb72
    --distnum) echo -n "$DISTNUM" ;;
Elliot Lee 259bb72
    --disttype) echo -n "$DISTTYPE" ;;
Elliot Lee 259bb72
    --help)
Elliot Lee 259bb72
	printf "Usage: $0 [OPTIONS]\n"
Elliot Lee 259bb72
	printf " Default mode is --dist. Possible options:\n"
Elliot Lee 259bb72
	printf " --el\t\tfor RHEL version (if RHEL)\n"
Elliot Lee 259bb72
	printf " --fc\t\tfor Fedora version (if Fedora)\n"
Elliot Lee 259bb72
	printf " --rhl\t\tfor RHL version (if RHL)\n"
Elliot Lee 259bb72
	printf " --dist\t\tfor distribution tag\n"
Elliot Lee 259bb72
	printf " --distnum\tfor distribution number (major)\n"
Elliot Lee 259bb72
	printf " --disttype\tfor distribution type\n" ;;
Elliot Lee 259bb72
    *) echo -n "$DISTTAG" ;;
Elliot Lee 259bb72
esac