#!/bin/bash
#===============================================================================
#
#          FILE: pg_error_log
#
#         USAGE: pg_error_log
#
#   DESCRIPTION: If your username is not root(UID=0), assume you cannot list 
#                files in the PostgreSQL log directory(pg_log), and you cannot 
#                read the log files in the directory.
#
#                So here it is, a C-Function for PostgreSQL. This function will
#                run by user postgres, and the function will run a system call 
#                chmod() to change the directories and files permission.
#
#                C-Function Usage:
#                SELECT pg_chmod('mode', 'directory or file name');
#
#                mode - numeric only(octal)
#
#
#  REQUIREMENTS: postgresql-devel, gcc, highlight
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Fri Jan 25 11:39:02 JST 2013
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                       # Enable job control

[ -e ~/.gamerc ] && eval $(grep "^HOST_NAME=" ~/.gamerc)

# Trying to get the path of PostgreSQL data directory
PGDATA="$(awk -F= '/^PGDATA=/ {print $2}' /etc/init.d/postgre* 2> /dev/null)"

# Reading the PostgreSQL log file
read_log () {

    # Timestamp as a key
    TIMESTAMP="$(date +%s)"

    # Has any error?
    cat $PGDATA/pg_log/$LOG_FILE|egrep -q "ERROR:|WARNING:" && HAS_ERROR="1"

    # Print the hostname once is there is error
    if [ "$HAS_ERROR" == "1" ] && [ "$HOSTNAME_PRINTED" != "1" ] ; then
        # Show the hostname even you are not using Ctrl
        [ "$HOST_NAME" == "" ] \
        && sed 's/\(.*\)/\x1b\x5b40\x3b33\x3b\x31\x6d\1\x1b\x5b\x30\x6d/g' <<< "PostgreSQL Error log on $HOSTNAME: " \
        || sed 's/\(.*\)/\x1b\x5b40\x3b33\x3b\x31\x6d\1\x1b\x5b\x30\x6d/g' <<< "PostgreSQL Error log on $HOST_NAME: "
        HOSTNAME_PRINTED="1"
    fi

    # READ THE LOG_FILE
    cat $PGDATA/pg_log/$LOG_FILE | egrep -A1 "ERROR:|WARNING:" | grep -v "^--$" > /tmp/pg_error_log.$TIMESTAMP

    # Filter "ERROR: ...." by pattern
    cat $(dirname $0)/pg_error_log.ignore_error | while read PATTERN ; do
	sed -n -i -e "/ERROR.*$PATTERN/"'{n;d};x;1d;p;${x;p}' /tmp/pg_error_log.$TIMESTAMP
	sed -n -i -e "/$PATTERN.*ERROR/"'{n;d};x;1d;p;${x;p}' /tmp/pg_error_log.$TIMESTAMP
    done

    # Filter "STATEMENT: ...." by pattern
    cat $(dirname $0)/pg_error_log.ignore_statement | while read PATTERN ; do
	sed -n -i -e "/STATEMENT.*$PATTERN/"'{n;x;d};x;1d;p;${x;p}' /tmp/pg_error_log.$TIMESTAMP
	sed -n -i -e "/$PATTERN.*STATEMENT/"'{n;x;d};x;1d;p;${x;p}' /tmp/pg_error_log.$TIMESTAMP
    done

    # Colorize by highlight
    ~/bin/highlight /tmp/pg_error_log.$TIMESTAMP "relation .* does not exist" "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]" \
	"STATEMENT" "syntax error" "DB:[0-9A-Za-z_]+"  "WARNING" "ERROR" "INSERT" "UPDATE"  "duplicate key value violates unique constraint" \
        "SELECT" "DELETE" "TRUNCATE" $(
            cat $PGDATA/pg_log/$LOG_FILE|egrep -A1 "ERROR:|WARNING:"|awk -F"SESS:" '{print $2}'|\
            awk -F: '/^[0-9a-f]/ {print $1}'|uniq|tail -8|awk '{printf ""$1" "}'
        )

    rm -f /tmp/pg_error_log.$TIMESTAMP

}

if [ "$UID" == "0" ] ; then
    # Read only lastest two files
    for LOG_FILE in $(ls -tr $PGDATA/pg_log|tail -2);do
        read_log
    done
else
    if ! psql -U postgres <<< "\df" | grep -q pg_chmod ; then
        cat > ~/pg_chmod.c << __EOF__
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "postgres.h"
#include "fmgr.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

// For PostgreSQL 8.2+
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(pg_chmod);


