#! /bin/csh -f
#
# Script to extract document names from a FrameMaker book file for fmbib.
#
# 1.  Use fmbatch to save book file in MIF.
# 2.  Scan MIF file for document names.
#	- use full Unix path names
#	- ignore derived documents
# 3.  Use fmbib to create bib file and update document files.
# 4.  If Bib file is not listed in the book file, 
#   a.	add it after last non-derived document,
#   b.	use fmbatch to save book file in Frame binary format
#
#     !! Can't do steps #1 and 4.  (Hurumph!)
#     -- you must create the MIF file yourself,
#        and you must be sure the bib file is included in the book file.
#
# Adapted from fmbib.csh (cm 2-11-91) ask, 2-27-91
#
set TMPBATCH    = /tmp/fmbib.$$
set FMBATCH     = fmbatch
set BIBPROG     = /usr/projects/CF/Frame/FMBib/fmbib.csh
set CMD         = "$BIBPROG -b -r /usr/projects/CF/Frame/FMBib "
set DOCS        = ""
set OUT         = ""
if ( -e ~/fmconsole.log ) then
	set LOG = ~/fmconsole.log
else
	set LOG = /dev/null
endif

# create new command line for fmbib script with requested options.
# Collect filenames of book files for processing.  The names of the documents
# contained in each book file will be passed to fmbib for processing.

@ n = 1
while ( $n <= $#argv )
        set arg = $argv[$n]
        switch ( $arg )
                case -r*:
                        if( "$arg" == '-r' ) then
                                @ n++
                        endif
			breaksw
                case -b:        
			breaksw

                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 -s*:
                        set CMD = "$CMD $arg"
                        if( "$arg" == '-s' ) then
                                @ n++
                                set CMD = "$CMD $argv[$n]"
                        endif
                        breaksw

                case -o*:
                        if( "$arg" == '-o' ) then
                                @ n++
                                set OUT = $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)
                        breaksw
        endsw
        @ n++
end

if ( "$#DOCS" != 1 ) then
	echo This script requires exactly one filename (a FrameMaker book file)
	exit 1
endif
set BOOK = $DOCS

if ( "$OUT" == "" ) then
	set OUT = `echo $BOOK | cut -d. -f1`.Bib.doc
endif
set CMD = ($CMD -o $OUT.mif)
echo Output file is $OUT

# # create batch file of instructions for fmbatch
# 
# echo >$TMPBATCH "# fmbibBook: creating MIF file of book file"
# echo >>$TMPBATCH "Open $BOOK"
# echo >>$TMPBATCH "SaveAs m $BOOK $BOOK.mif"
# echo >>$TMPBATCH "Quit $BOOK"
# echo >>$TMPBATCH "Quit"
# 
# # create the MIF file.
# echo -n Using fmbatch to create MIF file from Book file ...
# # eval $FMBATCH $TMPBATCH >>& $LOG
# eval $FMBATCH $TMPBATCH
# 
# set STATUS = $status
# if ( $STATUS ) then
# 	echo $STATUS
#         echo "fmbib: fmbatch failed to create MIF file."
#         set exval = 1
#         goto done
# else
# 	echo done.
# endif

echo -n Retrieving document names from book file...

# gleen document file names from the book file.
# - store awk program
echo  >$TMPBATCH.awk 'check==1    {'
echo >>$TMPBATCH.awk '                if ($1 == "<StartPageSide") {'
echo >>$TMPBATCH.awk '                    print line'
echo >>$TMPBATCH.awk '                }'
echo >>$TMPBATCH.awk '            }'
echo >>$TMPBATCH.awk ' '
echo >>$TMPBATCH.awk '            {'
echo >>$TMPBATCH.awk '                check = 0;'
echo >>$TMPBATCH.awk '            }'
echo >>$TMPBATCH.awk ' '
echo >>$TMPBATCH.awk '/FileName/  {'
echo >>$TMPBATCH.awk '                check = 1'
echo >>$TMPBATCH.awk '                line=$2'
echo >>$TMPBATCH.awk '            };'

set DOCS = `awk -f $TMPBATCH.awk $BOOK.mif | sed -e 's/.*U\\>//' -e 's/..$//' | tr '\012' ' '`
echo Done.

# run the fmbib script on the document files.
set CMD = "$CMD $DOCS"
eval $CMD

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

# Save the Bib file in FrameMaker binary format.

echo >$TMPBATCH "Open $OUT.mif"
echo >>$TMPBATCH "SaveAs d $OUT.mif $OUT"
echo >>$TMPBATCH "Quit $OUT.mif"
echo >>$TMPBATCH "Quit"

# create the binary file.
echo -n Using fmbatch to convert $OUT to binary form...
eval $FMBATCH $TMPBATCH >>& $LOG

set STATUS = $status
if ( $STATUS ) then
	echo $STATUS
        echo "fmbib: fmbatch failed to create $OUT"
	echo "Mif version is in $OUT.mif"
        set exval = 1
        goto done
else
	echo done.
endif


echo -n "Do you wish to update cross-references? (Y/N) [Y] "
set UPDATE = $<
switch ( $UPDATE )
	case N:
	case n:
	case No:
	case NO:
	case no:
	case nO:
		echo CrossReferences in each document file must be updated 
		echo before citations will match the Bibliography!
		breaksw
	default:

		# now run fmbatch to update crossreferences in each document
		echo >$TMPBATCH "# fmbibbook: Updating cross-references"
		foreach doc ($DOCS)
		        echo >>$TMPBATCH "Open ${doc}"
		        echo >>$TMPBATCH "Update ${doc}"
		        echo >>$TMPBATCH "Quit ${doc}"
		        echo >>$TMPBATCH "#"
		end
		echo >>$TMPBATCH "Quit"
		# echo >>$TMPBATCH "Open $BOOK.mif"
		# echo >>$TMPBATCH "Update $BOOK.mif"
		# echo >>$TMPBATCH "SaveAs d $BOOK.mif $BOOK"
		# echo >>$TMPBATCH "Quit $BOOK.mif"
		# echo >>$TMPBATCH "Open $BOOK"
		# echo >>$TMPBATCH "Update $BOOK"
		# echo >>$TMPBATCH "Quit $BOOK"
		# echo >>$TMPBATCH "Quit"

		# Update crossreferences in book
		echo -n Using fmbatch to update cross-references...
		eval $FMBATCH $TMPBATCH >>& $LOG
		
		set STATUS = $status
		if ( $STATUS ) then
			echo $STATUS
		        echo "fmbib: fmbatch failed to update cross-references."
		        exit 1
		else 
			echo Done.
		endif

		breaksw
        endsw

set exval = 0

done:
        rm -f $TMPBATCH
        exit $exval

