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