53 lines
1.9 KiB
Bash
53 lines
1.9 KiB
Bash
#!/bin/bash
|
|
|
|
# Output file
|
|
output_file="$HOME/.config/hypr/conf/env.conf"
|
|
|
|
# Well-known environment variables to skip
|
|
skip_vars="HOME"
|
|
|
|
# Define the output file
|
|
output_file="$HOME/.config/hypr/conf/env.conf"
|
|
|
|
# Write Info into file
|
|
echo -e "# This file was imported from envvars config in .config .\n# Don't edit this file! This file would be override by import-env script.\n" > "$output_file"
|
|
|
|
# Temporary associative array to store referenced variables
|
|
declare -A var_references
|
|
|
|
# Process each .conf file in the directory
|
|
for conf_file in ~/.config/environment.d/*.conf; do
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
# Preserve comments
|
|
if [[ $line =~ ^#.* ]]; then
|
|
echo "$line" >> "$output_file"
|
|
continue
|
|
fi
|
|
|
|
# Handle variable definitions, skipping lines without '='
|
|
if [[ $line =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
|
|
key="${BASH_REMATCH[1]}"
|
|
value="${BASH_REMATCH[2]}"
|
|
|
|
# Check if the value references another variable
|
|
if [[ $value =~ \$(\{?[A-Za-z_][A-Za-z0-9_]*\}?) ]]; then
|
|
var_name="${BASH_REMATCH[1]}"
|
|
var_name="${var_name//\{\}/}"
|
|
|
|
# If the referenced variable is not in the skip list, add to references
|
|
if ! [[ $skip_vars =~ $var_name ]]; then
|
|
if [[ -z ${var_references[$var_name]} ]]; then
|
|
var_ref_value=$(eval echo \$$var_name)
|
|
echo "\$$var_name=$var_ref_value" >> "$output_file"
|
|
var_references[$var_name]=$var_ref_value
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Write the environment variable in the desired format
|
|
echo "env=${key},${value}" >> "$output_file"
|
|
fi
|
|
done < "$conf_file"
|
|
done
|
|
|
|
echo "Merged environment variables written to $output_file"
|