#!/bin/sh

find_target(){
  myexe="$1"
  dir=`dirname $myexe`

  target=`ls -l $myexe | awk '{loc=match($0,"->");target=substr($0,loc+3);print target}'`
  target_strip_slash=`echo $target | sed -e 's=^/=='`

  if [ x"$target_strip_slash" = x"$target" ]; then
  target="$dir/$target"
  fi
  
  if [ -h "$target" ]; then
    echo `find_target "$target" "$dir"`
  else
    echo $target  
  fi
}

if [ -h "$0" ]; then
  target=`find_target "$0"`
else
  target="$0"
fi

mydir=`dirname $target`

mydir_strip_dot=`echo $mydir | sed -e 's/^\.//'`
mydir_strip_dotdot=`echo $mydir | sed -e 's/^\.\.//'`

MGROOT="$mydir/.."

if [ x"$mydir" = x"." ]; then
  MGROOT="$PWD/.."
fi

if [ x"$mydir" != x"$mydir_strip_dot" ]; then
  MGROOT="$PWD/$mydir/.."
fi

if [ x"$mydir" != x"$mydir_strip_dotdot" ]; then
  MGROOT="$PWD/$mydir/.."
fi

if [ "${MGROOT#/}" = "${MGROOT}" ]; then
  #echo "This is a relative path, so let us make it absolute."
  MGROOT="`pwd`/$MGROOT"
fi

#echo $MGROOT

if [ x"${#*}" = x"0" ]; then
echo "Usage: matrix2axis \"filename\""
echo "where filename is the name of a file containing a 3x3 or 4x4 matrix"
echo "with values separated by whitespace or commas."
exit
fi

if [ -r "${MGROOT}/QtMG" ]; then
  export PYTHONPATH=${MGROOT}/lib
  export DYLD_FALLBACK_LIBRARY_PATH=${MGROOT}/lib
  PYTHON="${MGROOT}/QtMG"
else
  export PYTHONPATH=${MGROOT}/lib
  export LD_LIBRARY_PATH=${MGROOT}/lib
  PYTHON=python
fi

${PYTHON} - $1 <<EOF
import sys

inputFile = sys.argv[1]

try:
  f = open(inputFile)
except:
  print "Failed to open", inputFile
  sys.exit()

lines = f.readlines()
if len(lines) != 3 and len(lines) !=4:
  print "Error: File must contain 3 lines of 3 values or 4 lines of 4 values."
  print "NB: no blank lines"
  f.close()
  sys.exit()
f.close()

nlines = len(lines)

have_whitespace = 0
have_commas = 0
for l in lines:
  try:
     vals = l.split(',')
     if len(vals) == 4 or len(vals) == 5:
       have_commas = have_commas + 1
     else:
       vals = l.split()
       if len(vals) == 4 or len(vals) == 5:
         have_whitespace = have_whitespace + 1
  except:
     print "Error parsing file:",lines
     sys.exit()

if have_whitespace != nlines and have_commas != nlines:
   print "Error parsing file:",lines
   sys.exit()

matvals = []
try:
  if have_commas == nlines:
    for l in lines:
      vals = l.split(',')
      for val in vals[:nlines]:
        rval = float(val.strip())
        matvals.append(rval)
  else:
    for l in lines:
      vals = l.split()
      for val in vals[:nlines]:
        rval = float(val.strip())
        matvals.append(rval)
except:
     print "Error parsing file:",lines
     sys.exit()

if len(matvals) != nlines*nlines:
     print "Error parsing file:",lines
     sys.exit()

import pygl_coord
mat = pygl_coord.matrix(nlines,nlines,matvals)

print "Input matrix"
mat.Print()
print
axisPoint = pygl_coord.GetAxisAndPointFromMatrix(mat)
success =  axisPoint.CheckIsValid()
if success:
  print "Discovery of axis was successful"
  print "Axis direction:",;axisPoint.axis().Print(); print
  print "Two point on the axis:" 
  p1 = axisPoint.point()
  p2 = axisPoint.point() + axisPoint.axis()
  p1.Print(); print
  p2.Print(); print
  
else:
  print "Failed to find rotation axis"
EOF
