#!/bin/bash
#
# send_section_remark
#
# Speichert Bemerkungen zu logginginformationen in der Loggingdatenbank ber einen
# httpget-Aufruf
#
# Aufruf:
#
# send_section_remark [-c charset] [-d storepath] [-S] [-h hostname] section remark
#
###################################################################################
#
if [[ -f /etc/uls/uls.conf ]]
 then
  . /etc/uls/uls.conf
fi
#
if [[ -z "$ULSSERVER" ]]
 then
  ULSSERVER="STORE"
fi
#
ULSSEND="STORE"
if [[ ${LC_CTYPE:-$LANG} == *8 ]]
 then
  CHARSET="utf8"
else
  CHARSET="latin1"
fi
#
while getopts c:d:Sh:? op
 do
  case "$op" in
    c) CHARSET="$OPTARG";;
    d) ULS_TMP_PATH="$OPTARG";;
    S) ULSSEND="$ULSSERVER";;
    h) ULSHOSTNAME=$OPTARG;;
    *) echo "usage: $0 [-c charset] [-d storepath] [-S] [-h hostname] section remark"
       exit 1;;
  esac
done
shift $(($OPTIND - 1))
#
#
if [[ $# -ne 2 ]]
 then
  echo "usage: $0 [-c charset] [-d storepath] [-S] [-h hostname] section remark"
  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"`
  W=`code_http "$2"`
  RET=`httpget $SFLAGS -c -u logging -p lgput -s $ULSSERVER "put_section_remark.s2w?hostname=$ULSHOSTNAME&section=$S&charset=$CHARSET&remark=$W"`
  if [[ "$RET" = "OK" ]]
   then
    exit 0
  fi
fi
#
umask 077
#
ULSDAT=dat-`id -u`-$$.uls
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'`"
W="`echo \"$2\" | sed 's/\\([\"\\\\]\\)/\\\\\\1/g'`"
echo "L;;;;;;$CHARSET
R;;$ULSHOSTNAME;$S;;;\"$W\"" >>$ULSDAT
exit 0
