#!/bin/bash
#
# store_buffered_values
#
# Werte in DB eintragen, die zwischengespeichert wurden
#
########################################################
#
export SLEEP="sleep 0.7"
#
ULSSENDWAIT=10
#
PATH=/srv/uls/wwwbin:/usr/bin:/usr/local/bin:$PATH
export PATH
#
if [ -f /etc/ulsserver.conf ]
 then
  . /etc/ulsserver.conf
fi
if [[ -z "$ULSSTOREPATH" ]]
 then
  ULSSTOREPATH=/var/tmp/file2ulsstore
fi
if [ -z "$ULSNAME" ]
 then
  ULSNAME=`hostname`
fi
#
if [[ ! -d "$ULSSTOREPATH" ]]
 then
  exit 0
fi
#
CS=`/usr/bin/mysql -N -B -u "wwwuls" -e "select count(*) from verfahren" logging`
if echo $CS | grep -q '^[0-9][0-9]*'
 then
  :
else
  echo "Error: no connection to Database"
  exit 1
fi
#
TMPPATH="$ULSSTOREPATH.$$"
#
function make_fp
{ ip=$1
  if cd $1
   then
    SEPPATH=T.$$
    ls | while read F
     do
      ( if mkdir $SEPPATH && cd $SEPPATH
         then
          fileunpack ../$F
          file2ulsdb -i $ip -D . 2>&1 | head -3
          cd ..
        fi
      )
      rm -r $SEPPATH 2>&1
      rm $F 2>&1
    done
    cd ..
  fi
  rmdir $1 2>&1
}
#
function make_fpz
{ ip=$1
  if cd $1
   then
    SEPPATH=T.$$
    ls | while read F
     do
      ( if mkdir $SEPPATH && cd $SEPPATH
         then
          zcat ../$F | fileunpack
          file2ulsdb -i $ip -D . 2>&1 | head -3
          cd ..
        fi
      )
      rm -r $SEPPATH 2>&1
      rm $F 2>&1
    done
    cd ..
  fi
  rmdir $1 2>&1
}
#
function make_tgz
{ ip=$1
  if cd $1
   then
    SEPPATH=T.$$
    ls | while read F
     do
      ( if mkdir $SEPPATH && cd $SEPPATH
         then
          tar xfz ../$F
          file2ulsdb -i $ip -D . 2>&1 | head -3
          cd ..
        fi
      )
      rm -r $SEPPATH 2>&1
      rm $F 2>&1
    done
    cd ..
  fi
  rmdir $1 2>&1
}
#
function make_tab
{ file2ulsdb -i $1 -D $1 2>&1 | head -3
  rm $1/* 2>&1
  rmdir $1 2>&1
}
#
if [[ -d "$TMPPATH" ]]
 then
  TMPPATH="$TMPPATH.`date '+%s'`"
fi
if mv "$ULSSTOREPATH" "$TMPPATH" && cd "$TMPPATH"
 then
  sleep $ULSSENDWAIT
  if [[ -d FP ]]
   then
    ( if cd FP
       then
        ls | while read IP
         do
          if [[ -d $IP ]]
           then
            make_fp $IP &
            $SLEEP
          fi
        done
      fi
    )
  fi
  if [[ -d FPZ ]]
   then
    ( if cd FPZ
       then
        ls | while read IP
         do
          if [[ -d $IP ]]
           then
            make_fpz $IP &
            $SLEEP
          fi
        done
      fi
    )
  fi
  if [[ -d TGZ ]]
   then
    ( if cd TGZ
       then
        ls | while read IP
         do
          if [[ -d $IP ]]
           then
            make_tgz $IP &
            $SLEEP
          fi
        done
      fi
    )
  fi
  if [[ -d TAB ]]
   then
    ( if cd TAB
       then
        ls | while read IP
         do
          if [[ -d $IP ]]
           then
            make_tab $IP &
            $SLEEP
          fi
        done
      fi
    )
  fi
  cd ..
  sleep 10
  while [[ `find $TMPPATH -mindepth 2 -maxdepth 2 | wc -l` -gt 0 ]]
   do
    sleep 5
  done
  rmdir $TMPPATH/* 2>&1
  rmdir $TMPPATH 2>&1
fi