void
pg_chmod(PG_FUNCTION_ARGS)
{
    text *mode_str = PG_GETARG_TEXT_P(0);
    text *target = PG_GETARG_TEXT_P(1);
    mode_t mode;

    int result = 0;
    //fprintf(stderr, "mode_str->vl_dat at %p, side = %u\n", mode_str->vl_dat, VARSIZE(mode_str));
    //fprintf(stderr, "%s\n", (char *)(mode_str->vl_dat));
    mode = strtol((char *)(mode_str->vl_dat), NULL, 8);
    if(mode == 0 && (errno == EINVAL || errno == ERANGE)){
      result = 1;
      fprintf(stderr, "%s: wrong mode %s\n", __func__, (char *)(mode_str->vl_dat));
    }else{
      fprintf(stderr, "%s: %o %s\n", __func__, mode, (char *)(target->vl_dat));
      mode_t last_mask = umask(0000);
      if((result = chmod((char *)(target->vl_dat), mode)) < 0){
        fprintf(stderr, "%s: chmod failed, %s\n", __func__, strerror(errno));
      }
      umask(last_mask);
    }
}
__EOF__

        # Trying to get the full path of pg_config
        PG_CONFIG="$(rpm -ql $(rpm -qa | grep postgresql | grep -v debuginfo)|grep "/bin/pg_config$")"

        # If pg_config not found, exit right now
        if [ "$PG_CONFIG" == "" ] ; then 

            echo "Error: pg_config not found, you need to install postgresql-devel first."

        else

            # Compiling...
            gcc -Wall -I `$PG_CONFIG --includedir-server` -fpic -c ~/pg_chmod.c
            gcc -shared -o ~/.pg_chmod.so ~/pg_chmod.o

            # Success to complile?
            [ -e ~/.pg_chmod.so ] && echo Compiled successfully || echo Compile failed. Use pre-built shared library.

            # Create the C-function
            chmod 755 $PWD
            psql -U postgres <<< "CREATE OR REPLACE FUNCTION pg_chmod(varchar(4),varchar(256)) RETURNS VOID AS '$PWD/.pg_chmod', 'pg_chmod' LANGUAGE C STRICT;"

        fi # if [ "$PG_CONFIG" == "" ] ; then

    fi # if ! psql -U postgres <<< "\df" | grep -q pg_chmod ; then


    # If we cannot compile a shared library file, use pre-compiled shared library instead.
    if ! [ -e ~/.pg_chmod.so ] ; then
        POSTGRESQL_VERSION="$(psql --version|awk '/^psql / {print $NF}'|cut -c 1-3)"
        PSQL_ARCHITECTURE="$(file `which psql`|awk -F" ELF " '{print $2}'|awk -F- '{print $1}'|grep "^[0-9]\+$")" \
        || PSQL_ARCHITECTURE="$(getconf LONG_BIT)" # No "file" command? try to get the architecture from the kernel

        if [ "$POSTGRESQL_VERSION" == "8.4" ] ; then
            if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
                # 8.4 32 GF-DE
                # .pg_chmod.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
                cat << __EOF__ | base64 -i -d | gzip -d -c > ~/.pg_chmod.so
