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

#
#	miftoframe: convert the argument file(s) from .mif to .frame 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} [-locked|-unlocked] [-o file] file[.mif] ..."
	exit 1
endif

# default is editable maker format, use -locked to create locked destination.
set dest_type = "d"
set destination = ""


while ( $#argv > 0 )
    set source = $argv[1]
    shift

    if ("${source}" == "-o") then
	set destination = $argv[1]
	shift
	continue
    endif

    if ("${source}" == "-locked") then
	set dest_type = "l"
	continue
    endif

    if ("${source}" == "-unlocked") then
	set dest_type = "d"
	continue
    endif

    #	default source extension to .mif.
    if ( ${source:e} == "" ) then
	set source = ${source}.mif
    endif

    #	check that the source is readable.
    if ( ! -r ${source} ) then
	echo "${my_name}: Couldn't read ${source}"
	exit 1
    endif

    #	check if mif is a book
    head -1 $source | grep -s Book
    if ($status == 0) then
	set dest_type = ""
	set is_book = 1
    endif

    #	default destination extension
    if ("${destination}" == "") then
	if ( $?is_book ) then
            set destination = ${source:r}.book
        else
            set destination = ${source:r}.frame
	endif
    endif

    #	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 "creating" ${destination}
    endif
    #	unlock the destination, if someone has it in maker already.
    #	fmbatch fails, otherwise.
    if ( -e ${destination}.lck ) then
	echo "${my_name}: unlocking ${destination}"
	set lock
	mv ${destination}.lck ${destination}.lck.$$
    endif
    #	copy from source to destination changing format to Frame format
    echo \
	"Open ${source} \
	 SaveAs ${dest_type} ${source} ${destination} \
	 Quit" \
    | ${fmbatch}
    #	replace lock, if any
    if ( $?lock ) then
	mv ${destination}.lck.$$ ${destination}.lck
	echo "${my_name}: revert the maker that has ${destination} open\!"
    endif
end
