
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc license_start {} {
    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} {
	    mess "Not doing check -- LM_LICENSE_FILE has more than\
		one file\n" red
	    return
	}
	if {[string first @ $env(LM_LICENSE_FILE)] >= 0} {
	    mess "Not doing check -- LM_LICENSE_FILE uses port@host\
		format\n" red
	    return
	}
    } else {
	set file $env(ICEM_ACN)/lic/license.dat
    }
    
    if ![file readable $file] {
	mess "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.\n" red
	return
    }
    
    # Read in the data from the license file.
    set fp [open $file r]
    set nlines 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 ICEM_CFD*" $l] {
	    set daemon_line1 $nlines
	} elseif [string match "DAEMON icem*" $l] {
	    set daemon_line2 $nlines
	} elseif [string match "FEATURE*" $l] {
	    if ![info exists feature_line] {
		set feature_line $nlines
	    }
	}
	incr nlines
    }
    
    # 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] {
	mess "Can't parse the output of lmhostid: $xx\n" red
	return
    }
    
    if [info exists server_line] {
	set fileid [lindex $lines($server_line) 2]
	if [string compare $fileid $hostid] {
	    mess "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\"\n" red
	    return
	}
    } else {
	set p [expr {[llength $lines($feature_line)] - 1}]
	set fileid [lindex $lines($feature_line) $p]
	if ![string compare $fileid "DEMO"] {
	    mess "This is a DEMO license.\n" red
	    return
	    
	} elseif [string compare $fileid $hostid] {
	    mess "This is not the machine the nodelocked license is for. \
		This machine is hostid \"$hostid\", but the license is for\
		machine \"$fileid\"\n" red
	    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] {
	    mess "The machine name in your SERVER line is wrong.  It is\
		\"$filemach\" in the file and it should be \"$mach\".\n" red
	    set lines($server_line) [lreplace $lines($server_line) 1 1 $mach]
	    set changed 1
	}
	
	# Check and see if the daemon names are correct.
	if [info exists daemon_line1] {
	    set dpath $env(ICEM_ACN)/lic/ICEM_CFD
	    set filedpath [lindex $lines($daemon_line1) 2]
	    if [string compare $dpath $filedpath] {
		mess "The path in your DAEMON ICEM_CFD line is wrong.  It is\
		\"$filedpath\" and it should be \"$dpath\"\n"
		set lines($daemon_line1) [lreplace $lines($daemon_line1) 2 2 $dpath]
		set changed 1
	    }
	}
	if [info exists daemon_line2] {
	    set dpath $env(ICEM_ACN)/lic/icem
	    set filedpath [lindex $lines($daemon_line2) 2]
	    if [string compare $dpath $filedpath] {
		mess "The path in your DAEMON icem line is wrong.  It is\
		\"$filedpath\" and it should be \"$dpath\"\n"
		set lines($daemon_line2) [lreplace $lines($daemon_line2) 2 2 $dpath]
		set changed 1
	    }
	}
    }
    
    if $changed {
	if ![lpopup "Modify the license file $file?" 1] return
	
        if ![file writable $file] {
	    mess "Not modifying file -- you do not have write permission\
		for \"$file\"\n" red
	    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
	mess "Rewriting file \"$file\" -- moved the old one to\
		\"$file.backup\"\n" red
    } else {
	mess "Your license file seems to be correct.\n"
    }
    
    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 "" }
    if [catch {exec ps -e | grep ICEM_CFD} icemcfds] { set icemcfds "" }
    set lpids ""; set ipids ""; set icpids ""
    foreach out [concat [split $lmgrds \n] [split $icems \n] \
		     [split $icemcfds \n]] {
	if {[regexp {^ *([0-9]*) .*icem$} $out dummy pid]} {
	    lappend ipids $pid
	} elseif {[regexp {^ *([0-9]*) .*lmgrd$} $out dummy pid]} {
	    lappend lpids $pid
	} elseif {[regexp {^ *([0-9]*) .*ICEM_CFD$} $out dummy pid]} {
	    lappend icpids $pid
	}
    }
    if {![llength $ipids] && ![llength $icpids]} {
	if [llength $lpids] {
	    mess "It appears that lmgrd is running on your system for another\
		application, so we won't touch it.\n" red
	}
	set pids ""
    } else {
	set pids "$lpids $ipids $icpids"
    }
    if !$changed {
	if {[llength $pids] > 2} {
	    if [lpopup "The license daemon seems to be running already. \
		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
	}
    }
    
    foreach lock {/usr/tmp/lockicem /usr/tmp/lockICEM_CFD} {
	if [file exists $lock] {
	    catch {exec rm -f $lock}
	    if [file exists $lock] {
		if ![lpopup "The file $lock can't be removed. \
		    This will probably 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] {
	mess "$out\n" red
	return
    }
    exec sleep 3
    
    # Now check the log.
    set nope [catch {exec grep "Server started on" $log}]
    
    if $nope {
	mess "The license server has not started up.  Please contact your\
		distributor.  More information can be found in the\
		file $log\n" red
    } else {
	mess "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.\n" red
    }
    if [catch {enable_licenses $file} err] {
	mess "Warning: checking license: $err\n" red
    }
}

proc license_check {} {
    mess "\n[license_usage ""]\n" blue
}

proc unused__license_check_file {} {
    global env
    ic_run_application_exec . icemcfd license_check ""
}

proc license_is_lost {} {
    lpopup "Warning: the flexlm license server has gone down \
and this program has lost its license.  Please save your work, and then do \
either File/Utilities/License/Start server or Reconnect, \
or ask your system administrator to start the license server."
}

proc license_acquire {} {
    global lm_license_file licensing_flags license_message env
    
    if ![info exists lm_license_file] {
	set lm_license_file $env(ICEM_ACN)/lic/license.dat
    }
    
    if [catch {enable_licenses $lm_license_file $licensing_flags} err] {
	mess "Warning: checking license: $err\n" red
    }
    if [info exists license_message] {
	mess "$license_message\n"
    }
}
