#!/bin/bash -f
#  Caution:  print " " > file  print " " >> file
#           or print xxxx > file etc with xxx containing spaces
#           are not correctly treated by gawk. Instead we have to
#           use system function.
# 
#

if [ $# -ne 1 ]; then
    echo "Usage: splitiftoolarge convertedtracedata"
    exit;
fi



####### max data lines that can be accepted by Geomview (32767 ?)

maxsize=32000


#set ex = `ls tempvect* | wc -l | awk '{print $1}'`

#if(  $ex != "0" ) then
	rm -f tempvect*
#endif

	# remove extention
#file=$1:r
file=${1%.*}
line2=`awk 'NR==2 {print; exit}' $1`

#    number of polylines
#@ nl = $line2[1]
let nl=`echo $line2 | awk '{print $1}'`

#  get max number of vertices in a file
 
#  @ maxv = `gawk 'BEGIN{maxv=0}; NR>2 && NR<= nl+2 {if(maxv < $1) maxv=$1}; NR > nl+2 {print maxv;exit}' nl=$nl $1`

let maxv=`awk 'BEGIN{maxv=0}; NR>2 && NR<= nl+2 {if(maxv < $1) maxv=$1}; NR > nl+2 {print maxv;exit}' nl=$nl $1`

if [ $maxv -lt  $maxsize ];  then
#             size is not large; simply rename the file
  mv $1	${file}1.vect
else
#            qm is needed in split.awk
 qm='"'
 color=$qm` tail -1 $1 `$qm 

 awk  -f split.awk  maxsize=$maxsize file=$file color="$color" $1
#          remove original file
 rm -f $1
fi
