#!/bin/bash

# https://gist.github.com/athlan/b6f09977e2f5cf20840ef61ca3cda932
printenv | sed 's/^\(.*\)$/export \1/g' > /root/.env.sh
chmod +x /root/.env.sh

export TZ=Europe/Moscow

#. /etc/profile
#. ~/.bash_profile
#. ~/.bashrc
cd /opt/pentaho-di/mappings_old

for F in $(find . -type f); do
  sed -i "s/1111/$DB_DASHBOARD_PORT/g" $F
  sed -i "s/dashboard-dbhost/$DB_DASHBOARD_HOST/g" $F
  sed -i "s/dashboard-dbname/$DB_DASHBOARD_NAME/g" $F
  sed -i "s/dashboard-dbuser/$DB_DASHBOARD_USERNAME/g" $F
  sed -i "s/Encrypted 2daf9dca008c89396af54aa72ce93bcc9/$DB_DASHBOARD_PASSWORD/g" $F

  sed -i "s/2222/$DB_APPEAL_PORT/g" $F
  sed -i "s/appeal-dbhost/$DB_APPEAL_HOST/g" $F
  sed -i "s/appeal-dbname/$DB_APPEAL_NAME/g" $F
  sed -i "s/appeal-dbuser/$DB_APPEAL_USERNAME/g" $F
  sed -i "s/Encrypted 2be98afa91ad79785a754aa72ce93bcc9/$DB_APPEAL_PASSWORD/g" $F

  sed -i "s/3333/$DB_DECISION_PORT/g" $F
  sed -i "s/decision-dbhost/$DB_DECISION_HOST/g" $F
  sed -i "s/decision-dbname/$DB_DECISION_NAME/g" $F
  sed -i "s/decision-dbuser/$DB_DECISION_USERNAME/g" $F
  sed -i "s/Encrypted 2befccaab03d49b8ba554aa72ce93bcc9/$DB_DECISION_PASSWORD/g" $F

  sed -i "s/4444/$DB_PERSON_PORT/g" $F
  sed -i "s/person-dbhost/$DB_PERSON_HOST/g" $F
  sed -i "s/person-dbname/$DB_PERSON_NAME/g" $F
  sed -i "s/person-dbuser/$DB_PERSON_USERNAME/g" $F
  sed -i "s/Encrypted 2be98afb80fd5818ba554aa72ce93bcc9/$DB_PERSON_PASSWORD/g" $F

  sed -i "s/5555/$DB_SUBPOENA_PORT/g" $F
  sed -i "s/subpoena-dbhost/$DB_SUBPOENA_HOST/g" $F
  sed -i "s/subpoena-dbname/$DB_SUBPOENA_NAME/g" $F
  sed -i "s/subpoena-dbuser/$DB_SUBPOENA_USERNAME/g" $F
  sed -i "s/Encrypted 2beebdaaa1ac8978aaa54aa72ce93bcc9/$DB_SUBPOENA_PASSWORD/g" $F
  
  sed -i "s/6666/$DB_JOURNAL_PORT/g" $F
  sed -i "s/journal-dbhost/$DB_JOURNAL_HOST/g" $F
  sed -i "s/journal-dbname/$DB_JOURNAL_NAME/g" $F
  sed -i "s/journal-dbuser/$DB_JOURNAL_USERNAME/g" $F
  sed -i "s/Encrypted 2be98afc86aa7f2e4cb79ce10db80b9cf/$DB_JOURNAL_PASSWORD/g" $F
  
  sed -i "s/7777/$DB_METRICS_PORT/g" $F
  sed -i "s/metrics-dbhost/$DB_METRICS_HOST/g" $F
  sed -i "s/metrics-dbname/$DB_METRICS_NAME/g" $F
  sed -i "s/metrics-dbuser/$DB_METRICS_USERNAME/g" $F
  sed -i "s/Encrypted 657276755ddceddca104c281979414ab64cc9bacc9/$DB_METRICS_PASSWORD/g" $F
done;

  #Запускаем job один раз при старте контейнера
  echo "---- Run info_recruits/job_info_recruits.kjb ----"
  bash -c ". /root/.env.sh;  /opt/pentaho-di/data-integration/kitchen.sh /file:////opt/pentaho-di/mappings_old/info_recruits/job_info_recruits.kjb > /proc/1/fd/1 2>&1"

if [ $RUNONCE == true ]; then
  echo "---- Run job_general.kjb ----"
  bash -c ". /root/.env.sh;  /opt/pentaho-di/data-integration/kitchen.sh /file:////opt/pentaho-di/mappings_old/job_general.kjb > /proc/1/fd/1 2>&1"
  echo "---- Run job_metr.kjb ----"
  bash -c ". /root/.env.sh;  /opt/pentaho-di/data-integration/kitchen.sh /file:////opt/pentaho-di/mappings_old/metrics/job_metr.kjb > /proc/1/fd/1 2>&1"
  exit $?
else
  cat /root/crontab | crontab -
  cron -f
fi

