#!/bin/bash

if (( $# < 1 )); then
  echo "Usage: $0 inputFolder [outputFolder] [CAL3DHOME]"
  exit 1
fi

inputFolder=$1

##default values
outputFolder=$inputFolder
calHome=$CAL3DHOME

##replace default values with user-defined, if any values are provided
if (( $# > 1 )); then
  outputFolder=$2
fi

if (( $# > 2 )); then
  calHome=$3
fi

testFile="$outputFolder/test.cal3d"	#used to generate a cal3d file that the user can then tweak if they need to
modelFolder="modelfiles"	#used to keep .cal3d in the main outputDir and the converted files of the model in this dir so that they can be easily moved for use with the viewer

#Change those two file names to point to your own header/footer for cal3d if you are not using the ones provided with this script!
headerFile="header.cal3d"
footerFile="footer.cal3d"

mkdir -p $outputFolder/$modelFolder	#create folder for converted files - but only if it doesn't exist already
rm $testFile				#remove .cal3d file if there is an old one present
cp $headerFile $testFile		#start the new .cal3d file using the header file

echo "Starting to process files from $inputFolder into $outputFolder with cal3d at $calHome"

#processing skeleton files
for filename in $inputFolder/*.xsf; do
  echo "Processing $filename..."
  path=${filename%/*}
  file=${filename##*/}
  $calHome/cal3d_converter $filename $outputFolder/$modelFolder/${file%.xsf}.csf
  echo "  <skeleton file=\"${file%.xsf}.csf\" />" >> $testFile
  echo "done"
done

#processing material files
for filename in $inputFolder/*.xrf; do
  echo "Processing $filename..."
  path=${filename%/*}
  file=${filename##*/}
  $calHome/cal3d_converter $filename $outputFolder/$modelFolder/${file%.xrf}.crf
  echo "done"
done

#processing mesh files
for filename in $inputFolder/*.xmf; do
  echo "Processing $filename..."
  path=${filename%/*}
  file=${filename##*/}
  $calHome/cal3d_converter $filename $outputFolder/$modelFolder/${file%.xmf}.cmf

  #writing material tags to .cal3d
  echo "  <mesh file=\"${file%.xmf}.cmf\"" >> $testFile
  echo "    name=\"${file%.xmf}\"" >> $testFile
  echo "    material=\"green\" />" >> $testFile	#so far script knows of nothing else; one might want to change this!

  #let the user know
  echo "done"
done

#processing animation files
for filename in $inputFolder/*.xaf; do
  echo "Processing $filename..."
  path=${filename%/*}
  file=${filename##*/}
  animName=${file##*_}
  animName=${animName%\.xaf}
  echo "animName is $animName"
  $calHome/cal3d_converter $filename $outputFolder/$modelFolder/${file%.xaf}.caf

#write to the .cal3d file
  echo "  <animation" >> $testFile
  echo "    file=\"${file%.xaf}.caf\"" >> $testFile
  echo "    name=\"$animName\"" >> $testFile
  echo "    type=\"$animName\"" >> $testFile
  echo "    base_vel=\"3\"" >> $testFile
  echo "    min_vel=\"0\"" >> $testFile
  echo "    max_vel=\"50\"" >> $testFile
  echo "    min_random=\"10\"" >> $testFile
  echo "    max_random=\"30\" />" >> $testFile

#let the user know this is done
  echo "done"
done


#append footer to .cal3d file
cat $footerFile >> $testFile
