#!/bin/sh
#
#	Install script for LightWave 5.x
#
#	Copyright 1997, GW Hannaway & Associates
#
PATH=/bin:/usr/bin:/usr/sbin:/etc export PATH
DEST=/usr/lightwave5.0
OSVERS=`uname -r`
WHOAMI=`whoami`

#
# die -- print a message and exit
#
# usage: die exitval "message"
#
die()
{
	echo ""
	echo "ERROR: $2"
	echo ""
	echo The installation has NOT completed successfully.
	echo You must run the INSTALL script before using LightWave.
	echo ""
	exit $1
}

#
# warn -- print a warning message
#
# usage: warn "message"
#
warn()
{
	echo "WARNING: $2"
}

# catch hangup, control-C, etc. and issue an error
trap 'die 1 "Installation Interrupted!"' 1 2 3 15

#
# yorn -- ask a "yes or no" question until we get an answer
#
# usage: yorn "prompt"
#
yorn()
{
	while echo "$* (y/n) \c" >&2
	do
		read a b c d e f g
		case "$a" in
		[yY]*)
			return 0
			;;
		[nN]*)
			return 1
			;;
		*)
			echo "Please answer 'y' or 'n'" >&2
			;;
		esac
	done
}

#
# cptree -- copy a directory tree, preserving permissions, dates, and symlinks
#
# usage: cptree sourcedir destdir
#
# destdir is created if it doesn't already exist.  if sourcedir and destdir
# are the same, we don't bother to copy anything.
#
cptree()
{
	# this routine can be dangerous if not called with correct args...
	[ "$1" = "" ] && die 100 "cptree: missing sourcedir"
	[ -d "$1" ] || die 110 "cptree: $1: Not a directory"
	[ "$2" = "" ] && die 120 "cptree: missing destdir"
	[ -d "$2" ] || mkdir "$2" || die 130 "cptree: can't create $2"

	# see if source and dest are the same dir
	sourcedir=`(cd "$1"; pwd)`
	destdir=`(cd "$2"; pwd)`
	[ "$sourcedir" = "$destdir" ] && return
	# do the copy
	(cd $1; tar cf - .) | (cd $2; tar xpBf -)
}

# make sure the lightwave files are here.  if not, try the path used
# to run the INSTALL script.  if we still can't find the files, die.
[ ! -d workspace -o ! -d bin -o ! -d plugins ] && cd `dirname "$0"`
[ ! -d workspace -o ! -d bin -o ! -d plugins ] &&\
	die 140 "Can't find LightWave files.  Run INSTALL from its directory."

# make sure we're running as root
[ "$WHOAMI" = "root" ] || die 150 "You must run INSTALL as root."

# prompt user for destination and install lightwave files
if [ `pwd` != "$DEST" ]
then
	echo Hit Return to install LightWave in $DEST
	echo or enter a different destination directory:" \c"
	read newdest
	[ "$newdest" != "" ] && DEST="$newdest"
fi

# copy the files into the destination directory
echo Installing LightWave files...
cptree . $DEST

# setup some uniform permissions
chown -R 0 $DEST || warn "Can't change owner of LightWave files."
chgrp -R 0 $DEST || warn "Can't change group owner of LightWave files."

# link executables into /usr/bin
echo Installing Commands in /usr/bin...
cd $DEST/bin
for cmd in *
do
	chmod 755 $cmd
	if [ -f /usr/bin/$cmd ]
	then
		rm /usr/bin/$cmd ||\
		die 160 "Can't remove old cmd /usr/bin/$cmd"
	fi
	# commands lw3d and lwm are special cases
	if [ "$cmd" = "lw3d" ]
	then
		touch /usr/bin/lw3d || die 170 "Can't create /usr/bin/lw3d"
		cat > /usr/bin/lw3d << EOF
