
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Check various system-related parameters.


proc check_sysinfo {} {
    global is_win
    
    mess "\nSystem info -----\n"
    
    set local_display 1
    set console_display 1
    
    if $is_win {
	mess "You are using Windows.\n"
	return
    }
    
    # ----------------------------------
    
    set sysgen /usr/sysgen/master.d/kernel
    if [file exists $sysgen] {
	if ![catch {exec /bin/grep availsmem_accounting $sysgen} line] {
	    if [regexp {=[ 	]*([0-9]+)} $line dummy num] {
		if $num {
		    mess "Your system is configured incorrectly for\
			    running ICEM CFD.  The value of the\
			    \"availsmem_accounting\" variable in \
			    \"/usr/sysgen/master.d/kernel\" must be 0. \
			    Please change this value and recompile the\
			    kernel.  Before this is done you may have\
			    serious performance problems with ICEM CFD\
			    programs, especially relating to swap space.\n" red
		}
	    }
	}
    }
    
    # ----------------------------------
    
    if {[file exists /dev/lan0] && ![file readable /dev/lan0]} {
	warningmsg "The file /dev/lan0 must be readable for the licensing\
		    software to work.  Please change the permissions before\
		    starting any ICEM CFD applications."
    }
    
    # ----------------------------------
    
    if [file executable /usr/bin/lslpp] {
	set fp [open "|/usr/bin/lslpp -h xlf*.obj" r]
	while {![eof $fp]} {
	    set line [gets $fp]
	    if [regexp {([0-9]+)\.([0-9]+)\.[0-9]+\.[0-9]+[ \t]COMPLETE} \
		    $line dummy rev1 rev2] {
		set rev1 [expr $rev1]; set rev2 [expr $rev2]
		if {($rev1 < 2) || (($rev1 == 2) && ($rev2 < 3))} {
		    warningmsg "Some components of your AIX installation are\
			    at revision level less than 2.3 (version found was\
			    $rev1.$rev2).  You can check this using the\
			    \"lslpp\" command.  Please upgrade to at\
			    least version 2.3."
		    break
		}
	    }
	}
	catch {close $fp}
    }
    
    # ----------------------------------
    
    set totalmem 0
    set minmem 1
    
    if {[file executable /bin/hinv] && [file executable /bin/grep]} {
	set mem [exec /bin/hinv | /bin/grep "Main memory"]
	if [regexp {: ([0-9]+)} $mem dummy mem] {
	    mess "Main memory: $mem Mbytes\n"
	    incr totalmem $mem
	}
    } elseif {[string match AIX* [exec uname -a]] && \
		  [file executable /usr/sbin/lsdev]} {
	set fp [open "|/usr/sbin/lsdev -C" r]
	set mem 0
	while {![eof $fp]} {
	    set line [gets $fp]
	    if [regexp {([0-9]+) MB Memory} $line dummy num] {
		incr mem $num
	    }
	}
	close $fp
	if $mem {
	    mess "Main memory: $mem Mbytes\n"
	    incr totalmem $mem
	}
    } else {
	foreach mess [glob -nocomplain /usr/adm/messages*] {
	    if ![catch {exec /bin/grep ": mem" $mess} stuff] {
		if [regexp {mem = ([0-9]+)} $stuff dummy m] {
		    set mem [expr {$m / 1000}]
		    break
		}
	    }
	}
	if [info exists mem] {
	    mess "Main memory: $mem Mbytes\n"
	    incr totalmem $mem
	}
    }
    
    if [file executable /etc/swap] {
	if ![catch {exec /etc/swap -l} stuff] {
	    set total 0
	    # There are different formats for this command in IRIX 4 and 5.
	    case [lindex $stuff 0] in {
		path {
		    for {set i 8} {[lindex $stuff $i] != ""} {incr i 5} {
			if [scan [lindex $stuff $i] %d num] {
			    incr total [expr {$num / 2000}]
			}
		    }
		}
		lswap {
		    for {set i 14} {[lindex $stuff $i] != ""} {incr i 9} {
			if [scan [lindex $stuff $i] %d num] {
			    incr total [expr {$num / 2000}]
			}
		    }
		}
	    }
	    incr totalmem $total
	    if $total {
		mess "Swap space: $total Mbyte\n"
	    }
	}
    } elseif [file executable /etc/pstat] {
	if ![catch {exec /etc/pstat -s} stuff] {
	    set pat {([0-9]+)k allocated . ([0-9]+)k reserved = ([0-9]+)k\
		    used, ([0-9]+)k available}
	    if [regexp $pat $stuff dummy s_all s_res s_used s_avail] {
		set total [expr {$s_used / 1000 + $s_avail / 1000}]
		incr totalmem $total
		if $total {
		    mess "Swap space: $total Mbytes\n"
		}
	    }
	}
    } elseif [file executable /etc/lsps] {
	set fp [open "|/etc/lsps -a" r]
	set mem 0
	while {![eof $fp]} {
	    set line [gets $fp]
	    if [regexp {([0-9]+)MB} $line dummy num] {
		incr mem $num
	    }
	}
	close $fp
	if $mem {
	    mess "Swap space: $mem Mbytes\n"
	    incr totalmem $mem
	}
    } elseif [file executable /etc/swapinfo] {
	set fp [open "|/etc/swapinfo" r]
	set mem 0
	gets $fp; gets $fp
	while {![eof $fp]} {
	    set line [gets $fp]
	    catch {incr mem [expr {[lindex $line 1] / 1024}]}
	}
	close $fp
	if $mem {
	    mess "Swap space: $mem Mbytes\n"
	    incr totalmem $mem
	}
    }
    
    if $totalmem {
	mess "Total space: $totalmem Mbytes\n"
	if {$totalmem < $minmem} {
	    warningmsg "You should have at least $minmem Mbytes of tota \
		    memory (main + swap) to ensure optimal performance\
		    from ICEM CFD"
	}
    }
    
    # ----------------------------------
    
    if {$local_display && $console_display && \
	    [file executable /usr/bin/graphinfo]} {
	catch {exec /usr/bin/graphinfo | /bin/grep "image planes"} line
	set planes [lindex $line [expr {[llength $line] - 1}]]
	if {$planes != ""} {
	    mess "Your machine has $planes graphics bitplanes.\n"
	}
    } else {
	foreach xdpyinfo {
	    /usr/bin/X11/xdpyinfo
	    /usr/X11/bin/xdpyinfo
	    /usr/openwin/bin/xdpyinfo
	    /usr/openwin3.0/bin/xdpyinfo
	} {
	    if [file executable $xdpyinfo] {
		set lines [split [exec $xdpyinfo | /bin/grep "depth:"] \n]
		set planes 0
		foreach l $lines {
		    if [regexp {depth:[ \t]*([0-9]+)[ \t]*planes} $l dummy pl] {
			if {$pl > $planes} { set planes $pl }
		    }
		}
		mess "Your machine has $planes graphics bitplanes.\n"
		break
	    }
	}
    }
    
    # ----------------------------------
    if 0 {
	set vs /etc/config/vswap
	set vso /etc/config/vswap.options
	if {[file exists $vs] && [file exists $vso]} {
	    set what [exec cat $vs]
	    set vsl [exec grep vswaplen $vso]
	    if ![regexp {vswaplen[ \t]*=[ \t]*([0-9]+)+} $vsl dummy howmuch] {
		warningmsg "Can't parse line in $vso"
	    } else {
		if {($what == "off") || ($howmuch < 100000)} {
		    set lev [exec uname -sr]
		    
		    warningmsg "The amount of virtual swap space you have\
			    configured for your IRIX $lev system may be\
			    too small for ICEM CFD to run correctly."
		    infomsg "You should make the following modifications:"
		    infomsg "1. Change the file $vs to read \"on\"."
		    infomsg "2. Change the \"vswaplen\" option in $vso to be\
			    at least 800000 (i.e, 10x what it is now)."
		    infomsg "3. Reboot your system."
		    infomsg "Please call your distributor if you have\
			    any questions."
		}
	    }
	}
    }
}

