#!/bin/bash
#
# Wrapper for Linux uname command.
# Massages output to meet the XR environment expectation that 'hostname' is
# actually the XR node name, not the hostname of the router.
#
# September 2016, Glenn F. Matthews
#
# Copyright (c) 2016 by Cisco Systems, Inc.
# All rights reserved.

output=`/bin/uname $@ 2>&1`
rc=$?
# Replace all instances of /bin/uname in the output with our name
output="${output//\/bin\/uname/$0}"
if [ $rc != 0 ]; then
    echo "$output" >&2
    exit $rc
fi

while [ "$#" -ne 0 ]; do
    case "$1" in
        -a | --all | -n | --nodename)
            # Replace hostname in the output with XR node name.
            real_hostname=`/bin/uname -n`
            xr_nodename=`/pkg/bin/hostname`
            # Note, this may have unexpected results if the hostname has been
            # set to something that looks like a different uname field,
            # such as 'Linux' or 'Fri'.
            output="${output/$real_hostname/$xr_nodename}"
            ;;
        --help )
            # Customize the usage message slightly
            output="${output/network node hostname/XR node name}"
            ;;
        *)
            # ignore all other parameters we don't care to intercept
            ;;
    esac

    shift
done

echo "$output"
exit $rc
