#!/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 [[ -n "$SET_ULSTMP_PATH" ]]
 then
  ULS_TMP_PATH="$SET_ULSTMP_PATH"
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="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME=$OPTARG;;
    s) ACC=$OPTARG;;
    *) echo "usage: $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: $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
  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
  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
