#!/bin/bash
UBUK_BDNAME=ubuk
SERVER=localhost
UBUK_BDUSER=db_report

DAYS=$(psql -At ${UBUK_BDNAME?} -h ${SERVER?} -U ${UBUK_BDUSER?} -c "select  sf_get_one_local_conf(34)::integer")
if [ -z "$DAYS" ]; then
    DAYS="30"
fi
psql ${UBUK_BDNAME?} -h ${SERVER?} -U ${UBUK_BDUSER?} -c "DELETE FROM tj_got WHERE op_time + interval '$DAYS days' < now() " >/dev/null 2>/dev/null
psql ${UBUK_BDNAME?} -h ${SERVER?} -U ${UBUK_BDUSER?} -c "DELETE FROM tj_sent WHERE op_time + interval '$DAYS days' < now() ">/dev/null 2>/dev/null
psql ${UBUK_BDNAME?} -h ${SERVER?} -U ${UBUK_BDUSER?} -c "DELETE FROM sj_log WHERE now() > optime + interval '$DAYS days'">/dev/null 2>/dev/null
psql ${UBUK_BDNAME?} -h ${SERVER?} -U ${UBUK_BDUSER?} -c "DELETE FROM gj_send where (id_shop!=sf_get_cur_id_shop() or (id_shop=sf_get_cur_id_shop() and (done=true or (mark=true and ask=true)))) and now() > op_time + interval '$DAYS days'" >/dev/null 2>/dev/null
