2020-04-14 20:22:34 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-05-23 15:50:31 +00:00
|
|
|
#####################################################################
|
|
|
|
# Substitutes the given environment variables in the given files.
|
|
|
|
# Parameters:
|
|
|
|
# $1: Comma-delimited list of file paths
|
|
|
|
# $2: Comma-delimited list of environment variables
|
2020-05-24 15:26:20 +00:00
|
|
|
# $3: Absolute path to nginx html directory (optional)
|
2020-05-23 15:50:31 +00:00
|
|
|
#####################################################################
|
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Substituting environment variables...'
|
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# The first parameter is a comma-delimited list of paths to files which should be substituted
|
2020-04-14 20:22:34 +00:00
|
|
|
if [[ -z $1 ]]; then
|
2020-05-17 02:59:05 +00:00
|
|
|
echo 'ERROR: No target files given.'
|
2020-04-14 20:22:34 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-20 02:20:03 +00:00
|
|
|
# The second parameter is a comma-delimited list of environment variable names
|
|
|
|
if [[ -z $2 ]]; then
|
|
|
|
echo 'ERROR: No environment variables given.'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-24 15:26:20 +00:00
|
|
|
# The third parameter is the absolute path to the nginx html directory
|
|
|
|
if [[ -z $2 ]]; then
|
|
|
|
echo 'ERROR: No path to nginx html directory given.'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-24 02:54:10 +00:00
|
|
|
# Add trailing slash to $BASE_HREF if needed
|
|
|
|
if [[ "$2" == *"BASE_HREF"* ]]; then
|
|
|
|
length=${#BASE_HREF}
|
|
|
|
last_char=${BASE_HREF:length-1:1}
|
2020-05-24 03:55:43 +00:00
|
|
|
[[ $last_char == "/" ]] && BASE_HREF="$BASE_HREF"; :
|
2020-05-24 15:26:20 +00:00
|
|
|
|
|
|
|
# The third parameter is the absolute path to the nginx html directory
|
2020-05-24 18:27:06 +00:00
|
|
|
if [[ $# -ge 3 ]]; then
|
2020-05-24 15:26:20 +00:00
|
|
|
# Replace all instances of __REPLACE_ME_WITH_BASE_HREF__ with $BASE_HREF
|
|
|
|
find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \
|
|
|
|
xargs -0 sed -i 's@__REPLACE_ME_WITH_BASE_HREF__@'"$BASE_HREF"'@g'
|
|
|
|
|
|
|
|
echo 'Replacing base href...'
|
|
|
|
# Wait a few seconds in case find | sed needs more time
|
|
|
|
sleep 3
|
|
|
|
fi
|
2020-05-24 02:54:10 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-20 02:20:03 +00:00
|
|
|
# Convert "VAR1,VAR2,VAR3,..." to "\$VAR1 \$VAR2 \$VAR3 ..."
|
|
|
|
env_list="\\\$${2//,/ \\\$}" # "\" and "$" are escaped as "\\" and "\$"
|
|
|
|
for file_path in ${1//,/ }
|
2020-05-17 02:59:05 +00:00
|
|
|
do
|
2020-05-17 13:50:00 +00:00
|
|
|
echo "replacing $env_list in $file_path"
|
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# Replace strings in the given file(s) in env_list
|
2020-05-17 13:50:00 +00:00
|
|
|
envsubst "$env_list" < "$file_path" > "$file_path".tmp && mv "$file_path".tmp "$file_path"
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo '...'
|
2020-05-24 03:55:43 +00:00
|
|
|
# Wait a second in case envsubst needs more time
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
# If this is the nginx default.conf file, replace double slashes with single slashes
|
|
|
|
if [[ $file_path == *"/default.conf"* ]]; then
|
|
|
|
sed -i -e 's@//@/@g' "$file_path"
|
|
|
|
fi
|
2020-05-17 02:59:05 +00:00
|
|
|
done
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Finished substituting environment variables.'
|
2020-05-20 02:20:03 +00:00
|
|
|
for env_var in ${2//,/ }
|
|
|
|
do
|
|
|
|
echo "$env_var = ${!env_var}"
|
|
|
|
done
|
2020-05-24 18:27:06 +00:00
|
|
|
|
|
|
|
# Execute all other commands with parameters
|
|
|
|
if [[ $# -gt 3 ]]; then
|
|
|
|
exec "${@:4}"
|
|
|
|
else
|
|
|
|
if [[ $# -gt 2 ]]; then
|
|
|
|
exec "${@:3}"
|
|
|
|
fi
|
|
|
|
fi
|