H4sIAArkCVECA9VYb2wUxxWf3TuwDcaxiQ1u6qSn6JBMIVdDjG3oP4N9BiLzzzZpCDWb9d3Zd83d7eV2D+yqpHE3bWJdnVJo1T9SFbdI9ENSyVINTRtUmRyKg8QHi1KJD4RaLYnWlZsYya2shnB9b2
Z2b/Zsf2i/dU/v3vzevHlvZvbNzM77drCzQ5IkYj8e+CEa8xLSiHwNkzcSH/GSevII2UgK2uy5WcJoFYJKQuupbCOjWhD4gFbzehnYHNTPbWQ0SRhhPbglRz4wwmSZx673AJUTZgdl13xjErnaWoay
CqD1QH/429lvtQX/PPe9I55Lt154pq788M+u/H70XsXf//RxxdS6CzeLbZ/i/FHe/hccX+JYHLMPaEsRPsbLX+b6KY47OP4JxzUcv8oxjqcK6Acc+3l9HcfPguPHgSuCP5hCkgNaqLXxvfw4zqmAnw
b+FryQ5yS0t4b8rqj+APBfQf0GwurR5oyH1a8npVTeU2LjteSrwL8v6BNFGUhoSUU31LShKESJJWMGUfqBQVVoUMWiGo99M0KUp04qXZGBmG5E0m1xVdcjOjk8oCTUgVhI6c8kQyQ1gNr9mgKFUDSh
hVESjhiaqhtKWDUyCaIbaUOLg+lIOp3UlLgWUo2YlgR5GCSkP5WOJY1+kkmo+vOoDEItTeKxvlBA1wJNRImAHRXa9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pMbjQmqChNKGbmT6+wMhuPy39R
zqUjr3d/dgxkJpd6Gn2hyA3yxKLDwY2NG880kCHyOpeMSIhBFuA82wpgzEtT41rtCPG0XNDBL60aOEM4nEkO0neLCdGu7o2n0g6CB0Y5cLhkKOIfsCAP11kiT0u6lxW3MTSQyxTAmglh1gAdMnigJo
ZxNR9nYe2rO7UznU0dEd7FF6du/pDFIvuqZE1WQ4HrHHbPuPNbU0BQYihpIKKUY0k3w+EBpcVtwH4vZjB3cf2N+2JAXT2lrIm9iJF1FWnChaJhvE0kZ02EXWDC0uSmh6x62C6R9RwjNBosi5U7mzTk
tyQTxHtDQb5U5oFUxj0mhJpoplwv4DXeqquFgVAAA=
__EOF__
            else # if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
                # 8.4 64 FN-FR
                # .pg_chmod.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
                cat << __EOF__ | base64 -i -d | gzip -d -c > ~/.pg_chmod.so
H4sIAOaaB1ECA9VZfUwcxxWfXY6PAwyHk9jUduJVZCqcjxOm4GC3dsFw9hIB/iQictrNch9wyd0tuduzIQ0NEqlVREjc/lNHalI3Up0q+SeKqnxYag0hre2qitykVa2qVVFSR0fjKrHkNlR1cn0zO7
PMzO0Z/qgqdU57b95v5s28fTvzZubNk6GevaqiIJZK0G6EudNlDt9O8YoatwpgbagU/jeielK3FBVPt1aKFCGN/GM53EU9xesrNYE2qg7epopyKpXTqZxO6zM6QV+F0QoqfeCKHfEBHfE7vEy3IJH6
KD0IcmVo9anCtSNCa7j3vLigKcg368dYLTy3wHP2w+8/0Rn6/dUTB0tev/zYwKbqA8/NvTVzreZv739Sc37Nmd+t1FcJ9PapZPwH4VnL8Q2UKq4FxXR3EXwTPHdy/JepPZ+X2n+L0hEJZ/njEl5H6X
e5PG+3Z6X6Jyn9gYSz+gY8AQ/95wl+LX+gWny7VyneLuEPkPYr0Ylyh99J3/eNIvV7af0xWn8NxQMEL/wu6whehZYqRPzrtJ2E1A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ozfQnvWEV99Le8VH1U8
7HAXwQvt0EbwQjuECL6sJ/tshymuV4vnq0Gq56yk5yjF28tXp/9TVH9N0ud7BK9BgUqx/o+K6P8K00cab3NF+v0NaWd5HG6i+F8ofo9fXB//TvEmv3gO+zfG1Wt5+bvXKRgvHFd3EDyAmiT8bsVpRx
5vLYp3HH2f4h0vf6AI/ojiHfcdUbzj608r3vH1FxXv+Prrinfc+pziHXf/s+Idb/5Y8Y5zf6Z4x9FvKN7x+4Dq3c561TtevlX1jvfvVr3j/QdV73j/oOodd0dhM5HgQukonLYzdjYWC4aRYXQe2X/I
6Ok+fARH2I0ugbu/02Xw+cqIR0aD27e13Ifg4DSSiNrRCGZboWbEMoYS1qCZMMhBzDCzo8g5rkWyyeQY6yfU10Ua3nuoozfkcrgbll9uKOw2xAJOoK8b1CdnvJbWbTtQcsyJ7APX3AYt4HC/YQD3lR
3I2Nezf09Hj7F/797DoSPGkY49PSHSS8Yyhs1UJBFl70z773qwr6O3uxOR/trb+dC9fBvhceXgdTchNsKuDQpQ20qImBtlk+8jxGriLUfB3YNzdyG9iHNPUXgjQm84xNrkuqNANfHmxLmZ+Q9Gqa/2
SB0AAA==
__EOF__
            fi # if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
        elif [ "$POSTGRESQL_VERSION" == "9.1" ] ; then
            if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
                # 9.1 32 GLOBAL
                # .pg_chmod.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
                cat << __EOF__ | base64 -i -d | gzip -d -c > ~/.pg_chmod.so
