#! /bin/csh -f
# 
# cover script for the 'fmbib' program, which generates
# FrameMaker Bibliography documents.
# When run with the '-b' (batch-mode) switch, this script
# assumes that the FrameMaker Documents are in binary format
# and attempts to pre/post process them with the 'fmbatch'
# program to translate them to/from MIF format for use w/ the
# fmbib executable. If this switch is NOT specified, it is
# assumed that the Maker files are already in MIF format.
# 
# cm, 11 feb 91
#
# ask, 2-26-91  Add "'s around $arg in if statements within switch that
#	processes arguments to make it work.
# ask, 2-26-91 Save status after invokations of fmbatch and fmbib (3 places)
#	and display it if error.
# ask, 2-27-91  Fix re-create documents loop so files are saved under original
#	file names, rather than "doc".
# ask, 2-27-91  Fix cases -m -o -s and -r so correct argument is appended to
#	the command line after the option.

####### Configure the following variables to your installation of fmbib #####

# set the following line to where the executable will be installed.
set BIBPROG     = /usr/misc/.frame/bin/fmbib

# set the following line to the directory containing the 
# .style and .tmpl files.  
set STYLE_DIR	= /usr/misc/.frame/contrib/misc/fmbib

#################### End of system configuration variables ##################

set TMPBATCH    = /tmp/fmbib.$$
set FMBATCH     = fmbatch
set CMD         = "$BIBPROG "
set DOCS        = ""
set MIFDOCS     = ""
set BATCH 	= 0
if ( -e ~/fmconsole.log ) then
	set LOG = ~/fmconsole.log
else
	set LOG = /dev/null
endif

# create new command line for fmbib program with filenames of 
# the soon-to-be-created MIF files.  This script assumes that
# all Maker files are in Binary format, and uses the 'fmbatch'
# utility to create their MIF versions.  The '-n' switch indicates
# that the input files are already in MIF form.

@ n = 1
while ( $n <= $#argv )
        set arg = $argv[$n]
        switch ( $arg )
                case -b:        
                        set BATCH = 1
						breaksw

                case -h*:
                case -v*:
				case -c*:
                        set CMD = "$CMD $arg"
                        breaksw
        
                case -m*:
                        set CMD = "$CMD $arg"
                        if( "$arg" == '-m' ) then
                                @ n++
                                set CMD = "$CMD $argv[$n]"
                        endif
                        breaksw

                case -o*:
                        set CMD = "$CMD $arg"
                        if( "$arg" == '-o' ) then
                                @ n++
                                set CMD = "$CMD $argv[$n]"
                        endif
                        breaksw

                case -s*:
                        set CMD = "$CMD $arg"
                        if( "$arg" == '-s' ) then
                                @ n++
                                set CMD = "$CMD $argv[$n]"
                        endif
                        breaksw

                case -r*:
                        if( "$arg" == '-r' ) then
                                @ n++
								set STYLE_DIR = $argv[$n]
                        endif
                        breaksw

                case -*:        # unknown switch
                        eval $BIBPROG -h
                        exit 1

                default:        # filenames hopefully
			if ( ! -e $arg ) then
				echo Cannot find: $arg 
				exit 1
			endif 
			set DOCS = ($DOCS $arg)
                        set MIFDOCS = ($MIFDOCS ${arg}.mif )
                        breaksw
        endsw
        @ n++
end


set CMD = "$CMD -r $STYLE_DIR"

if ( "$#DOCS" == 0 ) then
	eval $BIBPROG -h
	exit 1
endif

if( $BATCH == 0 ) then	# Assume FrameMaker MIF format
	set CMD = "$CMD $DOCS"
	goto doit
else
	set CMD = "$CMD $MIFDOCS"
endif

# create batch file of instructions for fmbatch

echo >$TMPBATCH "# fmbib: creating MIF files"
foreach doc ($DOCS)
        echo >>$TMPBATCH "Open $doc"
        echo >>$TMPBATCH "SaveAs m $doc $doc.mif"
        echo >>$TMPBATCH "Quit $doc"
        echo >>$TMPBATCH "#"
end
echo >>$TMPBATCH "Quit"

# create the MIF files.
echo -n Using fmbatch to create MIF files from Frame Docs...
eval $FMBATCH $TMPBATCH >>& $LOG

set STATUS = $status
if ( $STATUS ) then
	echo $STATUS
        echo "fmbib: fmbatch failed to create MIF files."
        set exval = 1
        goto done
else
	echo done.
endif

	
# run the fmbib program on the MIF files.
doit:
if ( $BATCH ) echo -n Generating the Bibliography...
eval $CMD
if ( $BATCH == 0 ) exit $status

set STATUS = $status
if ( $STATUS ) then
	echo $STATUS
        echo "fmbib exited w/ errors, cleaning up."
        set exval = 1
        goto done
endif
echo Done.

# now run fmbatch in reverse, to create the new Maker binaries
# from the MIF files.
echo >$TMPBATCH "# fmbib: re-creating Maker documents"
foreach doc ($DOCS)
        echo >>$TMPBATCH "Open ${doc}.mif"
        echo >>$TMPBATCH "SaveAs d ${doc}.mif ${doc}"
        echo >>$TMPBATCH "Quit ${doc}.mif"
        echo >>$TMPBATCH "#"
end
echo >>$TMPBATCH "Quit"

# un-create the MIF files.
echo -n Using fmbatch to re-create Frame Docs...
eval $FMBATCH $TMPBATCH >>& $LOG

set STATUS = $status
if ( $STATUS ) then
	echo $STATUS
        echo "fmbib: fmbatch failed to re-create Maker documents."
        echo "the documents have been saved in MIF format as:"
        foreach doc ($MIFDOCS)
                echo "  $doc"
        end
        rm -f $TMPBATCH
        exit 1
else 
	echo Done.
endif

set exval = 0

done:
        rm -f $TMPBATCH
        foreach doc ($MIFDOCS)
                rm -f $doc
        end
        exit $exval

