#!/bin/bash
#
# this script creates links to vtk files to allow an animation with paraview.
# the original files must be stored in the following way (OpenFoam 
# planarSmapling style)
#
# vtkdir/0.001/xyz.vtk
# vtkdir/0.002/xyz.vtk
# vtkdir/0.003/xyz.vtk
# 
# the corresponding output file links are then
#
# vtkdir/0.001_xyz.vtk
# vtkdir/0.002_xyz.vtk
# vtkdir/0.003_xyz.vtk
# 
# all files in the time directories are linked
#
#
# usage:
#
#    vtkLink  [folder]
#
# the default folder with vtk files is planarSampling
if [ -z "$1" ]; then
  echo "Syntax: `basename $0` folder"
  exit 1
fi



#if [ "$1" = "" ]
#then
#      orgdir=planarSampling
#else
orgdir=$1
#fi

orgdir=${orgdir%/*}

counter=0   # file number

for dir in `ls -d $orgdir/*/` # go through directories only
do

  dir=${dir%/*}
  echo $dir


  for file in ${dir}/*
  do
     filename=`basename $file`     # chop directory
     filename=${filename%.*}       # chop .vtk 
     number=${dir##*/}             # get number
#     echo -s `pwd`/$file ${orgdir}/${filename}-${number}.vtk
   if [ "$number" = "" ]
   then
     echo skipping file
   else
     cnttxt=$(printf %05d $counter)    # generate file number with leading zeros
     #echo $cnttxt
     numberconstlen=$(echo ${number}00000000000000000 | cut -c1-12)
     ln -s `pwd`/$file ${orgdir}/${filename}-${numberconstlen}.vtk
     counter=$((counter + 1))
   fi
  done

done

echo -- ready --
