#!/bin/bash

if [  -f /etc/default/pg_backup ] ; then
    . /etc/default/pg_backup
else
    PG_SERVICE=postgresql
    USE_NAGIOS=no
    LOG_DIR=/var/log/postgresql
    LOG_FILE=$LOG_DIR/postgresql-backup.log
    PG_BCK_BIN=/usr/local/sbin/postgresql-backup
fi

export PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH"
PG_SVC=$( service $PG_SERVICE status >/dev/null )
PG_RUNNING=$?

if [  ! -d $LOG_DIR  ] ; then
    mkdir -p $LOG_DIR
fi

if [ "$PG_RUNNING" -ne "0" -a "$PG_RUNNING" -ne "3" ] ; then
    echo "postgresql not running" > $LOG_FILE
    exit 1
else
    $PG_BCK_BIN > $LOG_FILE 2>&1
fi
if [ "${USE_NAGIOS}" == "yes" ] ; then
    N_LOGDIR=/var/log/nagios-checks
    if [  ! -d $N_LOGDIR  ] ; then
	mkdir -p $N_LOGDIR
    fi
fi

exit 0