#!/bin/bash

UBUK_BDNAME=ubuk
UBUK_BDUSER=bukts
UBUK_HOST=127.0.0.1

while read -r j
do
#   echo "$j" >> /tmp/logtest
  if [[ "$j" =~ ^\[[0-9]*\]$ ]]; then
    AZS=`echo "$j" | sed -r 's/[^0-9]//g'`
#     echo "AZS=$AZS" >> /tmp/logtest
  fi
  if [[ "$j" =~ ^NAME= ]]; then
    VAL=`echo "$j" | sed -r 's/^NAME=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 0 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=0) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=0;"
#     echo "IP=$VAL" >> /tmp/logtest
  fi  
  if [[ "$j" =~ ^DB_PORT= ]]; then
    VAL=`echo "$j" | sed -r 's/^DB_PORT=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 1 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=1) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=1;"
  fi  
  if [[ "$j" =~ ^DB_NAME= ]]; then
    VAL=`echo "$j" | sed -r 's/^DB_NAME=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 2 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=2) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=2;"
  fi  
  if [[ "$j" =~ ^DB_USER= ]]; then
    VAL=`echo "$j" | sed -r 's/^DB_USER=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 3 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=3) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=3;"
  fi  
  if [[ "$j" =~ ^DB_PASS= ]]; then
    VAL=`echo "$j" | sed -r 's/^DB_PASS=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 4 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=4) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=4;"
  fi  
  if [[ "$j" =~ ^SSH_PORT= ]]; then
    VAL=`echo "$j" | sed -r 's/^SSH_PORT=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 5 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=5) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=5;"
  fi  
  if [[ "$j" =~ ^TIME= ]]; then
    VAL=`echo "$j" | sed -r 's/^TIME=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 6 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=6) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=6;"
  fi  
  if [[ "$j" =~ ^TIME2= ]]; then
    VAL=`echo "$j" | sed -r 's/^TIME2=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 7 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=7) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=7;"
  fi  
  if [[ "$j" =~ ^TIME3= ]]; then
    VAL=`echo "$j" | sed -r 's/^TIME3=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 8 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=8) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=8;"
  fi  
  if [[ "$j" =~ ^ADD_LIST= ]]; then
    VAL=`echo "$j" | sed -r 's/^ADD_LIST=//g'`
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "INSERT INTO uremote.sd_remote_conf (select $AZS, 13 , '$VAL' from (select 1) as t where not exists (SELECT 1 FROM uremote.sd_remote_conf where id_shop=$AZS AND typ=13) LIMIT 1)"
    psql $UBUK_BDNAME -U $UBUK_BDUSER -h $UBUK_HOST -qAt -c "UPDATE uremote.sd_remote_conf SET tvalue='$VAL' WHERE id_shop=$AZS AND typ=13;"
  fi  
done < /etc/opt/bukmmadm/point.ini

