#!/bin/bash
#
# get_proxy_values
#
# Werte der ULS-Clients aus der DMZ abholen
#
###############################################################################
#
TMPBASE=/ulsproxytmp
ULSPWDB64="to be defined"
HTTPGETCMD="httpget -S -s -a $ULSPWDB64"
MYSQLSOCKET=/ulsdb/mysql/mysql.sock
#
export USLEEPTIME=100000
#
if [[ "$1" = "-s" ]]
 then
  sleep $2
  shift 2
fi
#
PATH=/srv/uls/wwwbin:/usr/bin:/usr/local/bin:$PATH
export PATH
#
if [[ -z "$1" ]]
 then
  echo "usage: `basename $0` <proxy>"
  exit 1
fi
#
PROXY="$0"
#
if [ -f /etc/ulsserver.conf ]
 then
  . /etc/ulsserver.conf
fi
if [ -z "$ULSNAME" ]
 then
  ULSNAME=`hostname`
fi
#
CS=`/usr/bin/mysql -N -B -u "logging" -ploggingput -e "select count(*) from swerte" logging`
if echo $CS | grep -q '^[0-9][0-9]*'
 then
  :
else
  echo "Error: no connection to Database"
  exit 1
fi
#
TMPPATH=$TMPBASE/$$
#
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 -S $MYSQLSOCKET -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 -S $MYSQLSOCKET -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 -S $MYSQLSOCKET -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
{ if cd $1
   then
    file2ulsdb -S $MYSQLSOCKET -i $1 -D . 2>&1 | head -3
    cd ..
  fi
  rm $1/* 2>&1
  rmdir $1 2>&1
}
#
if mkdir -p $TMPPATH && cd $TMPPATH
 then
  if $HTTPGETCMD $1 "get_uls_data.s3w" | tar xfz - 2>/tmp/get_uls_data.$$.err
   then
    if [[ -d TAB ]]
     then
      ( if cd TAB
         then
          ls | while read IP
           do
            if [[ -d $IP ]]
             then
              make_tab $IP &
              usleep $USLEEPTIME
            fi
          done
        fi
      )
    fi
    if [[ -d FP ]]
     then
      ( if cd FP
         then
          ls | while read IP
           do
            if [[ -d $IP ]]
             then
              make_fp $IP &
              usleep $USLEEPTIME
            fi
          done
        fi
      )
    fi
    if [[ -d FPZ ]]
     then
      ( if cd FPZ
         then
          ls | while read IP
           do
            if [[ -d $IP ]]
             then
              make_fpz $IP &
              usleep $USLEEPTIME
            fi
          done
        fi
      )
    fi
    if [[ -d TGZ ]]
     then
      ( if cd TGZ
         then
          ls | while read IP
           do
            if [[ -d $IP ]]
             then
              make_tgz $IP &
              usleep $USLEEPTIME
            fi
          done
        fi
      )
    fi
    cd ..
    sleep 10
    while [[ `find $TMPPATH -mindepth 2 -maxdepth 2 2>/dev/null | wc -l` -gt 0 ]]
     do
      sleep 5
    done
    rmdir $TMPPATH/* 2>&1
  else
    echo "Error: $?"
    if [[ -s /tmp/get_uls_data.$$.err ]]
     then
      cat /tmp/get_uls_data.$$.err
    fi
  fi
  rm /tmp/get_uls_data.$$.err
  rmdir $TMPPATH 2>&1
fi
