#!/bin/sh
#
# (c) 2003-2007 Linbox FAS, http://linbox.com
# (c) 2008-2009 Mandriva, http://www.mandriva.com
#
# $Id$
#
# This file is part of Pulse 2, http://pulse2.mandriva.org
#
# Pulse 2 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Pulse 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# HW Inventory
#
# TODO: DOS drives and partitions
#

if [ "$1" != "stdout" ]; then
    ETH=`cat /etc/eth`
    MAC=`cat /etc/mac`

    . /etc/netinfo.sh
    SRV=$Next_server
    [ -z "$SRV" ] && exit 1
fi

# self-explanatory...
# cannot use busybox's printf which does not support hex output
hex()
{
    case $1 in
    10)
        echo a
        ;;
    11)
        echo b
        ;;
    12)
        echo c
        ;;
    13)
        echo d
        ;;
    14)
        echo e
        ;;
    15)
        echo f
        ;;
    *)
        echo $1
        ;;
    esac
}

# read a number in stdin and output its hex value
dectohex()
{
    NUM=`tr -cd 01234567989`
    OUT=""
    while [ $NUM != "0" ]
    do

        OUT="`hex $(($NUM%16))`$OUT"
        NUM=$(($NUM/16))
    done
    echo $OUT
}

diskinfo()
{
    NUM=0
    sfdisk -l -uS|grep /dev|tr -d "*"|tr -s " "|while read FIRST ALL
    do
        if [ $FIRST = "Disk" ]
        then
            PNUM=0
            C=`echo $ALL|cut -f 2 -d " "`
            H=`echo $ALL|cut -f 4 -d " "`
            S=`echo $ALL|cut -f 6 -d " "`
            TOT=$(($C*$H*$S))
            echo "D:(hd$NUM):CHS($C,$H,$S)=$TOT"
            NUM=$(($NUM+1))
        else
            T=`echo $ALL|cut -f 4 -d " "`
            S=`echo $ALL|cut -f 1 -d " "`
            L=`echo $ALL|cut -f 3 -d " "`
            [ "$S" = "0" ] && continue
            [ "$T" = "5" ] && continue
            echo "P:$PNUM,t:$T,s:$S,l:$L"
            PNUM=$(($PNUM+1))
        fi
    done
    exit
}

pciinfo()
{
    # still missing: bus number, function number.
    cat /proc/pci|grep Class|sed 's/.*Class \([^:]*\): PCI device \([^:]*\):\([^ ]*\).*/B:0,f:0,v:\2,d:\3,c:\1/'
}

echo "*** Sending inventory ***"

MEM=`cat /proc/meminfo |grep MemTotal|dectohex`
FREQ=`cat /proc/cpuinfo|grep "^cpu MHz"|tr -cd 0123456789`
CPU1=`cat /proc/cpuinfo|grep "^cpu fami"|tr -cd 0123456789`
CPU2=`cat /proc/cpuinfo|grep "^model"|tr -cd 0123456789`
CPU4=`cat /proc/cpuinfo|grep "^stepping"|tr -cd 0123456789`
CPUV=47
grep AuthenticAMD /proc/cpuinfo && CPUV=41
PCI=`pciinfo`
DI=`diskinfo`

DATA="M:280,U:$MEM
$PCI
C:$CPU1,$CPU2,0,$CPU4,0,0,0,0,0,0,0,0,$CPUV
F:$FREQ
$DI"

if [ "$1" != "stdout" ]; then
    echo -en "\252$DATA\00Mc:$MAC" | nc -p 1001 -w 1 $SRV 1001
    sleep 1
else
    echo "$DATA"
fi
