#!/bin/sh
#
# Copyright (c) 2009 by Sun Microsystems, Inc.
# All rights reserved.
#

# registers the operating system into the service tag registry

STCLIENT=/usr/bin/stclient

# note that we reference Solaris 11 in here, this corresponds to Nevada
# and is included since we do have a product reference in swordfish
# and because it comes out as 5.11 when running the uname -r command

# check that we've got the client, if not, then we can't do anything
if [ -x ${STCLIENT} ]; then
    INSERT="false"
    UPDATE="false"
    UPDATE_INSTANCE_URN=""
    CSN=`cat /var/run/csn.out 2> /dev/null`

    # determine which urn corresponds to the current environment
    TARGET_URN=""
    if [ `uname -s` = "SunOS" ]; then
        uname=`uname -p`
        case `uname -r` in
        5.11)
            TARGET_URN="urn:uuid:6df19e63-7ef5-11db-a4bd-080020a9ed93"
            ;;
        5.10)
            TARGET_URN="urn:uuid:5005588c-36f3-11d6-9cec-fc96f718e113"
            ;;
        5.9)
            TARGET_URN="urn:uuid:4f82caac-36f3-11d6-866b-85f428ef944e"
            ;;
        5.8)
            TARGET_URN="urn:uuid:a7a38948-2bd5-11d6-98ce-9d3ac1c0cfd7"
            ;;
        *)
            exit 0
            ;;
        esac
    fi

    # the list of possible urns (solaris 8,9,10,11)
    URN_LIST="urn:uuid:6df19e63-7ef5-11db-a4bd-080020a9ed93 urn:uuid:5005588c-36f3-11d6-9cec-fc96f718e113 urn:uuid:4f82caac-36f3-11d6-866b-85f428ef944e urn:uuid:a7a38948-2bd5-11d6-98ce-9d3ac1c0cfd7"

    for urn in ${URN_LIST}; do
        TEST=`${STCLIENT} -f -t ${urn} 2> /dev/null`

        if [ "${TEST}" = "No records found" ]; then
            # there isn't a product entered into the registry
            # we might need to insert, depends if we match our TARGET_URN
            if [ "${urn}" = "${TARGET_URN}" ]; then
                INSERT="true"
            fi
        elif [ "${TEST}" = "Record not found" ]; then
            # there isn't a product entered into the registry
            # we might need to insert, depends if we match our TARGET_URN
            if [ "${urn}" = "${TARGET_URN}" ]; then
                INSERT="true"
            fi
        elif [ "${TEST}" = "" ]; then
            # there isn't a product entered into the registry
            # we might need to insert, depends if we match our TARGET_URN
            if [ "${urn}" = "${TARGET_URN}" ]; then
                INSERT="true"
            fi
        else
            # found a matching product in the registry, need to determine
            # if we should remove it or not (needs to be removed if it doesn't
            # match our TARGET_URN)
            if [ "${urn}" != "${TARGET_URN}" ]; then
                for remove_urn in ${TEST}; do
                    out=`${STCLIENT} -d -i ${remove_urn} 2> /dev/null`
                done
            else
                UPDATE_INSTANCE_URN=${TEST}
                EXISTING_SN=`${STCLIENT} -g -i ${TEST} 2> /dev/null | grep container | nawk -F "container=" '{ print $2 }'`
                # check if the CSN we found is different from the existing serial number
                # if they are different and the CSN we found isn't empty, then update
                # using the new CSN
                if [ "${EXISTING_SN}" != "${CSN}" ]; then
                    if [ "${CSN}" != "" ]; then
                        UPDATE="true"
                    fi
                fi
            fi
        fi
    done

    if [ "${UPDATE}" = "true" ]; then
        _p_name=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_name=" | nawk -F "=" '{ print $2 }'`
        _p_version=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_version=" | nawk -F "=" '{ print $2 }'`
        _p_urn=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_urn=" | nawk -F "=" '{ print $2 }'`
        _p_p_urn=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_parent_urn=" | nawk -F "=" '{ print $2 }'`
        _p_p_name=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_parent=" | nawk -F "=" '{ print $2 }'`
        _p_inst_id=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_defined_inst_id=" | nawk -F "=" '{ print $2 }'`
        _p_vendor=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "product_vendor=" | nawk -F "=" '{ print $2 }'`
        _p_arch=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "platform_arch=" | nawk -F "=" '{ print $2 }'`
        _p_source=`${STCLIENT} -g -i ${UPDATE_INSTANCE_URN} | grep "source=" | nawk -F "=" '{ print $2 }'`
        output=`${STCLIENT} -d -i ${UPDATE_INSTANCE_URN}`
        output=`${STCLIENT} -a -i ${UPDATE_INSTANCE_URN} -p "${_p_name}" -e ${_p_version} -t ${_p_urn} -S "${_p_source}" -F ${_p_p_urn} -P "${_p_p_name}" -m "${_p_vendor}" -A "${_p_arch}" -z ${CSN} -I "${_p_inst_id}"`
    fi

    if [ "${INSERT}" = "true" ]; then
        if [ `uname -s` = "SunOS" ]; then
            uname=`uname -p`
            case `uname -r` in
            5.11)
                zone=$CSN
                if [ "${CSN}" = "" ]; then
                    zone="global"
                    if [ -x /usr/bin/zonename ]; then
                        zone=`/usr/bin/zonename`
                    fi
                fi

                output=`${STCLIENT} -a -p "Solaris 11 Operating System" -e 11 -t urn:uuid:6df19e63-7ef5-11db-a4bd-080020a9ed93 -S SUNWstosreg -F urn:uuid:596ffcfa-63d5-11d7-9886-ac816a682f92 -P "Solaris Operating System" -m "Sun Microsystems" -A ${uname} -z ${zone}`
                ;;
            5.10)
                zone=$CSN
                if [ "${CSN}" = "" ]; then
                    zone="global"
                    if [ -x /usr/bin/zonename ]; then
                        zone=`/usr/bin/zonename`
                    fi
                fi

                output=`${STCLIENT} -a -p "Solaris 10 Operating System" -e 10 -t urn:uuid:5005588c-36f3-11d6-9cec-fc96f718e113 -S SUNWstosreg -F urn:uuid:596ffcfa-63d5-11d7-9886-ac816a682f92 -P "Solaris Operating System" -m "Sun Microsystems" -A ${uname} -z ${zone}`
                ;;
            5.9)
                zone=$CSN
                if [ "${CSN}" = "" ]; then
                    zone="global"
                fi
                output=`${STCLIENT} -a -p "Solaris 9 Operating System" -e 9   -t urn:uuid:4f82caac-36f3-11d6-866b-85f428ef944e -S SUNWstosreg -F urn:uuid:596ffcfa-63d5-11d7-9886-ac816a682f92 -P "Solaris Operating System" -m "Sun Microsystems" -A ${uname} -z ${zone}`
                ;;
            5.8)
                zone=$CSN
                if [ "${CSN}" = "" ]; then
                    zone="global"
                fi
                output=`${STCLIENT} -a -p "Solaris 8 Operating System" -e 8   -t urn:uuid:a7a38948-2bd5-11d6-98ce-9d3ac1c0cfd7 -S SUNWstosreg -F urn:uuid:596ffcfa-63d5-11d7-9886-ac816a682f92 -P "Solaris Operating System" -m "Sun Microsystems" -A ${uname} -z ${zone}`
                ;;
            *)
                exit 0
                ;;
            esac
        fi
    fi
fi

exit 0
