#! /bin/sh

#
# Copyright (c) 1994 Open Software Foundation, Inc.
# 
# Permission is hereby granted to use, copy, modify and freely distribute
# the software in this file and its documentation for any purpose without
# fee, provided that the above copyright notice appears in all copies, and
# that both the copyright notice and this permission notice appear in
# supporting documentation.  Further, provided that the name of Open
# Software Foundation, Inc. ("OSF") not be used in advertising or
# publicity pertaining to distribution of the software without prior
# written permission from OSF.  OSF makes no representations about the
# suitability of this software for any purpose.  It is provided "AS IS"
# without express or implied warranty.
#

true && eval 'exec otcl /project/ot/tcl/otadmin ${1+"$@"}'

# This should be replaced with - #! /usr/local/bin/otcl - someday.
# At the moment for operational reasons we need to keep all OT client
# binaries in /project/tools/bin/..., which means we cannot use the
# hashbang convention because our binary directory names 1) exceed the
# unarchitected limit of 32 characters and 2) have machine-dependent
# strings which cannot be fed in via envirovars, which are inaccess. to
# the kernel.  There is a pseudovariable '1..@' defined in otcl so this first
# line will get by the TCL interpreter.

set argv0 [file tail $argv0]

# otadmin
# Open Track administration.

set otadminUsage [format {usage: %s [option ...] 
  -k            restart otKwikPix server for project <proj>
  -p <proj>     relative to project <proj>
  -s <pattern>  scan project log for pattern
		e.g. to scan for pnh's connections on 11/21/93
		  otadmin -pnot -s 11/21/93.*userName.*pnh
  -u <num>	unlock object <num>
} $argv0] 

proc otAdminPrintHelp { } {
    global otadminUsage

    puts stdout $otadminUsage nonewline
    return
}

proc otAdminConnect { curPrj } {

    if { $curPrj == "" } {
	error "Requires project specification"
    }
    set msg [project set $curPrj]
    if { [string match {*not found} $msg] } {
	error $msg
    }
    if { [catch [list remote [list project set $curPrj]] msg] } {
	error $msg
    }
    if { [catch [list remote [list source /project/ot/tcl/otAdmin.tcl]] msg] } {
	error $msg
    }
    return 
}

proc otAdminCheckKwikPix { curPrj } {

    if { [catch [list otAdminConnect $curPrj] msg] } {
	error $msg
    } 
    if { [catch [list remote [list otCheckKwikPix $curPrj]] msg] } {
	error $msg
    }
    return $msg

}

proc otAdminUnlock { curPrj objNum } {

    if { $objNum == "" } {
	error "Unlocking requires object number"
    }
    if { [catch [list otAdminConnect $curPrj] msg] } {
	error $msg
    } 

# Get names of matching files
    if { [catch [list remote [list otMatchingFilesLs $objNum]] msg] } {
	error $msg
    } else {
	puts stdout $msg
    }
    
    puts stdout {Do you want to unlock it? (y/n) } nonewline

    set ans [gets stdin]
    if { [string match {y*} $ans] } {
	if { [catch [list remote [list unlock $objNum]] msg] } {
	    error $msg
	} else {
	    return "Unlocked"
	}
    } else {
	return {}
    }

}

proc otAdminExec { curPrj toEval } {

    if { $toEval == "" } {
	error "Null string for evaluation"
    }
    if { [catch [list otAdminConnect $curPrj] msg] } {
	error $msg
    } 
    if { [catch [list remote $toEval] msg] } {
	error $msg
    } else {
	return $msg
    }

}

proc otAdminScan { curPrj scanCri } {

    if { $scanCri == "" } {
	error "Null scan criteria"
    }
    if { [catch [list otAdminConnect $curPrj] msg] } {
	error $msg
    } 
    if { [catch [list remote [list otScanLog $scanCri]] msg] } {
	error $msg
    } else {
	return $msg
    }

}

set i 	    0
set kwikPix {}
set unlock  {}
set curPrj  {}
set objNum  {}
set scanCri {}
set toEval  {}

if { $argc == 0 } {
    otAdminPrintHelp
    exit 1
}

while { $i < [llength $argv] } {

    switch -glob -- [lindex $argv $i] {
	{-k}	{ set kwikPix 1						}
	{-p}	{ set curPrj [lindex $argv [incr i]]			}
	{-p*}	{ set curPrj [string range [lindex $argv $i] 2 end] 	}
	{-u}	{ set objNum [lindex $argv [incr i]]
		  set unlock 1
		}
	{-s}	{ set scanCri [lindex $argv [incr i]]			}
	{-u*}	{ set objNum [string range [lindex $argv $i] 2 end]
		  set unlock 1
	 	}
	{-x}	{ set toEval [lindex $argv [incr i]]			}
	*	{ otAdminPrintHelp ; exit 1				}
    } 
    incr i

}

if { $kwikPix == 1 } {
    if { [catch [list otAdminCheckKwikPix $curPrj] msg] } {
	puts stderr $msg
	exit 1
    } else {
	if { $msg != "" } {
	    puts stdout $msg
	}
	exit 0
    }
}

if { $unlock == 1  } {
    if { [catch [list otAdminUnlock $curPrj $objNum] msg] } {
	puts stderr $msg
	exit 1
    } else {
	if { $msg != "" } {
	    puts stdout $msg
	}
	exit 0
    }
}

if { $toEval != "" } {
    if { [catch [list otAdminExec $curPrj $toEval] msg] } {
	puts stderr $msg
	exit 1
    } else {
	if { $msg != "" } {
	    puts stdout $msg
	}
	exit 0
    }
}

if { $scanCri != "" } {
    if { [catch [list otAdminScan $curPrj $scanCri] msg] } {
	puts stderr $msg
	exit 1
    } else {
	if { $msg != "" } {
	    puts stdout $msg
	}
	exit 0
    }
}
