#!/bin/sh
#Tag 1536

USAGE="rtg inputfile"

cd $rtg_data

rtg_pst="$games_loc/pst"
export rtg_pst

#
# Do some sanity checks, see if we can write to this file
#

rcfile="$rtg_data/rtg_params.rc"
echo test >$rcfile

if [ ! -w $rcfile ]
then
  rcfile="/usr/tmp/rtg_params.rc"
  echo test >$rcfile
  if [ ! -w $rcfile ]
  then
    aliasWarning -b "OK" -t "Error" -s "Could not make writable $rcfile"
    exit 
  fi
 rtg_data="/usr/tmp"
fi

# Can we write to the pst file

pstfile="$rtg_data/rtg.pst"
if [ ! -w $pstfile ]
then
  cp $rtg_pst/rtg.pst-defaults  $pstfile
  chmod +w $pstfile
  if [ ! -w $pstfile ]
  then
    aliasWarning -b "OK" -t "Error" -s "Could not find writable $pstfile"
    exit
  fi
fi

if test ! "$1"
then
	aliasWarning -b "OK" -t "Error" -s "Could not find input file $INFILE"
	exit 1
fi

INFILE=$1

if [ ! -w $INFILE ]
	then
	aliasWarning -b "OK" -t "Error" -s "Could not find input file $INFILE"
	exit 1 
fi

#
# Launch the rtg dialog box
#
$ALIAS_LOCATION/bin/pst -geometry 435x797+300+150 $pstfile

#
# Check for cancel
#
if [ $? != "0" ]
	then
	exit 1
fi

version=`pstget $pstfile version`

if [ $version = "1" ]
	then
	rtg_converter="v7.0.1_rtg_output_v1.75"
else
	rtg_converter="v7_rtg_output_v1.75"
fi

#
# If you are looking at this then the next line is here for
# general ease of forcing this.  For V7.5 we will only have 1
# version of the rtg_output file.  If the user changes the
# code and makes their own, then will need to update this script
# anyways. So we'll force him to do that.
#

rtg_converter="rtg_output"

echo "... converter is $rtg_converter"

echo "outpath `pstget $pstfile outpath`" > $rcfile

echo "outfile `pstget $pstfile outfile`" >> $rcfile
echo "geomMode `pstget $pstfile geomMode`" >> $rcfile
echo "hxType `pstget $pstfile hxType`" >> $rcfile
echo "outVNorm `pstget $pstfile outVNorm`" >> $rcfile
echo "outTCoord `pstget $pstfile outTCoord`" >> $rcfile
echo "outVColor `pstget $pstfile outVColor`" >> $rcfile
echo "outPNorm `pstget $pstfile outPNorm`" >> $rcfile
echo "optVandN `pstget $pstfile optVandN`" >> $rcfile
echo "vertOrient `pstget $pstfile vertOrient`" >> $rcfile
echo "q2triFlag `pstget $pstfile q2triFlag`" >> $rcfile
echo "scaleMult `pstget $pstfile scaleMult`" >> $rcfile
echo "texgenAllFrms `pstget $pstfile texgenAllFrms`" >> $rcfile
echo "texOutput `pstget $pstfile texOutput`" >> $rcfile
echo "texResX `pstget $pstfile texResX`" >> $rcfile
echo "texResY `pstget $pstfile texResY`" >> $rcfile
echo "texMode `pstget $pstfile texMode`" >> $rcfile
echo "encTexRes `pstget $pstfile encTexRes`" >> $rcfile
echo "lightFlag `pstget $pstfile lightFlag`" >> $rcfile
echo "objLevel `pstget $pstfile objLevel`" >> $rcfile
echo "showIndexC `pstget $pstfile showIndexC`" >> $rcfile
echo "newline `pstget $pstfile newline`" >> $rcfile
echo "fbackLvl `pstget $pstfile fbackLvl`" >> $rcfile
echo "showParams `pstget $pstfile showParams`" >> $rcfile
echo "startFrm `pstget $pstfile startFrm`" >> $rcfile
echo "endFrm `pstget $pstfile endFrm`" >> $rcfile
echo "byFrm `pstget $pstfile byFrm`" >> $rcfile

precision=`pstget $pstfile precision`

echo "pprec $precision" >> $rcfile
echo "piconv 1.0" >> $rcfile
echo "nprec $precision" >> $rcfile
echo "niconv 1.0" >> $rcfile
echo "tprec $precision" >> $rcfile
echo "ticonv 1.0" >> $rcfile
echo "hprec $precision" >> $rcfile
echo "hiconv 1.0" >> $rcfile

#
# Prompt for output file and run
#

echo Converting ...
echo
$rtg_bin/$rtg_converter $INFILE $rcfile

echo
date
echo DONE.
sleep 3600
