#!/bin/sh
#@(#)$Revision: 1.5 $  $Date: 07/06/2001 15:36:53 $
#@(#)Copyright(c) 2000 by Rational Software Corporation.
#**************************************************************************
#SOHT
#
#NAME
#     get_hostinfo - Display host name and FLEXlm hostid.
#     rs_hostinfo  - Display host name and FLEXlm hostid.
#
#SYNOPSIS
#     get_hostinfo [<options>] [<remote_host>]
#     rs_hostinfo  [<options>] [<remote_host>]
#
#DESCRIPTION
#     On UNIX, run the get_hostinfo or the rs_hostinfo scripts like this:
#           sh get_hostinfo
#       -OR-
#           ./rs_hostinfo
#
#     You must run this on the machine you plan to use as the
#     license server.  This tool is currently supported on the
#     following UNIX platforms:
#         AIX 4.x
#         Compaq Tru64 4.0
#         HP-UX B.10.xx or B.11.xx
#         IRIX 6.x
#         Linux 2.x/Intel x86
#         Solaris 2.x
#         SunOS 4.x
#
#     This tool will display the host name and FLEXlm hostid of the
#     local host.
#
#     If a remote host is specified, remote copy (rcp) this script
#     to that host, and remote execute (rsh/remsh) the script.
#
#     If the lmhostid FLEXlm command is not on your command search
#     path, this script will look for the lmhostid command in the
#     distribution area under extras/flexlm and under
#     <rational>/base/cots/flexlm.*.  If not found, it will
#     download lmhostid from the Rational FTP server.
#
#OPTIONS
#     -help or -h    Show this usage message.
#     -id or -i      Show the host ID only.
#     -vebose or -v  Run in verbose mode.
#     -trace or -x   Run in bourne-shell trace mode.
#
#DEPENDENCIES
#     If you are not on a SunOS machine and lmhostid is not on your
#     command search path, you will need to have FTP connectivity from
#     the local (or remote) host.  This way, get_hostinfo can download
#     the lmhostid command from the Rational FTP server:
     SERVER=ftp.rational.com
#
#SEE ALSO
#     rcp(1), rsh(1), remsh(1), ftp(1)
#
#EOHT
#**************************************************************************

FILE=$0
CMD=`basename $0`
HOST=`uname -n`
USER=${USER:-$LOGNAME}
TMPDIR=/tmp/$CMD.$$
do_clean=false
id_only=false
verbose=:

#------------------------------------------------------------------------
rhi_err() {
#------------------------------------------------------------------------
    echo "$CMD: $*" 2>&1
}

#------------------------------------------------------------------------
rhi_usage() {
#------------------------------------------------------------------------
# Extract the text between #SOHT and #EOHT in this script

    if [ $1 = 0 ]
    then
        sed -e '1,/^#SOHT/d' -e '/^#EOHT/,$d' -e 's/^#//' $FILE | more
    else
        rhi_err "Run '$CMD -help' for usage information."
    fi
    exit $1
}

#------------------------------------------------------------------------
rhi_set_hostid_cmd() {
#------------------------------------------------------------------------

    for dir in /bin /usr/bin /usr/ucb
    do
        if [ -x $dir/hostid ]
        then
            hostid_cmd=$dir/hostid
            $verbose Host ID command set to: $hostid_cmd
            break
        fi
    done
}

#------------------------------------------------------------------------
rhi_ftp_get_lmhostid() {
#------------------------------------------------------------------------

    $verbose mkdir $TMPDIR
    mkdir $TMPDIR
    $verbose cd $TMPDIR
    cd $TMPDIR

    ftp_cmd=ftp
    for dir in /usr/ucb /usr/bin /usr/bsd /bin
    do
        if [ -x $dir/ftp ]
        then
            ftp_cmd=$dir/ftp
            break
        fi
    done


    ftp_steps="    open $SERVER
    user ftp $USER@
    cd /public/tools/flexlm/$arch
    bin
    get lmhostid
    quit"

    $verbose "$ftp_steps | $ftp_cmd -n"

    echo "$ftp_steps" | $ftp_cmd -n

    if [ ! -f lmhostid ]
    then
        rhi_err "Failed to download /public/tools/flexlm/$arch/lmhostid
from $SERVER."
        exit 1
    fi

    $verbose chmod a+x lmhostid
    chmod a+x lmhostid
    lmhostid_cmd="$TMPDIR/lmhostid -n"
}

