#!/bin/bash

usage () {
  echo "`basename $0` all <targetimage> ... "
  echo "`basename $0` -h"
  echo "`basename $0` --help"
}

_extractLUT () { # $1: lutsrc $2: lutfile
   if [ -f $2 ]; then rm $2 ; fi
   convert -verbose $1 -gravity SouthEast -crop 512x512+0+0 +repage -strip $2
}

_normalizeIMGs () {
   ( for img in $*; do
      echo time convert -verbose -depth 8  \
            -crop 1920x1080-0-0 $img \
            -gravity SouthEast -crop 512x512-0-0 -background gray -extent 1920x512+0+0 $img \
            -append $img.i
	                   ##### HACK; NYI; must specify size generically
   done ) | gm batch -echo on -feedback off
   for img in $*; do
      mv -v $img.i $img
   done
}

_videoLUT_all () {
   echo "Called _videoLUT_all $*"
    if [ $# = 0 ]; then
       echo "No Parameter given"
    elif [ $# = 1 ]; then
       lutsrc=$1
       dir=$(dirname "$lutsrc")
       # fn=$(basename "$1")
       fn=${1##*/}
       ext=${fn##*.}
       fb=${fn%.*}

       echo "Removing single luts"
       rm -vf $dir/../../lut/*.png

       echo "extract LUT from $lutsrc"
       LUT=$dir/../../1lut.png

       _extractLUT $lutsrc $LUT

      ( for img in $dir/*.$ext; do
          if [ $lutsrc != $img ]; then
              d=$(dirname "$img")
              i=${img##*/}
              # ext=${fn##*.}
              ib=${i%.*}
#             if [ -f $d/../../raw/tif/$ib.tif ]; then
#              echo "time convert -verbose -depth 8 -crop 1920x1080-0-0 -hald-clut $LUT -background gray -gravity SouthEast $d/../../raw/tif/$ib.tif -background gray -gravity SouthEast -extent 1920x512+0+0 $LUT -append $img"
              is=$d/../../raw/png/$ib.png
             if [ -f $is ]; then
                  echo  time convert -verbose -depth 8 \
                     -crop 1920x1080-0-0 -hald-clut $LUT $is \
                     -background gray -gravity SouthEast -extent 1920x512+0+0 $LUT \
                     -append $img
             fi
          fi
        done ) | gm batch -echo on -feedback off

        _normalizeIMGs $lutsrc

    else
       echo "Error: More then on image indicated; doing nothing"
    fi
}

_videoLUT_gradient () {
set -x
   echo "Called _videoLUT_gradient $*"
    if [ $# = 0 ]; then
       echo "No Parameter given"
    elif [ $# = 1 ]; then
       lutsrc=$1
       dir=$(dirname "$lutsrc")
       # fn=$(basename "$1")
       fn=${1##*/}
       ext=${fn##*.}
       fb=${fn%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          echo "Removing Single Lut $LUT1"
          rm -v $LUT1
          for ll in $dir/*.$ext ; do
                # l=$ll
                d=$(dirname "$ll")
                f=${ll##*/}
                e=${f##*.}
                b=${f%.*}
              echo "extract LUT from $lutsrc to $LUT"
             _extractLUT $ll $d/../../lut/$b.png
          done
       else
          LUT=$dir/../../lut/$fb.png
          echo "extract LUT from $lutsrc to $LUT"
          _extractLUT $lutsrc $LUT
       fi

       _normalizeIMGs $lutsrc

    elif [ $# = 2 ]; then
       dir=$(dirname "$1")
       # fn=$(basename "$1")
       fn=${1##*/}
       ext=${fn##*.}
       # fb=${fn%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          echo "Removing Single Lut $LUT1"
          rm -v $LUT1
          for ll in $dir/*.$ext ; do
                d=$(dirname "$ll")
                f=${ll##*/}
                e=${f##*.}
                b=${f%.*}
              echo "extract LUT from $ll to $d/../../lut/$b.png"
             _extractLUT $ll $d/../../lut/$b.png
          done
       else
          l=$1
          d=$(dirname "$l")
          f=${l##*/}
          e=${f##*.}
          b=${f%.*}
          echo "extract LUT from $l to $d/../../lut/$b.png"
          _extractLUT $l $d/../../lut/$b.png
          l=$2
          d=$(dirname "$l")
          f=${l##*/}
          e=${f##*.}
          b=${f%.*}
          echo "extract LUT from $l to $d/../../lut/$b.png"
          _extractLUT $l $d/../../lut/$b.png
       fi

       _normalizeIMGs $1 $2

    else # $# -ge 3

       dir=$(dirname "$1")
#       # fn=$(basename "$1")
       f1=${1##*/}
       ext=${f1##*.}
       b1=${f1%.*}

       last=${@: -1}
       f2=${last##*/}
       b2=${f2%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          REGENERATE=yes
          rm -v $LUT1
       else
          REGENERATE=no
       fi

       _extractLUT $1 $dir/../../lut/$b1.png
       _extractLUT $last $dir/../../lut/$b2.png

       _normalizeIMGs $1 $last

       for ll in $dir/*.$ext ; do
            d=$(dirname "$ll")
            f=${ll##*/}
            e=${f##*.}
            b=${f%.*}

           if [ $b -lt $b1 ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           elif [ $b -eq $b1 ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b1 -lt $b -a $b -lt $b2 ]; then
                  f1=`echo "scale=4;($b2-$b)/($b2-$b1)" | bc`
                  f2=`echo "scale=4;($b-$b1)/($b2-$b1)" | bc` 
                   echo "Mixing $d/../../lut/$b1.png ($f1) with $d/../../lut/$b2.png ($f2)"
                   _mixlut $d/../../lut/$b1.png $f1 $d/../../lut/$b2.png $f2 $d/../../lut/$b.png
                   echo "Applying LUT to picture"
                   _applyLUT $ll $d/../../lut/$b.png
           elif [ $b -eq $b2 ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b2 -lt $b ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           else 
              echo "Unknown case"
           fi
           echo "extract LUT from $ll to $d/../../lut/$b.png"
       done

    fi
}

_videoLUT_right () {
   echo "Called _videoLUT_right $*"
    if [ $# = 0 ]; then
       echo "No Parameter given"
    elif [ $# = 1 ]; then
       lutsrc=$1
       dir=$(dirname "$lutsrc")
       fn=$(basename "$lutsrc")
       fn=${lutsrc##*/}
       ext=${fn##*.}
       fb=${fn%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          REGENERATE=yes
          rm -v $LUT1
       else
          REGENERATE=no
       fi

       _extractLUT $lutsrc $dir/../../lut/$fb.png
       _normalizeIMGs $lutsrc

       for ll in $dir/*.$ext ; do
            l=$ll
            d=$(dirname "$l")
            f=${ll##*/}
            e=${f##*.}
            b=${f%.*}

           if [ $b -lt $fb ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           elif [ $b -eq $fb ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $fb -lt $b ]; then
                echo "Applying LUT to picture"
                 cp -v $dir/../../lut/$fb.png $d/../../lut/$b.png
                _applyLUT $ll $d/../../lut/$b.png
           else 
              echo "Unknown case"
           fi
       done

    else # $# -ge 2

       dir=$(dirname "$1")
#       # fn=$(basename "$1")
       f1=${1##*/}
       ext=${f1##*.}
       b1=${f1%.*}

       last=${@: -1}
       f2=${last##*/}
       b2=${f2%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          REGENERATE=yes
          rm -v $LUT1
       else
          REGENERATE=no
       fi

       _extractLUT $1 $dir/../../lut/$b1.png
       _normalizeIMGs $1

       for ll in $dir/*.$ext ; do
            l=$ll
            d=$(dirname "$l")
            f=${ll##*/}
            e=${f##*.}
            b=${f%.*}

           if [ $b -lt $b1 ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           elif [ $b -eq $b1 ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b1 -lt $b -a $b -le $b2 ]; then
                   cp -v $d/../../lut/$b1.png $d/../../lut/$b.png
                   echo "Applying LUT to picture"
                   _applyLUT $ll $d/../../lut/$b.png
#           elif [ $b -eq $b2 ]; then
#                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b2 -lt $b ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           else 
              echo "Unknown case"
           fi
           echo "extract LUT from $ll to $d/../../lut/$b.png"
       done

    fi
}

_videoLUT_left () {
   echo "Called _videoLUT_left $*"
    if [ $# = 0 ]; then
       echo "No Parameter given"
    elif [ $# = 1 ]; then
       lutsrc=$1
       dir=$(dirname "$lutsrc")
       fn=$(basename "$lutsrc")
       fn=${lutsrc##*/}
       ext=${fn##*.}
       fb=${fn%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          REGENERATE=yes
          rm -v $LUT1
       else
          REGENERATE=no
       fi

       _extractLUT $lutsrc $dir/../../lut/$fb.png
       _normalizeIMGs $lutsrc

       for ll in $dir/*.$ext ; do
            l=$ll
            d=$(dirname "$l")
            f=${ll##*/}
            e=${f##*.}
            b=${f%.*}

           if [ $b -lt $fb ]; then
                echo "Applying LUT to picture"
                cp -v $dir/../../lut/$fb.png $d/../../lut/$b.png
                _applyLUT $ll $d/../../lut/$b.png
           elif [ $b -eq $fb ]; then
                 echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $fb -lt $b ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           else 
              echo "Unknown case"
           fi
       done

    else # $# -ge 2

       dir=$(dirname "$1")
#       # fn=$(basename "$1")
       f1=${1##*/}
       ext=${f1##*.}
       b1=${f1%.*}

       last=${@: -1}
       f2=${last##*/}
       b2=${f2%.*}

       LUT1=$dir/../../1lut.png

       if [ -f $LUT1 ]; then
          REGENERATE=yes
          rm -v $LUT1
       else
          REGENERATE=no
       fi

       _extractLUT $last $dir/../../lut/$b2.png
       _normalizeIMGs $last

       for ll in $dir/*.$ext ; do
            l=$ll
            d=$(dirname "$l")
            f=${ll##*/}
            e=${f##*.}
            b=${f%.*}

           if [ $b -lt $b1 ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
#           elif [ $b -eq $b1 ]; then
#                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b1 -le $b -a $b -lt $b2 ]; then
                   cp -v $d/../../lut/$b2.png $d/../../lut/$b.png
                   echo "Applying LUT to picture"
                   _applyLUT $ll $d/../../lut/$b.png
           elif [ $b -eq $b2 ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png already done" 
           elif [ $b2 -lt $b ]; then
               if [ $REGENERATE = yes ]; then
                   echo "extract LUT from $ll to $d/../../lut/$b.png"
                   _extractLUT $ll $d/../../lut/$b.png
               fi              
           else 
              echo "Unknown case"
           fi
           echo "extract LUT from $ll to $d/../../lut/$b.png"
       done

    fi
}

_mixlut () { # lut1, p1, lut2, p2, targetlut
     set -x
     convert -verbose $1 $3 -poly "$2,1 $4,1" $5
     set +x
}

_applyLUT () { # img, lut
    set -x
    f=`realpath $1`
    raw=`echo $f | sed '1,$ s/\/color\/png\//\/raw\/tif\//g ; 1,$ s/\.png$/\.tif/g'`
    gm time convert -verbose -depth 8 \
            -crop 1920x1080-0-0 -hald-clut $2 $raw \
            -background gray -gravity SouthEast -extent 1920x512+0+0 $2 \
            -append $1
	                   ##### HACK; NYI; must specify size generically
    set +x
}



##### READING PARAMETERS ################################################################

echo "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
echo "$0 $*"

if [ "$1" = "-h" -o "$1" = "--help" ]; then
  usage
  exit 17
fi

if [ "$#" = 0 ]; then
  usage
  exit 17
fi

FIRST="$1"
shift
REST="`echo "$*" | sed '1,$ s/file:\/\///g'`"

_videoLUT_$FIRST $REST

