#!/bin/sh

# vlpsql
# jorgericarte@asuni.com.mx
# Ultimo cambio: Tue 15 Mar 2011 10:25:37 PM MDT
# Thu Feb 10 10:30:25 MST 2011
VERSION="0.1"
# View log psql
# En /etc/postgresql/8.3/main/postgresql.conf
# # ------------------------------------------------------------------------------
# # ERROR REPORTING AND LOGGING
# #------------------------------------------------------------------------------
# # - Where to Log -
# #
# log_destination = 'stderr'                    # Valid values are combinations of
# logging_collector = on                        # Enable capturing of stderr and csvlog
# log_directory = '/var/log/postgresql'         # directory where log files are written,
# log_filename = 'postgresql-%Y-%m-%d.log'      # log file name pattern,
# log_statement = 'all'                         # none, ddl, mod, all
VERSION="0.2"
# Tue Mar 15 20:19:31 MDT 2011
# leer /etc/postgresql/8.3/main/postgresql.conf para saber que log esta generando
# log_destination = 'stderr'
# log_destination = 'csvlog'

FPSQL="/etc/postgresql/8.3/main/postgresql.conf"
FPSQL="/etc/postgresql/8.4/main/postgresql.conf"
FPSQL="/etc/postgresql/9.1/main/postgresql.conf"
FAWK="/var/www/ficnx/psql/vlpsql.awk"
PAWK="/usr/bin/awk"
PGREP="/bin/grep"
LOGDIR="/var/log/postgresql/"
MFECHA=`date +%Y-%m-%d`

if [ "$1" = "-h" ]; then
  echo "vlpsql $VERSION"
  echo "Usa: $0 "
  echo "  Para  Ver log "
  echo "Usa: $0 [--purge]"
  echo "  Para Eliminar logs"
  exit 1
fi

KLOG=`$PGREP -v ^[#] $FPSQL | $PAWK -f $FAWK`
MFECHA="postgresql-"$MFECHA$KLOG
echo $MFECHA

if [ "$1" = "--purge" ]; then
  MFECANT=`date -d '1 day ago' +%Y-%m-%d`
  for FILE in $LOGDIR*
    do
      if [ "$LOGDIR$MFECHA" = "$FILE" ]; then
        echo $FILE $MFECANT "ES HOY"
      else
        rm $FILE
      fi
    done
fi

/usr/bin/tail -f $LOGDIR$MFECHA

