
# 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 check_project_lock {dir} {
    global project_lock env
    
    set user_name [cmd_whoami]
    set host_name [cmd_uname_n]
    
    if [info exists project_lock] free_project_lock
    
    if ![file exists $dir] { return 1 }
    
    if ![file writable $dir] {
	mess "The directory $dir is not writable by you, so no\
		locking will be done.  You will probably encounter other\
		problems also unless you fix the permissions.\n" red
	return 1
    }
    
    set project_lock $dir/project_lock
    
    if [file exists $project_lock] {
	# not if [catch {open $project_lock "WRONLY CREAT EXCL TRUNC"} fp]
	# The lock file already exists.
	if [catch {open $project_lock r} fp] {
	    mess "Warning: The advisory project lock exists but could not\
		be opened - it is probably unreadable by this user.\n" red
	    set pid unknown
	    set user unknown
	    set host unknown
	} else {
	    set stuff [read $fp]
	    close $fp
	    
	    set pid [lindex $stuff 0]
	    set user [lindex $stuff 1]
	    set host [lindex $stuff 2]
	}
	set check 1
	if {($host_name == $host) && ($pid != "unknown") && \
		![check_process_exists $pid]} {
	    mess "Warning: lock file found (pid $pid, user $user,\
		    host $host), but icemcfd is no longer running. \
		    It probably terminated abnormally.  Overriding\
 		    lock.\n" orange
	    set check 0
	}
	
	if $check {
	    set message \
		"The project \"$dir\" has been locked by user\
		\"$user\", running icemcfd with process ID $pid on machine\
		 \"$host\".  You can:\n\
		 1) Override the lock,\n\
		 2) Open the project without overriding the lock, or\n\
		 3) Cancel the Open project operation.\n\
		 If you pick 1 or 2 you should be careful not to write the\
		 same data at the same time in more than one session."
	    
	    switch [tk_dialog .lock "Lock problem" $message "" 0 \
			Override Open Cancel ] {
		0 { # Just fall through.
			    }
		1 {
		    mess "Warning: project opened without locking.\n" red
		    return 1
		}
		2 {
		    mess "Warning: not opening project.\n" red
		    return 0
		}
	    }
	}
	ic_rm $project_lock
	if [catch {open $project_lock w} fp] {
	    error "Can't create project lock -- the $dir\
		    directory is probably not writable by you."
	}
    } else {
	if [catch {open $project_lock w} fp] {
	    mess "$fp\n" red
	    mess "Warning: project opened without locking.\n" red
	    return 1
	    
	}
    }
    mess "Acquiring lock on project\n"
    puts $fp "[pid] $user_name $host_name"
    close $fp
    return 1
}

proc free_project_lock {} {
    global project_lock
    
    if ![info exists project_lock] {
	# errormsg "Error: can't release lock: no lock held."
	return
    }
    
    set dir [file dirname $project_lock]
    
    if ![file exists $project_lock] {
	mess "Error: can't release lock for project\
		\"$dir\" - the lock file is missing.\n" red
	return
    }
    
    set fp [open $project_lock r]
    set stuff [read $fp]
    close $fp
    
    if {[pid] != [lindex $stuff 0]} {
	mess "Error: Can't release lock for project \"$dir\" -\
		the lock has been overridden by another process:\
		user [lindex $stuff 1], process ID [lindex $stuff 0],\
		on machine \"[lindex $stuff 2]\".\n" red
	return
    }
    
    mess "Releasing lock on project \"$dir\"\n"
    ic_rm $project_lock
    unset project_lock
}
