#!/bin/bash
# This is a duplicate of GetVPD script. Changes made here must be done in
# GetVPD script.
# First argument passed in can be
# -b to query bios version
# -s to query serial number
# -a to query machine type, model and serial number
# -g to query uuid

display_values()
{
    if [ "$1" = "-a" ]
    then
       echo "$cmodel$mtype*$sn"
    elif [ "$1" = "-s" ]
    then
       echo "$sn"
    elif [ "$1" = "-m" ]
    then
       echo "$cmodel$mtype"
    else
       echo "$cmodel$mtype"
    fi
}

getbiosinfo()
{
    arch=`uname -p`

    if [ "$1" = "-b" ]; then
        if [ "ppc64le" == "$arch" ]; then
            hypervinfo=`/opt/hmc/bin/gethypervisorinfo`
            if [ $? -eq 0 ]; then
                echo "n/a"
            else
                prodinfo=$(/usr/bin/ipmitool fru print 47 2>/dev/null|grep "Product Version" | awk '{print $4}')
                if [ "$prodinfo" = "" ]; then
                    echo "n/a"
                else
                    echo $prodinfo
                fi
            fi
            exit 0
        else
            /usr/sbin/dmidecode --string bios-version | sed -e 's/-\[//g' | sed -e 's/\]-//g'
            exit $?
        fi
    elif [ "$1" = "-g" -o "$1" = "-u" ]; then
        if [ "ppc64le" == "$arch" ]; then
            hypervinfo=`/opt/hmc/bin/gethypervisorinfo`
            if [ $? -eq 0 ]; then
                if [ "$hypervinfo" == "PowerVM" ]; then
                    cat /sys/firmware/devicetree/base/ibm,partition-uuid | sed -e 's/-//g'
                else
                    cat /sys/firmware/devicetree/base/vm,uuid | sed -e 's/-//g'
                fi
            else
                # UUID retrieval for POWER get UUID from /etc/machine-id
                # This is only temporary until we can find uuid from firmware
                cat /etc/machine-id
            fi
            exit 0
        else
            /usr/sbin/dmidecode --string  system-uuid | sed -e 's/-//g'| tr [:upper:] [:lower:]
            exit $?
        fi
    else
        cmodel="UNK"
        mtype="NOWN"
        sn="UNKNOWN"
        hypervinfo=`/opt/hmc/bin/gethypervisorinfo`
        if [ $? -eq 0 ]; then
            # Don't use MTM from SMBIOS
            if [ ! -f /opt/hsc/data/uvmid.out ]; then
                /opt/hsc/bin/genuvmid > /opt/hsc/data/uvmid.out
            fi
            uvmid=`cat /opt/hsc/data/uvmid.out | tr -d '\n' | tr -d ':'`
            if [ "$uvmid" != "" ]; then
                cmodel="V${uvmid:0:3}"
                mtype="${uvmid:3:3}"
                sn="${uvmid:6:3}${uvmid:12:4}"
            fi
            if [ "$1" = "-v" ]; then
            cat /opt/hsc/data/uvmid.out
            exit 0
            fi
            display_values $1
            exit 0
        fi
    fi

    if [ "ppc64le" == "$arch" ]; then
        # This is the bare metal case. -v will be ignored
        if [ $1 != "-v" ]; then
            vpdinfo=$(cat /sys/firmware/devicetree/base/model 2>/dev/null)
            if [ -n $vpdinfo ]; then
                # strip any hyphen from the machine type model
                x=$(echo $vpdinfo | sed  's/-//')
                cmodel=`echo $x | cut -d':' -f1 | cut -c1-4`
                mtype=`echo $x | cut -d':' -f1 | cut -c5-7`
            fi
            sn=$(cat /sys/firmware/devicetree/base/system-id 2>/dev/null)
            sn=$(echo ${sn/[[:blank:]]/})
            display_values $1
            exit 0
        else
            exit 0
        fi
    fi

    # First obtain MFG name to handle special OEM system
    mfg=`/usr/sbin/dmidecode --string system-manufacturer`
    # If Lenovo use Asset Tag to look up if there is nothing
    # in Asset Tag then we will use the normal mechanism
    if [ "$mfg" = "LENOVO" ]; then
        x=$(/usr/sbin/dmidecode --string chassis-asset-tag)
        case "$x" in
           *none*) ;;
           No*) ;;
           '') ;;
           *)
               cmodel=`echo $x | cut -d':' -f1 | cut -c1-4`
               mtype=`echo $x | cut -d':' -f1 | cut -c5-7`
               sn=`echo $x | cut -d':' -f2`
               display_values $1
               exit 0
               ;;
        esac
    fi
    # Use system-product-name
    x=$(/usr/sbin/dmidecode --string system-product-name)
    # Handle case where on rack mount VPD is enclosed in bracket
    # VPD returned has the form eServer xSeries  [7315R01]

    echo $x | grep "\[" 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
        cmodel=`echo $x | cut -d':' -f2 | cut -d'[' -f 2 | cut -d ']' -f 1 | cut -c1-4`
        mtype=`echo $x | cut -d':' -f2 | cut -d'[' -f 2 | cut -d ']' -f 1 | cut -c5-7`
    elif [ ${#x} -gt 9 ]; then
        # Special handling for system such as the T540
        cmodel=`echo $x | cut -d':' -f2 | cut -d'[' -f 2 | cut -d ']' -f 1 | cut -c2-5`
        mtype=`echo $x | cut -d':' -f2 | cut -d'[' -f 2 | cut -d ']' -f 1 | cut -c6-8`
    else
        cmodel=$x
        mtype=""
    fi
    sn=`/usr/sbin/dmidecode --string system-serial-number`
    # Special handling for system such as the T540
    if [ ${#sn} -gt 7 ]; then
        sn=`echo $sn | cut -d':' -f2 | cut -d'[' -f 2 | cut -d ']' -f 1 | cut -c2-8`
    fi
    display_values $1
    exit 0
}

usage()
{
    cat << EOF
usage: $0 OPTIONS
Query HMC VPD information

Options:
    -b         :  Obtains BIOS version information
    -a         :  Obtains Machine Type, Model and Serial Number
    -m         :  Obtains Machine Type, Model
    -s         :  Obtains Serial Number
    -v         :  Obtains Unique VM ID
    -g | -u    :  Obtains UUID
    -h, --help : Prints this help text
EOF
    exit $1
}

# MAIN

PATH=/usr/bin:/bin:$PATH
cmodel=""
mtype=""
sn=""
PROGNAME=$0
OPTS=$(getopt -n $PROGNAME -o asbgmuvh --long help -- "$@")
if [ $? -ne 0 ]; then
    echo "Run '$PROGNAME --help' for usage information."
    exit 1
fi

eval set -- "$OPTS"
while true; do
    case "$1" in
    -h|--help) usage 0 ;;
    -a | -s | -b | -g | -m | -v | -u) getbiosinfo $1 ;;
     *) usage 1 ;;
    esac
done
