
# Stuff to handle the license manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# This checks to see that the current license file is correct and the license
# daemon is running.

proc lpopup {mess {quest 0}} {
    return [popup "License message" $mess $quest]
}

proc verify_license_daemon {} {
    global env is_win
    
    if $is_win {
	lpopup "This function does not work on Windows."
	return
    }
    
    set changed 0
    
    if [info exists env(LM_LICENSE_FILE)] {
	set file $env(LM_LICENSE_FILE)
	if {[string first : $env(LM_LICENSE_FILE)] >= 0} {
	    lpopup "Not doing check -- LM_LICENSE_FILE has more than one file"
	    return
	}
	if {[string first @ $env(LM_LICENSE_FILE)] >= 0} {
	    lpopup "Not doing check -- LM_LICENSE_FILE uses port@host format"
	    return
	}
    } else {
	set file $env(ICEM_ACN)/lic/license.dat
    }
    
    if ![file readable $file] {
	lpopup "Your license file, $file, is not readable or doesn't exist. \
		Please make sure that this file is there and try again. \
		If you need a license file please consult the installation\
		guide for information on obtaining one."
	return
    }
    
    # Read in the data from the license file.
    set fp [open $file r]
    set nlines 0
    set appnext 0
    while {![eof $fp]} {
	set l [gets $fp]
	set lines($nlines) $l
	if [string match "SERVER*" $l] {
	    set server_line $nlines
	} elseif [string match "DAEMON*" $l] {
	    set daemon_line $nlines
	} elseif [string match "FEATURE*" $l] {
	    if ![info exists feature_line] {
		set feature_line $nlines
	    }
	} elseif {$appnext || (([string index $l 0] != "\#") && \
				   ($l != "") && \
				   ![string match "INCREMENT*" $l])} {
	    # This line must be part of the previous line.  Just
	    # append it.
	    incr nlines -1
	    append lines($nlines) " $l"
	    if !$appnext {
		set changed 1
	    }
	}
	set last [expr {[string length $l] - 1}]
	if {[string index $l $last] == "\\"} {
	    set lines($nlines) [string range $lines($nlines) 0 [expr $last -1]]
	    set appnext 1
	} else {
	    set appnext 0
	}
	
	incr nlines
    }
    if $changed {
	lpopup "Joined some lines in the license file that seem to have\
		been broken."
    }
    
    # Make sure this is a reasonable machine to run this check on.
    set xx [exec $env(ICEM_ACN)/lic/lmhostid]
    if ![regexp {machine is "(.*)"} $xx dummy hostid] {
	lpopup "Can't parse the output of lmhostid: $xx"
	return
    }
    
    if [info exists server_line] {
	set fileid [lindex $lines($server_line) 2]
	if [string compare $fileid $hostid] {
	    lpopup "This is not the machine the license server should run on. \
		This machine is hostid \"$hostid\", but the server has to run\
		on machine \"$fileid\""
	    return
	}
    } else {
	set p [expr {[llength $lines($feature_line)] - 1}]
	set fileid [lindex $lines($feature_line) $p]
	if ![string compare $fileid "DEMO"] {
	    lpopup "This is a DEMO license."
	    return
	    
	} elseif [string compare $fileid $hostid] {
	    lpopup "This is not the machine the nodelocked license is for. \
		This machine is hostid \"$hostid\", but the license is for\
		machine \"$fileid\""
	    return
	}
	if !$changed {
	    lpopup "Your nodelocked license seems to be fine."
	    return
	}
    }
    
    if [info exists server_line] {
	# Check and see if the server name is correct.
	set mach [lindex [split [exec uname -n] .] 0]
	set filemach [lindex [split [lindex $lines($server_line) 1] .] 0]
	if [string compare $mach $filemach] {
	    lpopup "The machine name in your SERVER line is wrong.  It is\
		\"$filemach\" in the file and it should be \"$mach\"."
	    set lines($server_line) [lreplace $lines($server_line) 1 1 $mach]
	    set changed 1
	}
	
	# Check and see if the daemon name is correct.
	set dpath $env(ICEM_ACN)/lic/icem
	set filedpath [lindex $lines($daemon_line) 2]
	if [string compare $dpath $filedpath] {
	    lpopup "The path in your DAEMON line is wrong.  It is\
		\"$filedpath\" and it should be \"$dpath\""
	    set lines($daemon_line) [lreplace $lines($daemon_line) 2 2 $dpath]
	    set changed 1
	}
    }
    
    if $changed {
	if ![lpopup "Modify the license file $file?" 1] return
	
        if ![file writable $file] {
	    lpopup "Not modifying file -- you do not have write permission\
		for \"$file\""
	    return
	}
	exec /bin/cp $file $file.backup
	set fp [open $file w]
	for {set i 0} {$i < $nlines} {incr i} {
	    puts $fp $lines($i)
	}
	close $fp
	lpopup "Rewriting file \"$file\" -- moved the old one to\
		\"$file.backup\""
    } else {
	lpopup "Your license file seems to be correct."
    }
    
    if ![info exists server_line] return
    
    # Now see if we need to restart the license daemon.
    if [catch {exec ps -e | grep lmgrd} lmgrds] { set lmgrds "" }
    if [catch {exec ps -e | grep icem} icems] { set icems "" }
    set lpids ""; set ipids ""
    foreach out [concat [split $lmgrds \n] [split $icems \n]] {
	if {[regexp {^ *([0-9]*) .*icem$} $out dummy pid]} {
	    lappend ipids $pid
	} elseif {[regexp {^ *([0-9]*) .*lmgrd$} $out dummy pid]} {
	    lappend lpids $pid
	}
    }
    if ![llength $ipids] {
	if [llength $lpids] {
	    lpopup "It appears that lmgrd is running on your system for another\
		application, so we won't touch it."
	}
	set pids ""
    } else {
	set pids "$lpids $ipids"
    }
    if !$changed {
	if {[llength $pids] == 2} {
	    if [lpopup "The license daemon seems to be running. \
		Leave it alone?" 1] return
	}
    }
    set q 1
    if {$pids != ""} {
	if ![lpopup "Kill processes: $pids?" 1] return
	if [catch {eval exec kill -9 $pids}] {
	    if ![lpopup "Some of the processes could not be killed. \
		They are probably not owned by you. \
		Please contact the owner of the processes\
		for help in restarting the license software.\n\nDo\
		you want to try to start the license server now?" 1] return
	    set q 0
	}
    }
    
    set lock /usr/tmp/lockicem
    if [file exists $lock] {
	catch {exec rm -f $lock}
	if [file exists $lock] {
	    if ![lpopup "The file $lock can't be removed. \
		This may cause trouble.  Continue?" 1] return
	}
    }
    
    if $q { if ![lpopup "Restart the license server?" 1] return }
    
    set log $env(ICEM_ACN)/lic/logs/license.log
    catch {exec mv -f $log $log-[exec date "+%m-%d-%y.%H:%M"]}
    
    if [catch {exec $env(ICEM_ACN)/lic/lmgrd -t 15 -c $file > $log &} out] {
	lpopup $out
	return
    }
    exec sleep 3
    
    # Now check the log.
    set nope [catch {exec grep "Server started on" $log}]
    
    if $nope {
	lpopup "The license server has not started up.  Please contact your\
		distributor.  More information can be found in the file $log"
    } else {
	lpopup "Your license server is up and running.  You should now\
		be able to run ICEM CFD applications.  If you have any\
		problems please contact your distributor."
    }
}
