#!/bin/sh

keys="SERVERADDRESS APPNAMEOVERWRITE IMPRINTLINK PRIVACYLINK CUSTOMLOGINMESSAGE"
files="/usr/share/nginx/html/assets/config-*.js /usr/share/nginx/html/manifest.webmanifest"

# Replace env vars in files served by NGINX
for file in $files
do
  echo "Processing $file ...";
  for key in $keys
  do
    # Get environment variable
    value=$(eval echo "\$$key")

    # Set default value for APPNAMEOVERWRITE if empty
    if [ "$key" = "APPNAMEOVERWRITE" ] && [ -z "$value" ]; then
      value="FF Admin"
    fi

    echo "replace $key by $value"

    # replace __[variable_name]__ value with environment variable
    sed -i 's|__'"$key"'__|'"$value"'|g' $file
  done
done

nginx -g 'daemon off;'