#!/bin/bash
#
# send_test_value
#
# Speichert logginginformationen in der Loggingdatenbank ueber einen httpget-Aufruf
#
# Aufruf:
#
# send_test_value [-c charset] [-d storepath] [-S] [-h hostname] [-n|-N HH:MM|-u]
#        [-s access] section teststep detail date time wert einheit
#
###################################################################################
#
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"
NODUP=""
ELAP=""
ACC="all"
MODE="V"
if [[ ${LC_CTYPE:-$LANG} == *8 ]]
 then
  CHARSET="utf8"
else
  CHARSET="latin1"
fi
#
while getopts c:d:Sh:nN:us:? op
 do
  case "$op" in
    c) CHARSET="$OPTARG";;
    d) ULS_TMP_PATH="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME=$OPTARG;;
    N) ELAP="&elapsed=$OPTARG"
       NODUP="_nodup"
       MODE="E$OPTARG";;
    n) NODUP="_nodup"
       MODE="N";;
    u) NODUP="_unique"
       MODE="U";;
    s) ACC=$OPTARG;;
    ?) echo "usage: $0 [-c charset] [-d storepath] [-S] [-h hostname] [-n|-N HH:MM|-u] [-s access] section teststep detail date time wert einheit"
       echo "  -c charset  : set characterset (lantin1/utf8)"
       echo "  -d storepath: store data in storepath and don't send to ULS"
       echo "  -S          : send immediately to ULS"
       echo "  -h hostname : set hostname"
       echo "  -n          : store only values which differs from previous values"
       echo "  -N HH:MM    : like -n when the old data is newer than HH:MM ago"
       echo "  -u          : store only if the values with the same timestamp doesn't exists"
       echo "  -s access   : set access-right"
       exit 0;;
    *) echo "usage: $0 [-c charset] [-d storepath] [-S] [-h hostname] [-n|-N HH:MM|-u] [-s access] section teststep detail date time wert einheit"
       exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#
if [[ $# -ne 7 ]]
 then
  echo "usage: $0 [-c charset] [-d storepath] [-S] [-h hostname] [-n|-N HH:MM|-u] [-s access] section teststep details date time wert einheit"
  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
  W=`code_http "$6"`
  E=`code_http "$7"`
  RET=`httpget $SFLAGS -c -u logging -p lgput -s $ULSSERVER "put_test_value$NODUP.s2w?hostname=$ULSHOSTNAME&section=$S&teststep=$T&details=$D&access=$ACC&date=$DA&time=$TI$ELAP&wert=$W&einheit=$E&charset=$CHARSET"`
  if [[ "$RET" = "OK" ]]
   then
    exit 0
  fi
fi
#
if [[ ${#6} -gt 120 ]]
 then
  ULSDAT=dat-`id -u`-$$.uls
else
  ULSDAT=dat-`id -u`.uls
fi
if [[ -z "$ULS_TMP_PATH" ]]
 then
  ULS_TMP_PATH=/var/tmp/uls
fi
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
#
S="`echo \"$1\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
T="`echo \"$2\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
D="`echo \"$3\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
W="`echo \"$6\" | sed 's/\\([\"\\\\]\\)/\\\\\\1/g'`"
E="`echo \"$7\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
echo "L;;;;;;$CHARSET;
$MODE;$4 $5;$ULSHOSTNAME;$S;$T;$D;\"$W\";$E;$ACC" >>$ULSDAT
exit 0
