#!/bin/ksh
#
# send_test_doku
#
# Speichert Dokumentation zu logginginformationen in der Loggingdatenbank ber einen
# httpget-Aufruf
#
# Aufruf:
#
# send_test_doku [-d storepath] [-S] [-h hostname] section teststep doku
#
###################################################################################
#
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"
#
while getopts d:Sh:nN:us:? op
 do
  case "$op" in
    d) LOKALER_TEST_PFAD="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME=$OPTARG;;
    *) echo "usage: $0 [-d storepath] [-S] [-h hostname] section teststep doku"
       exit 1;;
  esac
done
shift $(($OPTIND - 1))
#
#
if [ $# -ne 3 ]
 then
  echo "usage: $0 [-d storepath] [-S] [-h hostname] section teststep doku"
  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"`
  W=`/usr/local/bin/code_http "$3"`
  RET=`/usr/local/bin/httpget $SFLAGS -c -u logging -p lgput -s $ULSSERVER "put_test_doku.s2w?hostname=$ULSHOSTNAME&section=$S&teststep=$T&doku=$W"`
  if [ "$RET" = "OK" ]
   then
    exit 0
  fi
fi
#
umask 077
#
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
#
S="`echo \"$1\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
T="`echo \"$2\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
W="`echo \"$3\" | sed 's/\\([\"\\\\]\\)/\\\\\\1/g'`"
echo "T;;$ULSHOSTNAME;$S;$T;;\"$W\"" >>$ULSDAT
exit 0
