#!/bin/sh
# unpack-lrauv-cell-logs.sh

#TODO# Would probably be more efficient to use python and/or inotify in the future.

FLOCK=/usr/bin/flock
LOCKDIR=/tmp
XZ=/usr/bin/xz
# ... only want one unpack or unserialize instance running at a time (for now)

#TODO make the locations below more portable
DATA_ROOT=/mbari/LRAUV
UNSERIALIZE=/mbari/LRAUV/Tools/linuxi64/unserialize
PRIORITYSLATEFILENAME="cell-Priority"
NORMALSLATEFILENAME="cell-Normal"

for vehicle in tethys daphne makai ahi aku opah whoidhs galene brizo pontus triton
do
  lck=$LOCKDIR/handle-lrauv-cell-logs.$vehicle.lck  # don't unpack during push or pull
  searchbase=$DATA_ROOT"/"$vehicle"/realtime/cell-logs/"
  searchlimit=$searchbase`date --date="1 month ago" +%Y%m%dT%H%M%S`
  for d in `ls -d "$searchbase"*`
  do
    if [ "$d" \< "$searchlimit" ]; then
      continue
    fi
    unpack=false
    echo "checking for Priority-level logs in $d"
    if test -n "$(find $d -maxdepth 1 -name 'Priority*' -print -quit)"
    then
      unpackedslate="$(find $d -maxdepth 1 -name $PRIORITYSLATEFILENAME'*'.js -print -quit)"
      if test $unpackedslate # string $unpackedslate is not NULL
      then
        echo "found $unpackedslate, checking for new Priority-level logs."
        newerslate="$(find $d -maxdepth 1 -name 'Priority*' -newer $unpackedslate -print -quit)"
        if test $newerslate # string $newerslate is not NULL
        then
          unpack=true
          echo "found $newerslate, newer than $unpackedslate -- will attempt to unpack"
        else
          unpack=false
          echo "did not find any Priority-level logs newer than $unpackedslate"
        fi
      else
        unpack=true
        newerslate="[first time]"
        echo "$PRIORITYSLATEFILENAME*.js does not exist in $d, but Priority-level logs do -- will attempt to unpack."
      fi

      if test "$unpack" == true
      then
        echo "unzipping lzma files in $d"
        $XZ --decompress --keep $d/*.lzma &> /dev/null
        msg="unpacking slates up to $newerslate"
        cmd="nice $UNSERIALIZE -d -ljmN -P Priority $d/$PRIORITYSLATEFILENAME"
        $FLOCK -n -e $lck -c "echo $msg && echo $cmd && $cmd"
#        $FLOCK -n -e $lck -c "echo DEBUG && echo $msg && echo $cmd"
        echo "unpacked Priority-level logs in $d"
      fi
    else
      echo "did not find Priority-level logs in $d"
    fi
    unpack=false
    echo "checking for Normal-level logs in $d"
    if test -n "$(find $d -maxdepth 1 -name 'Normal*' -print -quit)"
    then
      unpackedslate="$(find $d -maxdepth 1 -name $NORMALSLATEFILENAME'*'.js -print -quit)"
      if test $unpackedslate # string $unpackedslate is not NULL
      then
        echo "found $unpackedslate, checking for new Normal-level logs."
        newerslate="$(find $d -maxdepth 1 -name 'Normal*' -newer $unpackedslate -print -quit)"
        if test $newerslate # string $newerslate is not NULL
        then
          unpack=true
          echo "found $newerslate, newer than $unpackedslate -- will attempt to unpack"
        else
          unpack=false
          echo "did not find any Normal-level logs newer than $unpackedslate"
        fi
      else
        unpack=true
        newerslate="[first time]"
        echo "$NORMALSLATEFILENAME*.js does not exist in $d, but Normal-level logs do -- will attempt to unpack."
      fi

      if test "$unpack" == true
      then
        echo "unzipping lzma files in $d"
        $XZ --decompress --keep $d/*.lzma &> /dev/null
        msg="unpacking slates up to $newerslate"
        cmd="nice $UNSERIALIZE -d -jmN -P Normal $d/temp_$NORMALSLATEFILENAME"
        $FLOCK -n -e $lck -c "echo $msg && echo $cmd && $cmd"
        flock_result=$?
        if [ $flock_result -eq 0 ]
        then
          mv "$d/temp_$NORMALSLATEFILENAME.js" "$d/$NORMALSLATEFILENAME.js"
          mv "$d/temp_$NORMALSLATEFILENAME.mat" "$d/$NORMALSLATEFILENAME.mat"
          mv "$d/temp_$NORMALSLATEFILENAME.nc4" "$d/$NORMALSLATEFILENAME.nc4"
          echo "unpacked Normal-level logs in $d"
        else
          echo "due to lock, did not unpack Normal-level logs in $d"
        fi
      fi
    else
      echo "did not find Normal-level logs in $d"
    fi

  done
done

