#! /bin/csh -f
#
# Copyright 10/10/96 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)frametomif	1.3	96/10/10 SMI

#	frametomif: convert the argument file(s) from frame to mif format.
#
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.

set fmbatch = /usr/dist/local/exe/fmbatch
set my_name = `basename $0`

if ( $#argv < 1 ) then
    echo "usage: ${my_name} file[.frame] ..."
    exit 1
endif

foreach source ( $argv )
    #	default the source extension 
    if ( ${source:e} == "" ) then
	set source = ${source}.frame
    endif
    #	check that the source is readable.
    if ( ! -r ${source} ) then
	echo "${my_name}: Couldn't read ${source}"
	exit 1
    endif
    #	default the destination extension to .mif.
    set destination = ${source:r}.mif
    #	check that the destination is okay to write.
    if ( -e ${destination} ) then
	# if the destination exists, we have to check it
	if ( ! -w ${destination} ) then
	    # destination isn't writeable, then forget writing it.
	    echo "${my_name}: ${destination} is not writeable"
	    exit 1
	else
	    if { newer ${destination} ${source} } then
		# destination is newer than the source, don't smash it.
		echo "${my_name}: ${source} is older than ${destination}"
		exit 1
	    else
		# give warning for overwriting old destination.
		echo "${my_name}: overwriting ${destination}"
	    endif
	endif
    else
	echo "${my_name}: creating ${destination}"
    endif
    #	unlock the source, if someone has it in maker.
    #	fmbatch fails, otherwise.
    if ( -e ${source}.lck ) then
	echo "${my_name} unlocking ${source}"
	set lock
	mv ${source}.lck ${source}.lck.$$
    endif
    #	copy from source to destination changing format to .mif.
    echo \
	"Open ${source} \
	 SaveAs m ${source} ${destination} \
	 Quit" \
    | ${fmbatch}
    #	replace lock, if any
    if ( $?lock ) then
	mv ${source}.lck.$$ ${source}.lck
    endif
end
