#!/bin/sh

pgname=`basename $0`

uname=`uname -m`
if [ `expr "$uname" : "IP*"` -eq 0 ]; then
	echo "$pgname: You can only run this script on a SGI machine."
	exit 1
fi

if [ $# != 0 ]; then
	echo "$pgname: A script to integate FrameMaker into SGI Workspace."
	exit 1
fi

if [ ! -d /usr/lib/filetype ]; then
	echo "$pgname: /usr/lib/filetype directory is not exist."
	exit 1
fi

if [ -x /usr/bin/id ]; then
	if [ `/usr/bin/id | cut -d "=" -f2 | cut -d "(" -f1` -ne 0 ]; then
		echo "$pgname: Only root can run $pgname."
		exit 1
	fi
else
if [ -x /bin/who ]; then
	if [ "`/bin/who am i | cut -d ' ' -f1`" != "root" ]; then
		echo "$pgname: Only root can run $pgname."
		exit 1
	fi
fi
fi

FTDIR=/usr/lib/filetype

if [ "$FMHOME" = "" ]; then
        echo "$pgname: \$FMHOME is not set!!"
        echo " Please enter the path of FrameMaker installed directory: " 
        read FMHOME
	if [ ! -f $FMHOME/fminit/samplehome/sgidt/frame.ftr ]; then
		echo "$pgname: Path is not correct!!"
		exit 1
	fi
fi

if [ ! -f $FMHOME/fminit/samplehome/sgidt/frame.ftr ]; then
        echo "$pgname: Cannot find frame.ftr in $FMHOME/fminit/samplehome/sgidt"
	exit 1
fi

echo "Setting up Workspace environment for FrameMaker..." 
if [ -f $FTDIR/install/frame.ftr ]; then
        echo "$pgname: Saving the old frame.ftr as frame.ftr.old..."
		mv $FTDIR/install/frame.ftr $FTDIR/install/frame.ftr.old
fi

if [ -f $FTDIR/iconlib/fm.open.fti ]; then
        echo "$pgname: Saving the old fm.open.fti as fm.open.fti.old..."
		mv $FTDIR/iconlib/fm.open.fti $FTDIR/iconlib/fm.open.fti.old
fi

if [ -f $FTDIR/iconlib/fm.closed.fti ]; then
        echo "$pgname: Saving the old fm.closed.fti as fm.closed.fti.old..."
		mv $FTDIR/iconlib/fm.closed.fti $FTDIR/iconlib/fm.closed.fti.old
fi

if [ -f $FTDIR/iconlib/fm.icon.fti ]; then
        echo "$pgname: Saving the old fm.icon.fti as fm.icon.fti.old..."
		mv $FTDIR/iconlib/fm.icon.fti $FTDIR/iconlib/fm.icon.fti.old
fi

if [ -f $FTDIR/iconlib/fm.maker.fti ]; then
        echo "$pgname: Saving the old fm.maker.fti as fm.maker.fti.old..."
		mv $FTDIR/iconlib/fm.maker.fti $FTDIR/iconlib/fm.maker.fti.old
fi

if [ -f $FTDIR/iconlib/fm.builder.fti ]; then
        echo "$pgname: Saving the old fm.builder.fti as fm.builder.fti.old..."
		mv $FTDIR/iconlib/fm.builder.fti $FTDIR/iconlib/fm.builder.fti.old
fi

if [ -f $FTDIR/iconlib/fm.viewer.fti ]; then
        echo "$pgname: Saving the old fm.viewer.fti as fm.viewer.fti.old..."
		mv $FTDIR/iconlib/fm.viewer.fti $FTDIR/iconlib/fm.viewer.fti.old
fi

echo "Copying frame.ftr to /usr/lib/filetype/install..."
cp $FMHOME/fminit/samplehome/sgidt/frame.ftr $FTDIR/install
cp $FMHOME/fminit/samplehome/sgidt/fm.*.fti $FTDIR/iconlib
echo "Making workspace.ctr. Please wait..."
cd $FTDIR
/bin/make 1> /dev/null
echo ""
echo "Done.  Remember to set FMHOME to take effect."
exit 0
