#!/bin/sh

VERBOSE=

while getopts "v" OPT; do
    case "$OPT" in
	v)
	VERBOSE=1
	;;
    esac
done

warn() {
    if [ "$VERBOSE" ]; then
        echo "WARNING! " "$@" >&2
    fi
}

if [ -e "/sys/hypervisor/type" ]; then
    if [ "$(cat /sys/hypervisor/type)" = xen ]; then
        DIR=/sys/hypervisor/version
	VERSION_EXTRA="$(cat $DIR/extra)"
        if [ "$VERSION_EXTRA" = "-unstable" ]; then
            # Old xen-unstable
            VERSION=unstable
        elif [ "$VERSION_EXTRA" != "${VERSION_EXTRA#-}" ]; then
            # ABI for Lenny and smaller
            VERSION="$(cat $DIR/major).$(cat $DIR/minor)$VERSION_EXTRA"
        else
            VERSION="$(cat $DIR/major).$(cat $DIR/minor)"
        fi
    else
        warn "Can't read type from sysfs!"
    fi
else
    warn "Can't find hypervisor information in sysfs!"
fi

echo "$VERSION"
exit 0
