#!/bin/ksh
#
# send_file_value
#
# Speichert logginginformationen in der Loggingdatenbank ueber einen httpget-Aufruf
#
# Aufruf:
#
# send_file_value [-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 [ -z "$ULSHOSTNAME" ]
 then
  ULSHOSTNAME=`hostname`
fi
#
if [ -z "$ULSSERVER" ]
 then
  ULSSERVER="STORE"
fi
#
ULSSEND="STORE"
ACC="all"
APPEND=""
APP=""
#
while getopts d:Sh:s:a? op
 do
  case "$op" in
    d) LOKALER_TEST_PFAD="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME="$OPTARG";;
    s) ACC=$OPTARG;;
    a) APPEND=_append
       APP=A;;
    *) echo "usage: $0 [-d storepath] [-S] [-h hostname] [-s access] [-a] section teststep date time <path>"
       exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#
#
if [ $# -ne 6 ]
 then
  echo "usage: $0 [-d storepath] [-S] [-h hostname] [-s access] [-a] section teststep details date time <path>"
  exit 1
fi
#
if [ "$ULSSEND" != "STORE" ]
 then
  if STATUS=`/usr/local/bin/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=`/usr/local/bin/code_http "$1"`
  T=`/usr/local/bin/code_http "$2"`
  D=`/usr/local/bin/code_http "$3"`
  DA=$4
  TI=$5
  N=`basename $6`
  RET=`/usr/local/bin/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"`
  if [ "$RET" = "OK" ]
   then
    exit 0
  fi
fi
#
umask 077
S="`echo \"$1\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
T="`echo \"$2\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
D="`echo \"$3\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
N="`basename \"$6\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
cp "$6" /tmp/ULSTMP.$$
ULSDAT=dat-`id -u`.uls
if [ -z "$LOKALER_TEST_PFAD" ]
 then
  LOKALER_TEST_PFAD=/tmp/uls
fi
if cd "$LOKALER_TEST_PFAD" 2>/dev/null
 then
  test -f $ULSDAT || touch $ULSDAT && chmod 600 $ULSDAT
else
  mkdir -m 1777 -p "$LOKALER_TEST_PFAD"
  if cd "$LOKALER_TEST_PFAD" 2>/dev/null
   then
    touch $ULSDAT && chmod 600 $ULSDAT
  else
    echo "Kann Lokales ULS-Verzeichnis: $LOKALER_TEST_PFAD nicht anlegen!"
    exit 1
  fi
fi
#
mv /tmp/ULSTMP.$$ $N.$$
echo "F$APP;$4 $5;$ULSHOSTNAME;$S;$T;$D;$N.$$;$N;$ACC" >>$ULSDAT
exit 0
