#! /bin/sh

# creating an overview table of all ies files in a directory

#if ! ls *.ies  >&/dev/null ; then exit ; fi
#          echoes on empty list, why?

if ! ls *.ies >/dev/null 2>/dev/null ; then exit ; fi 
# exiting, if there are no *.ies files

# if ! [ -a tab ] ; then mkdir tab ; fi
ies2tab -l -c -u $@ >all.tab

for F in *.ies
 do ies2tab -l -u -s $@ $F >>all.tab 
 if [ $? -eq 106 ] 
 then
# input file has comma-ended numbers, replacing commas with blanks:
  cp $F /tmp/$F
  chmod +w /tmp/$F
  nons2bla /tmp/$F 44 32 >/dev/null
  ies2tab -l -u -s $@ /tmp/$F >>all.tab
 fi
 if [ $? -ge 1 ] 
 then 
  echo "cannot process file $F - data format non-conforming to ies2tab"
 fi
done

#if grep -qs Full all.tab 
if [ -f sc.lis ] 
 then
  # creating a fco_fs subdirectory with symlinks to all FCO and FS files: 
  echo -e \
"# Making a directory for FCO and FS luminaires \n"\
"#   containing symlinks to the original directory \n"\
"\n"\
"if ! [ -a fco_fs ] ; then mkdir fco_fs ; fi \n"\
"cd fco_fs \n"\
"for F in \\"\
   >fco_fs.dir
  cat sc.lis >> fco_fs.dir
  echo -e "\n do if ! [ -a \$F ] ; then ln -s ../\$F \$F ; fi ; done" \
   >> fco_fs.dir
  chmod +x fco_fs.dir
  ./fco_fs.dir
  if grep -qs "Full_CutOff" all.tab 
  then
   ies2tab -l -c -u $@ >fco.tab
   grep "Full_CutOff" all.tab >>fco.tab
  fi    
  if grep -qs "Fully_Shielded" all.tab 
  then
   ies2tab -l -c -u $@ >fs.tab
   grep "Fully_Shielded" all.tab >>fs.tab
  fi
fi

if grep -qs "CIE_CutOff" all.tab 
 then
  ies2tab -l -c -u $@ >co.tab
  grep "CIE_CutOff" all.tab >>co.tab
fi  
if grep -qs "IES_CutOff" all.tab 
 then
  ies2tab -l -c -u $@ >ico.tab
  grep "IES_CutOff" all.tab >>ico.tab
fi