H4sIAANjB1ECA9VYb0wcxxWf3TsbsDEBB2yakvQUYQnXzpUQsMH9h4HDdoT/AU7juHiz3B3cNXe3l9s9G6o6Dd38Q1dS127VP1IVWkvuh6QSUrGbNlaFc1aIJX9Ariv5g+Oi1omWiiZYohVqHF/fm5
ndmz3gQ/ute3r35vfmzXszs29mdt53A12dkiQR+/HAD9G4l5BG5OuYvJH4iJfUkYfIZp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dmdaSg5mBZjl/tyV8nTLMRt3qYHqD
pWuOYXpjpWuLYXYhmnUwm8ElB5dQPu61MTuS7XuUh+48LMYZLmVjdfAGdp/ebOMyym85+AHK7zi43DnbGa5wnYseehKz+GH4QcovObiS6Tu4yjWvHrjtScJ4ZBhPhStm7uZqhP5K0N8vCv2VoL/NQn
8l6G+A+/NSXEV68HugmvC7VBXpx/kQ/A0BP7Rmdf8vAb8p+P+hMJ8SzOcbBf7fxO+uory9ywX2rvH3U0P1S8lf8N5bZPe3lPwD+OkiFq+IPxXmU5Lv5iqk/PuQ5AryCOBZBz9AtgGutv3L60gb4FfX
5PFRKf/+0F6/5M5LvCC58xJnJHce4pcF+ILkvse/I7nzFNckd57ituTOU3wiufMUkuzOU1TK7jzDw7I7T7BNducddsruvMVXCurbZXde4ojszoM8X3DWfE925y1IUI3FhNQECaYM3UgPDPiDcPlv7z
3YrXTt6+nFjIXS4UJPtjsAv1mUaGjI37Sz5QkCHyPJWNgIhxA+DpohTRmMaf1qTKEfN4qaHiL0o0cJpePxYdtP4EAHNdzZvXt/wEHoxi7nDQUdQ/YFAPrrJEnod1NjQ2MLiQ+zTAmgpmawgOkTRQG0
o4Uoe7oOtu3uUg52dvYEepXe3W1dAepF15SImgjFwvaYbf/RHc07/INhQ0kGFSOSTjznDw6tKO4HccfRA7v372tfloJpbc3nTezEiygrTBStkA1iaSM67AJrhhYTJTS941bB9I8o4ZkgUeTcqdxZp2
W5IJ4jWp6Ncie08qYxabQsU8UyYf8BatqIM1gVAAA=
__EOF__
        else # if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
                # 9.1 64 LH-JP
                # .pg_chmod.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
                cat << __EOF__ | base64 -i -d | gzip -d -c > ~/.pg_chmod.so
H4sIAH5sB1ECA81ZfWwcxRWfXZ8/znbsS4DETQJZIVw5fJxsyw4mbVJ/XbJGdj5tZBTaZX0f9sHdrbnbS2xKiiVDVOtqSPtPg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F__
            fi # if [ "$PSQL_ARCHITECTURE" == "32" ] ; then
        elif [ "$POSTGRESQL_VERSION" == "9.2" ] ; then # elif [ "$POSTGRESQL_VERSION" == "9.1" ] ; then
            if [ "$PSQL_ARCHITECTURE" == "64" ] ; then
                # 9.2 64 LH-KR
                # .pg_chmod.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped
                cat << __EOF__ | base64 -i -d | gzip -d -c > ~/.pg_chmod.so
