#!/bin/ksh
# process_dvr_output - a script processing dvr steering + data files
# $Id: process_dvr_output 260 2009-03-16 11:57:58Z raasch $

     # Last changes:
     # 16/03/09 - Siggi - Generating the first version



    # variable declarations + default values
 camera=false
 groundplate=false
 topography=false

 typeset  -i  i nstream nscenes


    # read shellscript options
 while  getopts  :d:f: option
 do
   case  $option  in
       (d)   data_catalog=$OPTARG;;
       (f)   file_identifier=$OPTARG;;
       (\?)  printf "\n  +++ unknown option $OPTARG \n"
             printf "\n      allowed option are -d and -f \n"
             exit;;
   esac
 done


    # find out the number of streams
 (( i = 0 ))
 while true
 do
    (( i = i + 1 ))
    if (( i < 10 ))
    then
       cstream=0$i
    else
       cstream=$i
    fi

    if [[ $(ls -1 *.* | grep -c $cstream) = 0 ]]
    then
       (( i = i - 1 ))
       break
    fi

       # find out the stream name
    streamname[$i]=`ls -1 ${cstream}_*-ge.dvrs | cut -f1 -d"-"`

       # get addres
    adr[$i]=`grep ADR= ${streamname[$i]}.dvrs | grep '*' | cut -f2 -d"="`

       # get maxbytes
    maxbytes[$i]=`head -1 ${streamname[$i]}.max`

       # get number of frames
    frames[$i]=`tail -1 ${streamname[$i]}.max`

 done

 nstream=$i
 nscenes=$nstream

 echo "*** number of detected streams = $i"

 (( i = 0 ))
 while (( i < nstream ))
 do

    (( i = i + 1 ))
    echo "*** streamname: \"${streamname[$i]}\" "
    echo "    adr = \"${adr[$i]}\" "
    echo "    maxbytes = \"${maxbytes[$i]}\" "
    echo "    frames = \"${frames[$i]}\" "

 done

    # Check, if there are files containing the camera data, the ground plate
    # and topography data.
    # Check the first stream only, because all streams have the same files.
 if [[ -f ${streamname[1]}_camera.max ]]
 then
    camera=true
    adr_camera=`grep ADR= ${streamname[1]}.dvrs | grep 'camera' | cut -f2 -d"="`
    maxbytes_camera=`head -1 ${streamname[1]}_camera.max`
    (( nscenes = nscenes + 1 ))
 fi
 if [[ -f ${streamname[1]}_groundplate.max ]]
 then
    groundplate=true
    adr_groundplate=`echo $adr_camera | sed "s/camera/groundplate/g"`
    maxbytes_groundplate=`head -1 ${streamname[1]}_groundplate.max`
    (( nscenes = nscenes + 1 ))
 fi
 if [[ -f ${streamname[1]}_topography.max ]]
 then
    topography=true
    adr_topography=`echo $adr_camera | sed "s/camera/topography/g"`
    maxbytes_topography=`head -1 ${streamname[1]}_topography.max`
    (( nscenes = nscenes + 1 ))
 fi



    # start with writing the dvrs file for the combined streams
 dvr_file=${file_identifier}.dvrs

 echo  "SCENES=$nscenes"  >  $dvr_file

    # first, add the static scenes
 if [[ $camera = true ]]
 then
    echo  "MED=TCP"                    >>  $dvr_file
    echo  "ADR=$adr_camera"            >>  $dvr_file
    echo  "MAXBYTES=$maxbytes_camera"  >>  $dvr_file
    echo  "FRAMES=1"                   >>  $dvr_file
    echo  "FRAMES_P_SEC=0"             >>  $dvr_file
    echo  "SCENEEND"                   >>  $dvr_file
 fi

 if [[ $groundplate = true ]]
 then
    echo  "MED=TCP"                         >>  $dvr_file
    echo  "ADR=$adr_groundplate"            >>  $dvr_file
    echo  "MAXBYTES=$maxbytes_groundplate"  >>  $dvr_file
    echo  "FRAMES=1"                        >>  $dvr_file
    echo  "FRAMES_P_SEC=0"                  >>  $dvr_file
    echo  "SCENEEND"                        >>  $dvr_file
 fi

 if [[ $topography = true ]]
 then
    echo  "MED=TCP"                        >>  $dvr_file
    echo  "ADR=$adr_topography"            >>  $dvr_file
    echo  "MAXBYTES=$maxbytes_topography"  >>  $dvr_file
    echo  "FRAMES=1"                       >>  $dvr_file
    echo  "FRAMES_P_SEC=0"                 >>  $dvr_file
    echo  "SCENEEND"                       >>  $dvr_file
 fi


     # now add the streams
 (( i = 0 ))
 while (( i < nstream ))
 do

    (( i = i + 1 ))

    echo  "MED=TCP"                   >>  $dvr_file
    echo  "ADR=${adr[$i]}"            >>  $dvr_file
    echo  "MAXBYTES=${maxbytes[$i]}"  >>  $dvr_file
    echo  "FRAMES=${frames[$i]}"      >>  $dvr_file
    echo  "FRAMES_P_SEC=25"           >>  $dvr_file
    echo  "SCENEEND"                  >>  $dvr_file

 done


    # create the html file for the combined scenes
 cp  01_*-ge.html  tmp.html
 replace=$(echo `grep src= tmp.html`)
 sed "s&${replace}&src=\"${file_identifier}.dvrs\"&g" tmp.html > ${file_identifier}.html
 rm tmp.html
