#!/bin/sh

################################ Configuration ################################

# Description de la base 


# Chemin d'accs aux binaires postgres
export PGBIN=/usr/bin

export PSQL=$PGBIN/psql

export PG_DROPDB=$PGBIN/dropdb
export PG_CREATEDB=$PGBIN/createdb

# Cygwin does not have the create/dropuser command : use the Windows ones

export PG_CREATEUSER=/cygdrive/d/Postgresql/bin/createuser
export PG_DROPUSER=/cygdrive/d/Postgresql/bin/dropuser


# Nom du super utilisateur Postgres (pour la creation de la base et des roles)
export PGADMIN=postgres

# Description des parametres de la base de donnes
export PGUSER=pabx
export PGDB=pabx

export PGHOST=127.0.0.1
export PGPORT=5432


# PATH par dfaut (surtout utile pour les jobs en tche planifie)
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin
# Note : /usr/X11R6/bin is needed for Gnuplot

# Rpertoire de cette application
APPLI=_PATH_TO_INSTALL_
DATA=$APPLI/raw
BIN=$APPLI/bin
ETC=$APPLI/etc

# TESTS

#export DATA=$APPLI/raw.short
#export PGUSER=pabx.short
#export PGDB=pabx.short

export DBDESC="hostaddr='$PGHOST' port='$PGPORT' dbname='$PGDB' user='$PGUSER' password=''"

###############################################################################


# Execute une requete SQL sur la base
function SqlQuery()
{
   $PSQL -h $PGHOST -qtA '' -F '	' -U $PGUSER  -c "`echo \"$@;\" | sed 's/;;/;/g'`" -d $PGDB  | dos2unix
}

function HtmlSqlQuery()
{
   $PSQL -h $PGHOST --html -U $PGUSER  -c "`echo \"$@;\" | sed 's/;;/;/g'`" -d $PGDB  | cat
}

function SuperQuery()
{
   (
      export PGUSER=$PGADMIN
      SqlQuery "$@"
   )
}

function CreateAndRegisterIndex()
{
   (
      TABLE="$1"
      NAME="$2"
      TYPE="$3"


      ID=`SqlQuery "insert into meta_indexes values ( DEFAULT, '$TABLE', '$NAME', '$TYPE' ); select last_value from meta_indexes_id_seq;"`

      echo -n "   - Creating index $NAME ($ID) on $TABLE... "

      shift
      shift
      shift

      (
         echo -n "create index i_$TABLE""_$ID on $TABLE using $TYPE ("
         COMMA=""
   
         for col in $*
         do
            echo -n "$COMMA "
	    echo -n $col
	    COMMA=','
         done

         echo ");"

         for col in $*
         do
            echo "insert into meta_indexesdesc values ( DEFAULT, $ID, '$col' );"
         done
      ) | $PSQL -qtA '' -F '	' -U $PGUSER -d $PGDB

      echo "Done !"
   )
}

function FindIndex()
{
   (
      NAME=$1

      SqlQuery "select id from meta_indexes where name = '$NAME';"
   )
}

function CutFile()
{
   (
      FILE=$1
      START=$2
      END=$3

      if [ "$FILE" = "" ]
      then
	 return;
      fi

      if [ ! -f "$FILE" ]
      then
	 echo "Model $FILE does not exists..." >&2
      fi

      if [ "$START" = "" -a "$END" = "" ]
      then
	 cat $FILE
	 return
      fi

      if [ "$END" = "" ]
      then
	 cat $FILE | awk "
BEGIN	{ OUT=0 }
        { if ( OUT == 1 ) print \$0 }
/$START/ { OUT=1 }"
	 return
      fi

      if [ "$START" = "" ]
      then
	 cat $FILE | awk "
BEGIN	{ OUT=1 }
/$END/{ OUT=0 }
        { if ( OUT == 1 ) print \$0 }"
	 return
      fi

      cat $FILE | awk "
BEGIN	{ OUT=0 }
/$END/	{ OUT=0 }
        { if ( OUT == 1 ) print \$0 }
/$START/{ OUT=1 }"


   )
}

function GetValueFromQueryString()
{
   echo "$QUERY_STRING" |tr '&' '\n' | grep "^$1=" | cut -f2- -d= | tr '+' ' ' | sed 's/%3A/:/g'
}
