#!/bin/bash set -euo pipefail log() { echo "[Env Manager] $*" } error() { log "ERROR: $*" >&2 exit 1 } get_nas_mount() { if [[ -f ${DYNAMICALSYSTEM_TEMP_DIR:-/tmp/dynamicalsystem}/nas_mount.env ]]; then source ${DYNAMICALSYSTEM_TEMP_DIR:-/tmp/dynamicalsystem}/nas_mount.env echo "${NAS_MOUNT_POINT}" else read -p "Enter NAS mount point: " mount_point echo "$mount_point" fi } prompt_for_env_value() { local var_name="$1" local prompt="$2" local is_secret="${3:-false}" if [[ "$is_secret" == "true" ]]; then read -s -p "$prompt: " value echo else read -p "$prompt: " value fi echo "$value" } create_env_template() { cat > "$1" << 'EOF' # Machine Environment Configuration # Generated on: $(date) DYNAMICAL_SYSTEM_FOLDER= ENVIRONMENT= EOF } load_or_create_env() { local env_file="$1" local env_vars=() if [[ -f "$env_file" ]]; then log "Found existing .env file at $env_file" while IFS='=' read -r key value; do if [[ ! "$key" =~ ^[[:space:]]*# ]] && [[ -n "$key" ]]; then key=$(echo "$key" | xargs) value=$(echo "$value" | xargs) if [[ -n "$value" ]]; then export "$key=$value" env_vars+=("$key") fi fi done < "$env_file" log "Loaded ${#env_vars[@]} environment variables" else log "No .env file found. Creating new configuration..." create_env_template "$env_file" echo log "Please provide values for the following environment variables:" log "(Press Enter to skip optional variables)" echo # Setup XDG variables for proper expansion export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" declare -A prompts=( ["DYNAMICAL_SYSTEM_FOLDER"]="$XDG_DATA_HOME/dynamicalsystem/" ["ENVIRONMENT"]="$(whoami)" ) declare -A secrets=( ["DYNAMICAL_SYSTEM_FOLDER"]=false ["ENVIRONMENT"]=false ) > "$env_file" echo "# Machine Environment Configuration" >> "$env_file" echo "# Generated on: $(date)" >> "$env_file" echo >> "$env_file" for var in ENVIRONMENT DYNAMICAL_SYSTEM_FOLDER; do is_secret="${secrets[$var]:-false}" default_value="${prompts[$var]}" value=$(prompt_for_env_value "$var" "$var [${default_value}]" "$is_secret") # Use default if empty if [[ -z "$value" ]]; then value="$default_value" fi if [[ -n "$value" ]]; then echo "$var=$value" >> "$env_file" export "$var=$value" env_vars+=("$var") fi done echo >> "$env_file" echo "# Custom Configuration" >> "$env_file" echo "# Add your custom environment variables below" >> "$env_file" log "Created .env file with ${#env_vars[@]} variables" fi # Try to set secure permissions, but don't fail if on NFS if ! chmod 600 "$env_file" 2>/dev/null; then log "Note: Could not change permissions on $env_file (may be on NFS mount)" fi echo "${env_vars[@]}" > ${DYNAMICALSYSTEM_TEMP_DIR:-/tmp/dynamicalsystem}/env_vars.list } create_profile_script() { local env_file="$1" local profile_dir="/etc/profile.d" local profile_script="$profile_dir/machine-env.sh" log "Creating system-wide profile script..." sudo tee "$profile_script" > /dev/null << 'EOF' #!/bin/bash # Machine environment variables loader # Auto-generated by machine setup # Determine environment name - default to username ENVIRONMENT="${ENVIRONMENT:-$(whoami)}" CONFIG_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/dynamicalsystem/config" ENV_FILE="$CONFIG_DIR/machine.$ENVIRONMENT.env" if [[ -f "$ENV_FILE" ]]; then set -a source "$ENV_FILE" set +a fi EOF sudo chmod 644 "$profile_script" } main() { log "Starting environment variable management..." # Use the symlink structure instead of raw mount point local config_dir="${XDG_DATA_HOME:-$HOME/.local/share}/dynamicalsystem/config" # Create config directory if it doesn't exist if [[ ! -d "$config_dir" ]]; then mkdir -p "$config_dir" fi # Determine environment name - default to username local environment="${ENVIRONMENT:-$(whoami)}" # Check for existing env files or prompt for environment local existing_files=($(ls "$config_dir"/machine.*.env 2>/dev/null)) if [[ ${#existing_files[@]} -gt 0 ]] && [[ -z "$ENVIRONMENT" ]]; then log "Found existing environment files:" for i in "${!existing_files[@]}"; do local fname=$(basename "${existing_files[$i]}") local env_name=${fname#machine.} env_name=${env_name%.env} echo "$((i+1))) $env_name" done echo "$((${#existing_files[@]}+1))) Create new environment" read -p "Select environment [1]: " selection selection="${selection:-1}" if [[ "$selection" -le "${#existing_files[@]}" ]]; then local fname=$(basename "${existing_files[$((selection-1))]}") environment=${fname#machine.} environment=${environment%.env} else read -p "Enter new environment name [$(whoami)]: " environment environment="${environment:-$(whoami)}" fi fi local env_file="$config_dir/machine.$environment.env" load_or_create_env "$env_file" create_profile_script "$env_file" log "Environment variables configured successfully!" log "Variables are stored in: $env_file" log "They will be automatically loaded on login via /etc/profile.d/machine-env.sh" } main "$@"