#!/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 [[ -z "$ULSSERVER" ]]; then
  ULSSERVER="STORE"
fi
#
ULSSEND='STORE'
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=$(readlink -f "$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: `basename $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: `basename $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: `basename $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
  S=$(sed 's/?/%3f/g;s/#/%23/g;s/!/%21/g;s/\$/%24/g;s/&/%26/g;s/'"'"'/%27/g;s/(/%28/g;s/)/%29/g;s/\*/%2a/g;s/+/%2b/g;s/,/%2c/g;s/=/%3d/g;s/ /%20/g' <<<"$1")
  T=$(sed 's/?/%3f/g;s/#/%23/g;s/!/%21/g;s/\$/%24/g;s/&/%26/g;s/'"'"'/%27/g;s/(/%28/g;s/)/%29/g;s/\*/%2a/g;s/+/%2b/g;s/,/%2c/g;s/=/%3d/g;s/ /%20/g' <<<"$2")
  D=$(sed 's/?/%3f/g;s/#/%23/g;s/!/%21/g;s/\$/%24/g;s/&/%26/g;s/'"'"'/%27/g;s/(/%28/g;s/)/%29/g;s/\*/%2a/g;s/+/%2b/g;s/,/%2c/g;s/=/%3d/g;s/ /%20/g' <<<"$3")
  DA=$4
  TI=$5
  W=$(sed 's/?/%3f/g;s/#/%23/g;s/!/%21/g;s/\$/%24/g;s/&/%26/g;s/'"'"'/%27/g;s/(/%28/g;s/)/%29/g;s/\*/%2a/g;s/+/%2b/g;s/,/%2c/g;s/=/%3d/g;s/ /%20/g' <<<"$6" | sed ':a;N;$!ba;s/\n/%0a/g')
  E=$(sed 's/?/%3f/g;s/#/%23/g;s/!/%21/g;s/\$/%24/g;s/&/%26/g;s/'"'"'/%27/g;s/(/%28/g;s/)/%29/g;s/\*/%2a/g;s/+/%2b/g;s/,/%2c/g;s/=/%3d/g;s/ /%20/g' <<<"$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