#------------------------------------------------------------------------
rhi_lmhostid_on_path() {
#------------------------------------------------------------------------

    for dir in `(IFS=: ; echo $PATH)`
    do
        if [ -x $dir/lmhostid ]
        then
            $verbose Found on PATH: $dir/lmhostid
            return 0
        fi
    done

    DIR=`dirname $FILE`
    ratl_dir=`(cd $DIR/../..; pwd)`
    for flexdir in \
        extras/flexlm.*.$sys \
        extras/flexlm/$sys/* \
        $ratl_dir/base/cots/flexlm.*/$arch
    do
        if [ -x $flexdir/lmhostid ]
        then
            $verbose Found: $flexdir/lmhostid
            PATH=$flexdir:$PATH
            export PATH
            return 0
        fi
    done
    return 1
}

#------------------------------------------------------------------------
rhi_show_host_id() {
#------------------------------------------------------------------------

    if $id_only
    then
        echo $HOSTID
        return
    fi

    echo
    echo "Rational Software Host Information"
    echo "=================================="
    echo "Host Name = $HOST"
    echo "Host ID   = $HOSTID"
    echo
}

#------------------------------------------------------------------------
rhi_cleanup() {
#------------------------------------------------------------------------

    if [ -d $TMPDIR ]
    then
        cd /tmp
        /bin/rm -fr $TMPDIR
    fi

    if $do_clean
    then
        /bin/rm -f /tmp/$CMD
    fi
    exit $1
}

#------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------

while [ $# -gt 0 ]
do
    case $1 in
    -clean )
        do_clean=true
        ;;
    -h|-help )
        rhi_usage 0
        ;;
    -i|-id )
        id_only=true
        r_opt="$r_opt $1"
        ;;
    -v|-verbose )
        verbose=echo
        r_opt="$r_opt $1"
        ;;
    -x|-trace )
        set -x
        r_opt="$r_opt $1"
        ;;
    -* )
        rhi_err "Option '$1' unrecognized"
        rhi_usage 1
        ;;
    * )
        rhost=$1
        break
        ;;
    esac
    shift
done

RCP=/bin/rcp
RSH=rsh
unset arch
case `uname -sr` in
AIX*)
    arch=rs6k
    sys=aix
    ;;
SunOS*4.*)
    arch=sun4
    sys=sun
    rhi_set_hostid_cmd
    RCP=/usr/ucb/rcp
    ;;
SunOS*5.*)
    arch=sun4_solaris2
    sys=sol
    rhi_set_hostid_cmd
    ;;
OSF1*)
    arch=alpha_osf1
    sys=axp
    ;;
HP-UX*)
    arch=hppa_hpux
    sys=hp
    RSH=remsh
    ;;
IRIX*5.* )
    arch=mips_irix5
    sys=sgi
    RCP=/usr/bsd/rcp
    ;;
IRIX*[6789].* )
    arch=mips_irix_n32
    sys=sg32
    RCP=/usr/bsd/rcp
    ;;
Linux*2.* )
    case `uname -m` in
    i?86 )
        arch=i386_linux2
        sys=$arch
        RCP=/usr/bin/rcp
        ;;
    ia64 )
        arch=i386_linux2
        sys=$arch
        RCP=/usr/bin/rcp
        ;;
    esac
esac

if [ -z "$arch" ]
then
    rhi_err "$HOST architecture unsupported."
    exit 1
fi


if [ -n "$rhost"  -a  "$rhost" != "$HOST" ]
then
    # Copy to script to the remote host, check that it got there
    # and then run it.
    $verbose $RCP $FILE $rhost:/tmp/$CMD
    $RCP $FILE $rhost:/tmp/$CMD
    if [ $? -ne 0 ]
    then
        $verbose $RSH $rhost /bin/ls /tmp/$CMD
        _rcmd=`$RSH $rhost /bin/ls /tmp/$CMD | tail -1`
        if [ "$_rcmd" != "/tmp/$CMD" ]
        then
            rhi_err "Cannot run remote to $rhost."
            exit 1
        fi
    fi

    # Run remote to rhost.
    $verbose $RSH $rhost /bin/sh /tmp/$CMD $r_opt -clean
    $RSH $rhost /bin/sh /tmp/$CMD $r_opt -clean
    exit 0
fi

if [ -n "$hostid_cmd" ]
then
    $verbose Running: $hostid_cmd
    HOSTID=`$hostid_cmd`
    hstatus=$?
    rhi_show_host_id
    exit $hstatus
fi

if rhi_lmhostid_on_path
then
    lmhostid_cmd="lmhostid -n"
else
    trap 'rhi_cleanup 1' 1 2 3 4 15
    rhi_ftp_get_lmhostid
fi

$verbose Running: $lmhostid_cmd
HOSTID=`$lmhostid_cmd`
hstatus=$?
rhi_show_host_id
rhi_cleanup $hstatus

