#!/bin/bash

DB_HOST=${DB_HOST:-localhost}
DB_PORT=${DB_HOST:-5432}
DB_KEEPALIVE=${DB_KEEPALIVE:-false} #default to false
DB_KEEPALIVE=${DB_KEEPALIVE,,} #set to lower-case
DB_HOMER_CONFIG=${DB_HOMER_CONFIG:-homer_config}
DB_HOMER_DATA=${DB_HOMER_DATA:-homer_data}
INFLUX_DB=${INFLUX_DB:-localhost}
PROM_HOST=${PROM_HOST:-localhost}
LOKI_HOST=${LOKI_HOST:-localhost}
GRAFANA_HOST=${GRAFANA_HOST:-localhost}
TSHARK_ACTIVE=${TSHARK_ACTIVE:-false}
TSHARK_BIN=${TSHARK_BIN:-/usr/bin/tshark}
TSHARK_UID=${TSHARK_UID:-0}
TSHARK_GID=${TSHARK_GID:-0}

if [ -f /usr/local/homer/etc/webapp_config.json ]; then

   if [ -n "$DB_HOST" ]; then sed -i "s/homer_db_host/${DB_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_PORT" ]; then sed -i "s/homer_db_port/${DB_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_USER" ]; then sed -i "s/homer_db_user/${DB_USER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_PASS" ]; then sed -i "s/homer_password/${DB_PASS}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_KEEPALIVE" ]; then sed -i "s/homer_db_keepalive/${DB_KEEPALIVE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_HOMER_CONFIG" ]; then sed -i "s/homer_config/${DB_HOMER_CONFIG}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$DB_HOMER_DATA" ]; then sed -i "s/homer_data/${DB_HOMER_DATA}/g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$HOMER_LOGLEVEL" ]; then sed -i "s/homer_loglevel/${HOMER_LOGLEVEL}/g" /usr/local/homer/etc/webapp_config.json;
   else sed -i "s/homer_loglevel/error/g" /usr/local/homer/etc/webapp_config.json; fi
   
   if [ -n "$HOMER_ENABLE_API" ]; then sed -i "s/homer_enable_api/${HOMER_ENABLE_API}/g" /usr/local/homer/etc/webapp_config.json;
   else sed -i "s/homer_enable_api/false/g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$INFLUX_HOST" ]; then sed -i "s/influx_host/${INFLUX_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$INFLUX_USER" ]; then sed -i "s/influx_user/${INFLUX_USER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$INFLUX_PASS" ]; then sed -i "s/influx_pass/${INFLUX_PASS}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$INFLUX_PORT" ]; then sed -i "s/8086/${INFLUX_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$INFLUX_DB" ]; then sed -i "s/influx_database/${INFLUX_DB}/g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$PROM_HOST" ]; then sed -i "s/prom_host/${PROM_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$PROM_PORT" ]; then sed -i "s/9090/${PROM_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$PROM_USER" ]; then sed -i "s/prom_user/${PROM_USER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$PROM_PASS" ]; then sed -i "s/prom_pass/${PROM_PASS}/g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$LOKI_HOST" ]; then sed -i "s/loki_host/${LOKI_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LOKI_PORT" ]; then sed -i "s/3100/${LOKI_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LOKI_USER" ]; then sed -i "s/loki_user/${LOKI_USER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LOKI_PASS" ]; then sed -i "s/loki_pass/${LOKI_PASS}/g" /usr/local/homer/etc/webapp_config.json; fi

   
   if [ -n "$GRAFANA_HOST" ]; then sed -i "s/grafana_host/${GRAFANA_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$GRAFANA_PORT" ]; then sed -i "s/3000/${GRAFANA_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$GRAFANA_TOKEN" ]; then sed -i "s/grafana_token/${GRAFANA_TOKEN}/g" /usr/local/homer/etc/webapp_config.json; fi
   sed -i "s/grafana_path/${GRAFANA_PATH}/g" /usr/local/homer/etc/webapp_config.json

   if [ -n "$HTTP_HOST" ]; then sed -i "s/0.0.0.0/${HTTP_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$HTTP_PORT" ]; then sed -i "s/homer_web_port/${HTTP_PORT}/g" /usr/local/homer/etc/webapp_config.json; 
   else sed -i "s/homer_web_port/80/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$HTTP_PATH" ]; then sed -i "s=homer_serve_path=${HTTP_PATH}=g" /usr/local/homer/etc/webapp_config.json;
   else sed -i "s=homer_serve_path=/=g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$AUTH_TYPE" ]; then sed -i "/type/ s/internal/${AUTH_TYPE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_BASE" ]; then sed -i "/base/ s/dc=example,dc=com/${LDAP_BASE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_HOST" ]; then sed -i "s/ldap.example.com/${LDAP_HOST}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_PORT" ]; then sed -i "s/389/${LDAP_PORT}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_SSL" ];  then sed -i "/usessl/ s/false/${LDAP_SSL}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_SKIP_TLS" ]; then sed -i "/skiptls/ s/true/${LDAP_SKIP_TLS}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_BIND_DN" ];  then sed -i "s/uid=readonlysuer,ou=People,dc=example,dc=com/${LDAP_BIND_DN}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_BIND_PASS" ]; then sed -i "s/readonlypassword/${LDAP_BIND_PASS}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_USER_FILTER" ]; then sed -i "s/(uid=%s)/${LDAP_USER_FILTER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_GROUP_FILTER" ]; then sed -i "s/(memberUid=%s)/${LDAP_GROUP_FILTER}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_GROUP_ATTRIB" ]; then sed -i "/group_attributes/ {N;N;N;N;N;N;N;N; s/\[.*/\[\n\t${LDAP_GROUP_ATTRIB}\n\t\],/g}" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_ADMINGROUP" ]; then sed -i "/admingroup/ s/HOMER_admin/${LDAP_ADMINGROUP}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_ADMINMODE" ]; then sed -i "/adminmode/ s/true/${LDAP_ADMINMODE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_USERGROUP" ]; then sed -i "/usergroup/ s/HOMER_user/${LDAP_USERGROUP}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_USERMODE" ]; then sed -i "/usermode/ s/true/${LDAP_USERMODE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_ATTRIBUTES" ]; then sed -i "/attributes/ s/\[\([^]]*\)\]/\[${LDAP_ATTRIBUTES}\]/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_SKIP_VERIFY" ]; then sed -i "/skipverify/ s/true/${LDAP_SKIP_VERIFY}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_ANONYMOUS" ]; then sed -i "/anonymous/ s/false/${LDAP_ANONYMOUS}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$LDAP_USER_DN" ]; then sed -i "/userdn/ s/uid=%s,ou=People,dc=example,dc=com/${LDAP_USER_DN}/g" /usr/local/homer/etc/webapp_config.json; fi

   if [ -n "$TSHARK_UID" ]; then sed -i "s/tshark_uid/${TSHARK_UID}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$TSHARK_GID" ]; then sed -i "s/tshark_gid/${TSHARK_GID}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$TSHARK_ACTIVE" ]; then sed -i "s/tshark_active/${TSHARK_ACTIVE}/g" /usr/local/homer/etc/webapp_config.json; fi
   if [ -n "$TSHARK_BIN" ]; then sed -i "s/tshark_bin/${TSHARK_BIN}/g" /usr/local/homer/etc/webapp_config.json; fi

   echo "Pre-Flight provisioning completed!"

else

   echo "Configuration file not found!"

fi

exec "$@"
