#!/bin/bash
#
# send_file_value
#
# Speichert logginginformationen in der Loggingdatenbank ueber einen httpget-Aufruf
#
# Aufruf:
#
# send_file_value [-z|-j|-J] [-R] [-c charset] [-n filename] [-d storepath] [-S] [-h host] [-s acc] [-a] section teststep details date time [<Datei>]
#
###################################################################################
#
if [[ -f /etc/uls/uls.conf ]]
 then
  . /etc/uls/uls.conf
fi
#
if [[ -n "$SET_ULSTMP_PATH" ]]
 then
  ULS_TMP_PATH="$SET_ULSTMP_PATH"
fi
#
if [[ -z "$ULSSERVER" ]]
 then
  ULSSERVER="STORE"
fi
#
ULSSEND="STORE"
ACC="all"
APPEND=""
APP=""
if [[ ${LC_CTYPE:-$LANG} == *8 ]]
 then
  CHARSET="utf8"
else
  CHARSET="latin1"
fi
#
unset COMPRESS COMPRESSEXT REMOVE FILENAME
#
while getopts n:zjJRc:d:Sh:s:a? op
 do
  case "$op" in
    c) CHARSET="$OPTARG";;
    n) FILENAME="$OPTARG";;
    d) ULS_TMP_PATH="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME="$OPTARG";;
    s) ACC=$OPTARG;;
    a) APPEND=_append
       APP=A;;
    z) COMPRESS=gzip
       COMPRESSEXT='.gz';;
    j) COMPRESS=bzip2
       COMPRESSEXT='.bz2';;
    J) COMPRESS=xz
       COMPRESSEXT='.xz';;
    R) REMOVE=1;;
    *) echo "usage: `basename $0` [-z|-j|-J] [-R] [-c charset] [-n filename] [-d storepath] [-S] [-h hostname] [-s access] [-a] section teststep details date time <path>"
       exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#
#
if [[ $# -ne 6 ]]
 then
  echo "usage: `basename $0` [-z|-j|-J] [-R] [-c charset] [-n filename] [-d storepath] [-S] [-h hostname] [-s access] [-a] section teststep details date time <path>"
  exit 1
fi
#
if [[ -z "$ULSHOSTNAME" ]]
 then
  ULSHOSTNAME=`hostname`
fi
#
if [[ "$ULSSEND" != "STORE" ]]
 then
  if STATUS=`httpget $SFLAGS -u logging -p lgput -s $ULSSERVER get_status.s2w?hostname=$ULSHOSTNAME`
   then
    if [[ "$STATUS" != "OK" ]]
     then
      ULSSEND="STORE"
    fi
  else
    ULSSEND="STORE"
  fi
fi
#
if [[ "$ULSSEND" != "STORE" ]]
 then
  S=`code_http "$1"`
  T=`code_http "$2"`
  D=`code_http "$3"`
  DA=$4
  TI=$5
  N=`basename $6`
  if [[ -n "$COMPRESS" ]]
   then
    RET=`$COMPRESS -c "$6" | httpget $SFLAGS -c -u logging -p lgput -s -mf - -mn $ULSHOSTNAME $ULSSERVER "put_test_blob$APPEND.s2w?fname=$ULSHOSTNAME&hostname=$ULSHOSTNAME&section=$S&teststep=$T&details=$D&access=$ACC&date=$DA&time=$TI&name=$N$COMPRESSEXT&charset=$CHARSET"`
  else
    RET=`httpget $SFLAGS -c -u logging -p lgput -s -mf "$6" -mn $ULSHOSTNAME $ULSSERVER "put_test_blob$APPEND.s2w?fname=$ULSHOSTNAME&hostname=$ULSHOSTNAME&section=$S&teststep=$T&details=$D&access=$ACC&date=$DA&time=$TI&name=$N&charset=$CHARSET"`
  fi
  if [[ "$RET" = "OK" ]]
   then
    if [[ -n "$REMOVE" ]]
     then
      rm "$6"
    fi
    exit 0
  fi
fi
#
if [[ -z "$ULS_TMP_PATH" ]]
 then
  ULS_TMP_PATH=/var/tmp/uls
fi
umask 077
S="`echo \"$1\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
T="`echo \"$2\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
D="`echo \"$3\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
if [[ -n "$FILENAME" ]]
 then
  N="`echo \"$FILENAME\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
else
  N="`basename \"$6\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
fi
if [[ -n "$COMPRESS" ]]
 then
  $COMPRESS -c "$6" >`dirname $ULS_TMP_PATH`/ULSTMP.$$
else
  cp "$6" `dirname $ULS_TMP_PATH`/ULSTMP.$$
fi
ULSDAT=dat-`id -u`.uls
if cd "$ULS_TMP_PATH" 2>/dev/null
 then
  test -f $ULSDAT || touch $ULSDAT && chmod 600 $ULSDAT
else
  mkdir -m 1777 -p "$ULS_TMP_PATH"
  if cd "$ULS_TMP_PATH" 2>/dev/null
   then
    touch $ULSDAT && chmod 600 $ULSDAT
  else
    echo "Kann Lokales ULS-Verzeichnis: $ULS_TMP_PATH nicht anlegen!"
    exit 1
  fi
fi
#
mv `dirname $ULS_TMP_PATH`/ULSTMP.$$ "$N.$$"
echo "L;;;;;;$CHARSET
F$APP;$4 $5;$ULSHOSTNAME;$S;$T;$D;$N.$$;$N$COMPRESSEXT;$ACC" >>$ULSDAT
if [[ -n "$REMOVE" ]]
 then
  rm "$6"
fi
exit 0
