#! /bin/sh
#
# systype:  figure out system type
#
# History:
#   19960925 GRR  updated custom code for /etc/issue:  whoa, gnarlier
#   19961020 GRR  updated memory code for 1.x kernels
#   19961112 GRR  fixed memory code calculation (round to nearest 4MB)
#   19961121 GRR  updated for Pentium Pro bogomips
#   19971210 GRR  updated memory code again (still get 93MB for 95MB system)
#   19980527 GRR  updated for Pentium II and 486 DX4
#   20000126 GRR  updated for Pentium III
#   20000224 GRR  merged 19980529 CPU-speed and /usr/bin/dc improvements
#   20000719 GRR  fixed dc "P" vs. "p" bug; updated for "kni" vs. "xmm"
#   20001116 GRR  modified to take "-rclocal" option; added modelname check
#   20010119 GRR  added AMD Athlon (tested) and Duron (untested) support
#   20010129 GRR  added Cyrix MediaGX support (tested); removed "rc.local done"


# GRR notes:
#
#   Athlon:		CPU speed == bogomips x 1
#   P3:			CPU speed == bogomips x 1 or 0.5, depending on chip???
#   PPro, P2:		CPU speed == bogomips x 1 (round to nearest 16.667)
#   Pentium MMX:	CPU speed == bogomips x 0.5
#   Pentium:		CPU speed == bogomips x 2.5 (round to nearest 6.667)
#   486:		CPU speed == bogomips x 2.0 (round to nearest 16.667)
#   386sx:		CPU speed == bogomips x 6.67 (don't round?)
#
# Calibrations are well-tested on Pentiums and up; not as well on lesser chips
# and on non-Intel chips (e.g., AMD, Cyrix, IBM, etc.).
#
#   cpu        : 686
#   model      : 3
#   flags      : fpu vme de pse tsc msr pae mce cx8 11 mtrr pge mca cmov mmx
#
#   cpu        : 686
#   model      : 0
#
#   cpu        : 586
#   model      : Pentium 75+
#
#   cpu        : 486
#   model      : DX2-66
#
#   cpu        : 386
#   model      : Unknown
#
#
# dc supports fractional precision, e.g.,
#	echo "2 k 5.33 2.33 / p" | dc
# => 2.28
#
# [round all to nearest 3.3333?  no, doesn't work for 264 -> 266]


#-----------------------------------------------------------------------------
# systype:  old /etc/rc.d/rc.local part began here
# systype:  figure out system type (GRR 19960925 - 20000720)
#
# GRR 20001116:  now systype is designed to be run as a separate script,
#                even from /etc/rc.d/rc.local (use -rclocal option).

#echo "number of command-line options = $#"

if [ $# -eq 0 ]; then
    standalone="true"
else
    if [ "$1" = "-rclocal" ]; then
        standalone="false"
    else
        echo "systype:  unknown option: $1"
        exit 1
    fi
fi

filetype=`/usr/bin/file /bin/cp | grep ELF`
if [ "$filetype" = "" ]; then
    bintype="a.out"
else
    bintype="ELF"
fi

cputype=`cat /proc/cpuinfo | egrep -i '^cpu' | head -1 | sed 's/^.*: //'`
cpuname=`cat /proc/cpuinfo | egrep -i model | head -1 | sed -e 's/^.*: //' -e 's/ [0-9].*//'`
modelname=`cat /proc/cpuinfo | egrep -i '^model name' | head -1 | sed -e 's/^.*: //' -e 's/ [0-9].*//' -e 's/AMD //' -e 's/Cyrix //' -e 's/tm / /g' -e 's/ (.*)//' -e 's/MMX .*//' -e 's/  *$//'`

mmx=`cat /proc/cpuinfo | egrep -i flags | egrep -i ' mmx| cxmmx'`
kni=`cat /proc/cpuinfo | egrep -i flags | egrep -i ' kni| xmm'`
numcpus=`cat /proc/cpuinfo | egrep -i model | egrep -v "model name" | wc -l | sed 's/ *//'`
realmhz=`cat /proc/cpuinfo | egrep -i '^cpu MHz' | head -1 | sed 's/^.*: //'`
bogomips=`cat /proc/cpuinfo | egrep -i '^bogomips' | head -1 | sed 's/^.*: //'`
bogoint=`echo $bogomips | sed 's/\..*//'`


case "$cputype" in
    3 | 386*)
        systype="i386"
        mult="6.67"
        ;;
    4 | 486*)
        systype="i486"
        mult="2.0"
        ;;
    5 | 586*)
        if [ "$modelname" != "" ]; then
            systype="$modelname"
        else
            systype="Pentium"
        fi
        if [ "$mmx" != "" ]; then
            systype="$systype MMX"
            mult="0.5"
        else
            mult="2.5"
        fi
        ;;
    6 | 686*)
        if [ "$modelname" != "" ]; then
            systype="$modelname"
        else
            case "$cpuname" in
                0 | "Pentium Pro")
                    systype="Pentium Pro"
                    ;;
                3 | "Pentium II")
                    systype="Pentium II"
                    ;;
                *Athlon*)
                    systype="Athlon"
                    ;;
                *Duron*)
                    systype="Duron"
                    ;;
                *)
                    if [ "$kni" != "" ]; then
                        systype="Pentium III"
                    elif [ "$mmx" != "" ]; then
                        systype="Pentium II"
                    elif [ $bogoint -gt 240 ]; then
                        systype="Pentium II"
                    elif [ $bogoint -lt 220 ]; then
                        systype="Pentium Pro"
                    else
                        systype="Pentium Pro/II"
                    fi
                    ;;
            esac
        fi
        mult="1.0"
        ;;
    *)
        systype="$cputype"
        mult=""
        ;;