#!/bin/sh
LIGHTWAVE="$DEST" export LIGHTWAVE
PATH="\$PATH:\$LIGHTWAVE/bin" export PATH
cd \$HOME
exec \$LIGHTWAVE/bin/lw3d \$*
EOF
		chmod 755 /usr/bin/lw3d || die 180 "Can't chmod /usr/bin/lw3d"
	elif [ "$cmd" = "lwm" ]
	then
		touch /usr/bin/lwm || die 190 "Can't create /usr/bin/lwm"
		cat > /usr/bin/lwm << EOF
#!/bin/sh
LIGHTWAVE="$DEST" export LIGHTWAVE
PATH="\$PATH:\$LIGHTWAVE/bin" export PATH
cd \$HOME
exec \$LIGHTWAVE/bin/lwm \$*
EOF
		chmod 755 /usr/bin/lwm || die 200 "Can't chmod /usr/bin/lwm"
	else
		ln -s `pwd`/$cmd /usr/bin/$cmd ||\
			die 210 "Can't create /usr/bin/$cmd"
	fi
	echo /usr/bin/$cmd installed.
done

# setup icons
cd $DEST
case "$OSVERS" in
	3.*)
		;;
	*)
		cp workspace/icons/*.icon /usr/lib/images ||\
			warn "Can't install Icons in /usr/lib/images"
		;;
esac

# setup workspace
[ -d /usr/lib/filetype/install ] ||\
	mkdir /usr/lib/filetype/install ||\
	die 220 "Can't create directory /usr/lib/filetype/install/iconlib"
[ -d /usr/lib/filetype/install/iconlib ] ||\
	mkdir /usr/lib/filetype/install/iconlib ||\
	die 230 "Can't create directory /usr/lib/filetype/install/iconlib"
cp workspace/icons/*.fti /usr/lib/filetype/install/iconlib ||\
	die 240 "Can't install fti files in /usr/lib/filetype/install/iconlib"
# not sure if this is still needed, so protect with -f test
[ workspace/lightwave.ftr ] &&\
	cp workspace/lightwave.ftr /usr/lib/filetype/install
if [ -x /usr/sbin/iconbookedit ]
then
	iconbookedit -add "Category:File Name:/usr/bin/lw3d" -syspage Applications
	iconbookedit -add "Category:File Name:/usr/bin/lwm" -syspage Applications
	if [ $? = 0 ]
	then
		iconbookedit -add "Category:File Name:/usr/bin/lw3d" /usr/lib/desktop/iconbook/syspages/Applications > /dev/null 2>&1
		iconbookedit -add "Category:File Name:/usr/bin/lwm" /usr/lib/desktop/iconbook/syspages/Applications > /dev/null 2>&1
	fi
fi

echo Installing PlugIns...
[ -d /usr/local ] || mkdir /usr/local || die 250 "Can't create /usr/local"
cd $DEST
cp -r plugins/hiip/* /usr/local || die 260 "Can't install plugins."

cd $DEST
# Make the Layout Config File
# the lines between the "cat" line and the "EOF" line below
# contain the default .lwrc given to each user if one doesn't
# already exist in the user's home directory.
touch .lwrc || die 270 "Can't create $DEST/.lwrc"
chmod 777 .lwrc
cat > .lwrc << EOF
LWCO 
0 
ContentDirectory $DEST 
ScenesDirectory scenes 
ObjectsDirectory objects 
HierarchiesDirectory scenes 
SurfacesDirectory surfaces 
ImagesDirectory images 
OutputDirectory images 
AnimationsDirectory images 
MotionsDirectory motions 
EnvelopesDirectory envelopes 
PreviewsDirectory previews 
CommandDirectory  
StatusFilename (none) 
DefaultFPS 30.000000 
DefaultTension 0.000000 
DefaultPixelAspect 2 
DefaultSegmentMemory 2200000 
DefaultZoomFactor 3.200000 
DefaultOverlay 0 
FrameEndBeep 0 
RenderDisplayDevice 1 
OutputFilenameFormat 1 
SerialPortName /dev/ttyd1 
RecordSetup1 (none) 
RecordSetup2 (none) 
RecordCommand (none) 
RecordDelay 0.000000 
FirstFrameDelay 0.000000 
DefaultLayoutGrid 8 
QuickMotionPathFrames 30 
RenderProgress 1 
AutoKeyAdjust 0 
ScreamerNetCPUs 8 
OverwriteWarnings 2 
Plugin ObjectLoader Translator3D_Import plugins/t3dload.p Translator3D_Import 
Plugin ImageLoader HIIPLoader plugins/hiipload.p HIIPLoader 
Plugin ImageSaver Alias(.als) plugins/hiipsave.p Alias(.als) 
Plugin ImageSaver BMP(.bmp) plugins/hiipsave.p BMP(.bmp) 
Plugin ImageSaver Cineon(.cin) plugins/hiipsave.p Cineon(.cin) 
Plugin ImageSaver IFF(.iff) plugins/hiipsave.p IFF(.iff) 
Plugin ImageSaver JPEG(.jpg) plugins/hiipsave.p JPEG(.jpg) 
Plugin ImageSaver PCX(.pcx) plugins/hiipsave.p PCX(.pcx) 
Plugin ImageSaver Pict(.pic) plugins/hiipsave.p Pict(.pic) 
Plugin ImageSaver Pixar(.pxr) plugins/hiipsave.p Pixar(.pxr) 
Plugin ImageSaver Rendition(.6rn) plugins/hiipsave.p Rendition(.6rn) 
Plugin ImageSaver SGI(.rgb) plugins/hiipsave.p SGI(.rgb) 
Plugin ImageSaver SoftImage(.pic) plugins/hiipsave.p SoftImage(.pic) 
Plugin ImageSaver Sun(.sun) plugins/hiipsave.p Sun(.sun) 
Plugin ImageSaver TDI(.tdi) plugins/hiipsave.p TDI(.tdi) 
Plugin ImageSaver TIFF(.tif) plugins/hiipsave.p TIFF(.tif) 
Plugin ImageSaver Targa32(.tga) plugins/hiipsave.p Targa32(.tga) 
Plugin ImageSaver Toaster(.vti) plugins/hiipsave.p Toaster(.vti) 
Plugin ImageSaver Wavefront(.rla) plugins/hiipsave.p Wavefront(.rla) 
Plugin ImageSaver XWIndows(.xwd) plugins/hiipsave.p XWIndows(.xwd) 
Plugin ImageSaver YUV(.yuv) plugins/hiipsave.p YUV(.yuv) 
Plugin ObjReplacementHandler ObjectSequence plugins/objrep.p ObjectSequence 
Plugin ShaderHandler ColorCube plugins/shadfilt.p ColorCube 
Plugin ShaderHandler HSLColorCube plugins/shadfilt.p HSLColorCube 
Plugin ImageFilterHandler Emboss plugins/shadfilt.p Emboss 
Plugin ImageFilterHandler EmbossBW plugins/shadfilt.p EmbossBW 
Plugin ImageFilterHandler Gamma plugins/shadfilt.p Gamma 
Plugin ImageFilterHandler Negative plugins/shadfilt.p Negative 
Plugin ImageFilterHandler NightVision plugins/vidfilt.p NightVision 
Plugin ImageFilterHandler Sepia plugins/shadfilt.p Sepia 
Plugin ImageFilterHandler VidNoise plugins/vidfilt.p VidNoise 
Plugin ImageFilterHandler Vignette plugins/shadfilt.p Vignette 
Plugin ImageFilterHandler NTSC_Legal plugins/hotvideo.p NTSC_Legal 
Plugin ImageFilterHandler WaterMark plugins/watermrk.p FGAlpha Watermark 
Plugin AnimSaverHandler SGIMovie plugins/hiipsave.p SGIMovie 
EOF

# Make Modeler config file
# the lines between the "cat" line and the "EOF" line below
# contain the default .lwmrc given to each user if one doesn't
# already exist in the user's home directory.
touch .lwmrc || die 280 "Can't create $DEST/.lwmrc"
chmod 777 .lwmrc
cat > .lwmrc << EOF
MDOP 1 
 
FlatnessLimit 0.500000 
QuadTriMode 1 
TwoSided 0 
CurveDivision 1 
UnitSystem 0 
UndoLevels 3 
GridOptions 3 1 0.001000 
ObjectsDirectory $DEST/objects 
FontsDirectory $DEST/psfonts 
MotionsDirectory $DEST/motions 
MacrosDirectory . 
MacroListsDirectory  
MacroList  
StartupCommand  
ColorInterface 1 
ScreenModeID 0 
FileRequester \$Default
FileType Objects * 
FileType Images * 
FileType Motions * 
FileType PSFonts * 
FileType Plugins *.p 
FileType Macros * 
FileType SCLists * 
PluginDirectory $DEST/plugins 
Plugin CommandSequence BoundingBox center.p Bounding Box 
Plugin CommandSequence Calculate mathplot.p Calculate 
Plugin CommandSequence Center center.p Center 
Plugin CommandSequence Center1D center.p Center 1D 
Plugin CommandSequence CenterScale center.p Centered Scale 
Plugin CommandSequence CutCurves prims.p CutCurves 
Plugin CommandSequence Demo_AllBGLayers layerset.p All BG Layers 
Plugin CommandSequence Demo_NextEmptyLayer layerset.p Next Empty Layer 
Plugin CommandSequence Envelopes motions.p Envelopes 
Plugin CommandSequence Julienne prims.p Julienne 
Plugin CommandSequence LightSwarm motions.p LightSwarm 
Plugin CommandSequence LoadFonts text.p Load Fonts 
Plugin CommandSequence MathMotion motions.p Math Motion 
Plugin CommandSequence PathToMotion motions.p Path To Motion 
Plugin CommandSequence Plot1D mathplot.p Plot 1D Function 
Plugin CommandSequence Plot2D mathplot.p Plot 2D Surface 
Plugin CommandSequence PointCenter center.p Center-of-Mass 
Plugin CommandSequence PolyEdgeShaper text.p Router 
Plugin CommandSequence Primitives prims.p Primitives 
Plugin CommandSequence RandPricks prims.p RandPricks 
Plugin CommandSequence RotateAnyAxis spinor.p Rotate Arbitrary Axis 
Plugin CommandSequence RotateAxisAlign spinor.p Rotate Axis Align 
Plugin CommandSequence RotateHPB spinor.p H,P,B Rotate 
Plugin CommandSequence Scene2VRML lw_vrml.p Scene To VRML 
Plugin CommandSequence Stipple prims.p Stipple 
Plugin CommandSequence Symmetrize center.p Symmetrize 
Plugin CommandSequence TextCompose text.p Compose Text 
Plugin CommandSequence TextCurve text.p Curved Text 
Plugin CommandSequence Throw motions.p Throw 
Plugin CommandSequence VRMLAutoSave lw_vrml.p VRML List Save 
Plugin CommandSequence VRMLSave lw_vrml.p Save as VRML 
Plugin MeshDataEdit BGConform spherize.p Conform to BG Object 
Plugin MeshDataEdit Demo_MakeSpikey spikey.p Spikey Subdivide 
Plugin MeshDataEdit Points2Polys prims.p Points2Polys 
Plugin MeshDataEdit Spherize spherize.p Conform to Unit Sphere 
Plugin MeshDataEdit Squarize spherize.p Conform to Unit Square 
Plugin MeshDataEdit Translator3D_Export t3dsave.p Translator3D_Export
Plugin ObjectLoader Translator3D_Import plugins/t3dload.p Translator3D_Import
Plugin ObjectLoader Demo_3DG1 vsformat.p Demo_3DG1 
Plugin ImageLoader HIIPLoader hiipload.p HIIPLoader 
Plugin Global ExpressEval equation.p ExpressEval
EOF

echo ""
echo "LightWave Installation Finished."
echo ""
