#!/bin/bash

# JUST FOR *.LDT, AT THIS PLACE

# in a directory with any ies/eulumdat files
# computes images and tables,
# creates html overviews (for spacing 5 and either 6 or 4)
# and moves photometric files to photom_files/ 
#   (Symlink ldt)
#           and obsolete text files into txt_obsolete/

# the second parameter is the needed rotation of the photometry in degrees

# (A script by Jan Hollan, using the ies2tab programme)

if [[ $1 == '' ]]
then
 echo 'needs a parameter: producer'
 exit
fi  

if ! [[ $2 == '' ]]
then
 R="-rv$2"
else
 R=
fi  


#blanks2dots.sh

if ! [ -a photom_files ] ; then mkdir photom_files ; fi
if ! [ -a large ] ; then mkdir large ; fi
if ! [ -a small ] ; then mkdir small ; fi
if ! [ -a large_single ] ; then mkdir large_single ; fi
if ! [ -a small_single ] ; then mkdir small_single ; fi
if ! [ -a htm ] ; then mkdir htm ; fi
if ! [ -a tab ] ; then mkdir tab ; fi
if ! [ -a large_6 ] ; then mkdir large_6 ; fi
if ! [ -a small_6 ] ; then mkdir small_6 ; fi
if ! [ -a large_4 ] ; then mkdir large_4 ; fi
if ! [ -a small_4 ] ; then mkdir small_4 ; fi
if ! [ -f lum.png ] ; then 
 ln -s ~/fixtures/lum.png ./
fi
if [ -f sli65-70.txt ] ; then rm sli65-70.txt ; fi

sed -r "s/PRODUCER/$1/" ~/fixtures/all_5all.head > all_5.htm
sed -r "s/PRODUCER/$1/" ~/fixtures/all_singleall.head > all_single.htm
sed -r "s/PRODUCER/$1/" ~/fixtures/READMEall.head > README.html



#for H in *.ldt *.LDT *.eld *.ELD *.ies *.IES
for H in *.LDT
do 
if [ -f $H ] 
then  
# suffix:
  S=${H/*\.}
# filename-.suffix:
  F=${H/\.$S}
echo "$F . $S"
  E='non-photometric'
  if [[ $S == eld || $S == ldt || $S == LDT || $S == ELD ]] 
  then 
   E='-e'
  else
   if [[ $S == ies || $S == IES ]]
   then 
    E= 
   fi    
  fi 

# if max. spec. lum. int. at >65 is over 599 cd/klm, then:
 ~/bin/ies2tab $E -l -mi -la15 $H 
 M=`~/bin/ies2tab $E -l -mi -la15 $H \
 | sed -rn "s/^ *([0-9.?]+ +){8}([0-9.]+) .*/\2/p"`
 echo $M "cd/klm is the max. spec. lum. int. at >65"
#exit
 if (( $M \> 599 ))
 then
 ~/bin/ies2tab $E -cm2 -n$F.ppm $R -rh6 -co $H >/dev/null
 convert $F.ppm small_6/$F.png
 ~/bin/ies2tab $E -ic  -n$F.ppm -mi -la15 $R -rh6 -co $H >/dev/null
 convert $F.ppm large_6/$F.png
 else 
  if (( $M \< 450 ))
  then
 ~/bin/ies2tab $E -cm2 -n$F.ppm $R -rh4 -co $H >/dev/null
 convert $F.ppm small_4/$F.png
 ~/bin/ies2tab $E -ic  -n$F.ppm -mi -la15 $R -rh4 -co $H >/dev/null
 convert $F.ppm large_4/$F.png
  fi
 fi  

 ~/bin/ies2tab $E -cm2 -n$F.ppm $R -rh5 -co $H >/dev/null
 convert $F.ppm small/$F.png
 ~/bin/ies2tab $E -ic  -n$F.ppm -mi -la15 $R -rh5 -co $H >tab/$F.txt
 convert $F.ppm large/$F.png
 ~/bin/ies2tab $E -cm2 -n$F.ppm  $H >/dev/null
 convert $F.ppm small_single/$F.png
 ~/bin/ies2tab $E     -n$F.ppm  $H >/dev/null
 convert $F.ppm large_single/$F.png
 ~/bin/ies2tab $E -l -mi -la15 $H >> sli65-70.txt


 rm $F.ppm
 echo "<A href=\"htm/$F.htm\"><img SRC=\"small/$F.png\">" >>all_5.htm 
 echo "<A href=\"htm/$F.htm\"><img SRC=\"small_single/$F.png\">" >>all_single.htm 

 mv $H photom_files/
fi

done

sed -rn "/^.+$/{
s/    ([ 1][0-9])/  \1/
s/\.(ies|IES|ldt|LDT|eld|ELD)//p}" sli65-70.txt > plain.txt


sed -r "s/PRODUCER/$1/" ~/fixtures/allall.head > all.txt
cat plain.txt >>all.txt
 
sort -k 9 -r -n plain.txt > sli70.txt

sed -r "s/PRODUCER/$1/" ~/fixtures/all_5_70all.head > all_5_70.htm
sed  -r '
s|.+ ([a-zA-Z0-9_+.-]+) *$|<A href=\"htm/\1.htm\"><img SRC=\"small/\1.png\">|
' sli70.txt >> all_5_70.htm		 

sed -r "s/PRODUCER/$1/" ~/fixtures/sli70all.head > sli70.txt
sort -k 9 -r -n plain.txt >> sli70.txt

sed -r "s/PRODUCER/$1/" ~/fixtures/sli65all.head > sli65.txt
sort -k 8 -r -n plain.txt >> sli65.txt

sed -r "s/PRODUCER/$1/" ~/fixtures/sli_maxall.head > sli_max.txt
sort -k 10 -r -n plain.txt >> sli_max.txt

htm_im6.sh 

if ! [ -a txt_obsolete ] ; then mkdir txt_obsolete ; fi


if  grep -l "Full" plain.txt > /dev/null 
then 
 ./ies_all2FS.sh $1
 hyper_fstxt.sh
 for F in fs70.txt fs65.txt fs_max.txt fs.txt
 do
  mv $F txt_obsolete/
 done 
fi

if  egrep -l "(IES_|CIE_|Non-|Unknown  )" plain.txt > /dev/null
then
 hyper_txt.sh
else
for F in sli70.html sli65.html sli_max.html all*.htm*
do
 mv $F txt_obsolete/
done

fi
for F in sli70.txt sli65.txt sli_max.txt all.txt sli65-70.txt plain.txt
do
 mv $F txt_obsolete/
done

ln -s photom_files ldt

zip -r all large large_single small small_single large_6 small_6 htm \
 sli70* sli65.* \
 sli_max* all*.htm* fs*.html README.html
