#!/bin/bash
#
# send_image_value
#
# Speichert logginginformationen in der Loggingdatenbank ueber einen httpget-Aufruf
#
# Aufruf:
#
# send_image_value [-c charset] [-d storepath] [-S] [-h host] [-s acc] section teststep details date time [<Datei>]
#
###################################################################################
#
if [[ -f /etc/uls/uls.conf ]]; then
  . /etc/uls/uls.conf
fi
#
if [[ -z "$ULSSERVER" ]]; then
  ULSSERVER="STORE"
fi
#
ULSSEND="STORE"
ACC="all"
if [[ ${LC_CTYPE:-$LANG} == *8 ]]; then
  CHARSET="utf8"
else
  CHARSET="latin1"
fi
#
while getopts c:d:Sh:s:? op; do
  case "$op" in
    c) CHARSET="$OPTARG";;
    d) ULS_TMP_PATH=$(readlink -f "$OPTARG");;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME=$OPTARG;;
    s) ACC=$OPTARG;;
    *) echo "usage: `basename $0` [-c charset] [-d storepath] [-S] [-h hostname] [-s access] section teststep date time <path>"
       exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#
if [[ $# -ne 6 ]]; then
  echo "usage: `basename $0` [-c charset] [-d storepath] [-S] [-h hostname] [-s access] section teststep details date time <path>"
  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
  N=$(basename "$6")
  RET=`httpget $SFLAGS -c -u logging -p lgput -s -mf "$6" -mn $ULSHOSTNAME $ULSSERVER "put_test_image.s2w?fname=$ULSHOSTNAME&hostname=$ULSHOSTNAME&section=$S&teststep=$T&details=$D&access=$ACC&date=$DA&time=$TI&name=$N&charset=$CHARSET"`
  if [[ "$RET" = "OK" ]]; then
    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'`"
N="`basename \"$6\" | sed 's/\\([\"\\;\\\\]\\)/\\\\\\1/g'`"
cp "$6" `dirname $ULS_TMP_PATH`/ULSTMP.$$
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
I;$4 $5;$ULSHOSTNAME;$S;$T;$D;$N.$$;$N;$ACC" >>$ULSDAT
exit 0