H4sIALHkJVECA71Ze2wUxxmfvYexzfkB4WEeLSsaS5CEq2MMgSQkNvbhhRowYCgpVKv1PXxH727d273EjiBFPYpwXTdu+kfVP9qg/kH6kCoqRYmbqokpKNgVjYhCVbdSU1eB6BxIgapt3KTk+s3szH
p2vIv5o+qc9r79fjPfY2Znvp359muRjm0+SUKs+NETiHDzLL6ZVVTbTQDbhMrgfyVaRtoGkXd5udJJEZLJP5Yrg2uE4iOVsoOu8Vn4Jp9TzkflilSuSNszepx2hdFyKh2g1zDFRXo/ctIApZ3XzBi+
D1F7IvWS2wNyZejeC+/nIvIcEGrftR8tXl/XPH34UOSjmsde/dzNL+1YvaazgGh9FTeO45OyhAKjFRirges+uF5774WjrZHf3zi5x//KxFcPrgx1fv/cL4duV3/wzs3qi1Vnrszlkx9VoE6hE0/Btd
ClrWQ/IWd50ANfCddqF/wHHvp7PfBnPPBTcC1wwZ/3aP89D1yFq9YFP0/w26VbIWfvzlJ8UsAPEP3z0ft0XSXovHvVo/1O2v6PtH0VxWsJPvu5LCF4CJ2qcOJPUj2XBD1IVXsyelY1TC1nqipSU9mU
idQEEKiK9mn4Vkunno0jdcfT6t54T8ow47nWtGYYcQN19qgZrScVVRP5bBT19uDWCV2Fm2gyo8cwEoubumaYakwz8xlkmDlTT4PqeC6X1dW0HtXMlJ4FPAYISvTmUlkzgfIZzfgKbgygnkPpVHc0bO
jhjUiNgx4N5LsNw3IaoGwMtXds39qqNoYbwxvs+ID/+R+enc6fhZzhnm1+eaoCy41RfpLGk3KB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LpcF/EHJ0iPOtybJPb/cLrnnxw944Eck9zxvr+SeT/+Wh90feeCvSO55
6jc82v9Zcs8vX5fc89ofSe55c5yncsvX1/rc9Sz1uefH1/rc/XzCA9/jc8/vd/vc8+woqqXTXOocRXOmYeYTiXAUqWpr1+69asf2fV04o662ObgdrRwT09WetN6tpVVyhFO1fB+Cc1lvOm7GY+GN65
seQbhCTcX6MLcZWafAWD6T6Wd2IrvaiK5te1t2RmwOm2H3M1aithWWHAJ/7SQ+OTo2bWzcjDL9ViYfuPWPgAac3ldV4MAhtb1j99aWDnX3tm37Il1qV8vWjgixYuhqUsvG0nHWZ2q/7aldLTu3tyJi
r7mZT9WLXx9cPjG4fYtwKmGfCWahpp52YnZGTPz+4Gzm/Kox61uD9a1C6Ij1XWL2FxD6RcPZmnzemOWa80uJ9SXmv42plNaYHQAA
__EOF__
            fi
        fi # elif [ "$POSTGRESQL_VERSION" == "9.2" ] ; then

        # Create the C-function
        chmod 755 $PWD
        psql -U postgres <<< "CREATE OR REPLACE FUNCTION pg_chmod(varchar(4),varchar(256)) RETURNS VOID AS '$PWD/.pg_chmod', 'pg_chmod' LANGUAGE C STRICT;"

    fi # if ! [ -e ~/.pg_chmod.so ] ; then

    # pg_chmod() still not found, maybe failed to compile or create function
    if ! psql -U postgres <<< "\df" | grep -q pg_chmod ; then
        exec echo "Error: Failed to create C-Function pg_chmod()"
    fi

    # Change the access permission of directories
    awk -F/ '{for(i=1;i<=NF;i++){printf "psql -U postgres <<< \"select pg_chmod(\x27"'755'"\x27, \x27";for(j=1;j<=i;j++){printf $j"/"}printf "\x27);\"\n"}}' <<< $PGDATA \
    | grep /pgsql | sh 2>&1 > /dev/null
    psql -U postgres <<< "select pg_chmod('755', '$PGDATA/pg_log');" 2>&1 > /dev/null

    # Read only lastest two files
    for LOG_FILE in $(ls -tr $PGDATA/pg_log|tail -2);do

        # If you are not root, call pg_chmod once to change the access permission
        psql -U postgres <<< "select pg_chmod('644', '$PGDATA/pg_log/$LOG_FILE');" 2>&1 > /dev/null

        # Read log files
	read_log

        # Change the access permission back
        psql -U postgres <<< "select pg_chmod('600', '$PGDATA/pg_log/$LOG_FILE');" 2>&1 > /dev/null

    done

    # After read, we have to change the access permission of directories back to 700
    # Otherwise, the postmaster WILL NOT START
    psql -U postgres <<< "select pg_chmod('700', '$PGDATA/pg_log');" 2>&1 > /dev/null
    awk -F/ '{for(i=4;i<=NF;i++){printf "psql -U postgres <<< \"select pg_chmod(\x27"'700'"\x27, \x27";for(j=1;j<=i;j++){printf $j"/"}printf "\x27);\"\n"}}' <<< $PGDATA | sh 2>&1 > /dev/null

    # Add a newline
    echo

fi # if [ "$UID" == "0" ] ; then