esac


#if [ $numcpus -eq 2 ]; then
#    systype="dual $systype"
#elif [ $numcpus -eq 4 ]; then
#    systype="quad $systype"
#fi


if [ "$realmhz" = "" -a "$mult" = "" ]; then
    systype="$systype ($bogomips bogomips)"
else
    # round MHz to nearest 16.667 (GRR:  need list of special cases that break)
    #mhz=`echo "3 k $bogomips $mult * p" | dc`
    #                              v MHz with fractional part
    #                                      v MHz with additional rounding amount
    #                                          v truncate divisions
#   mhzf=`echo "3 k $bogomips $mult * 8.333 + 0 k 16.667 % p" | dc`
#   mhzf=`echo "3 k $bogomips $mult * 8.333 + 0 k 16.667 / 3 k 16.667 * p" | dc`

    # round MHz to nearest 1.0
    if [ "$realmhz" = "" ]; then
        mhzf=`echo "3 k $bogomips $mult * 0.5 + 0 k 1 / p" | dc`
    else
        mhzf=`echo "3 k $realmhz 0.5 + 0 k 1 / p" | dc`
    fi

    #echo "DEBUG: floating-point mhz = $mhzf"
    mhz=`echo $mhzf | sed 's/\..*//'`
    systype="$mhz MHz $systype"
fi


if [ $numcpus -eq 2 ]; then
    systype="dual $systype"
elif [ $numcpus -eq 4 ]; then
    systype="quad $systype"
fi


#MemTotal:     64176 kB
#memkb=`cat /proc/meminfo | egrep -i '^MemTotal' | sed -e 's/^.*: *//' -e 's/ .*//'`
#systype="`expr $memkb / 1000` MB, $systype system"

#Mem:  15458304  7094272  8364032  5521408  3223552
mem=`cat /proc/meminfo | egrep -i '^Mem:' | sed -e 's/^.*: *//' -e 's/ .*//'`

# GRR NUKE #  #memmb=`expr \( $mem / 1024 + 3000 \) / 1024`
#memmb=`expr \( \( $mem / 1048576 + 3 \) / 4 \) \* 4`
#memmb=`expr \( $mem + 524288 \) / 1048576  + 1`
# GRR 19980528:  dc is RPN calculator; binary is less than half the size of expr
memmb=`echo "$mem 524288 + 1048576 / 1 + p" | dc`

systype="$memmb MB, $systype system"


#welcome="[Red Hat Commercial Linux 2.0]"
#welcome="Kernel $(uname -r) on a $(uname -m)"
#welcome="Linux $(uname -r) ($bintype) on a $systype"
welcome="Welcome to Linux $(uname -r) ($bintype) on a $systype."

if [ "$standalone" = "true" ]; then
    echo "$welcome"
else
    echo "" > /etc/issue
    echo "$welcome" >> /etc/issue
    #cp -f /etc/issue /etc/issue.net
    echo >> /etc/issue
fi
