Skip to content

Commit

Permalink
Merge pull request #927 from plexguide/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Admin9705 authored Sep 3, 2024
2 parents 48e297e + 329a5f6 commit af99824
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions mods/scripts/apps_defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,44 @@ parse_and_store_defaults() {
local app_name="$1"
local app_type="$2" # 'personal' for personal apps, 'official' for official apps

# Determine paths based on config type
# Determine paths based on app type
if [[ "$app_type" == "personal" ]]; then
local app_file_path="/pg/p_apps/${app_name}.app"
local config_path="/pg/personal_configs/${app_name}.cfg"
local app_path="/pg/p_apps/${app_name}.app"
else
local app_file_path="/pg/apps/${app_name}.app"
local config_path="/pg/config/${app_name}.cfg"
local app_path="/pg/apps/${app_name}.app"
fi

# Check if the config file exists, create it if not
[[ ! -f "$config_path" ]] && touch "$config_path"

# Check if the app file exists
if [[ ! -f "$app_file_path" ]]; then
echo "Error: App file $app_file_path does not exist."
if [[ ! -f "$app_path" ]]; then
echo "Error: App file $app_path does not exist."
return 1
fi

# Read through the app file for lines starting with "##### "
while IFS= read -r line; do
if [[ "$line" =~ ^#####[[:space:]]+(.*?):[[:space:]]*(.*) ]]; then
local key="${BASH_REMATCH[1]}"
local value="${BASH_REMATCH[2]}"

# Convert key to lowercase and replace spaces with underscores
key=$(echo "$key" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')

# Trim leading and trailing whitespace from value
value=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

# If value is "null", set it to an empty string
if [[ "$value" == "null" ]]; then
value=""
fi

# Check if the key already exists in the config file, update if it does, add if it doesn't
if grep -q "^$key=" "$config_path"; then
sed -i "s|^$key=.*|$key=$value|" "$config_path"
else
echo "$key=$value" >> "$config_path"
# Source the app's default_variables function
source "$app_path"

# Call the default_variables function for the specific app
default_variables

# Parse the default_variables function and write to config if not exist
declare -f default_variables | while read line; do
if [[ $line =~ ^[[:space:]]*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$ ]]; then
var="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"

# Remove any existing quotes and semicolons from the value
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//" -e 's/;$//')

# Check if the variable exists in the config file
if ! grep -q "^${var}=" "$config_path"; then
# Add quotes around the value (without semicolon) and write to config
echo "${var}=\"${value}\"" >> "$config_path"
fi
fi
done < "$app_file_path"

echo "Configuration updated in $config_path"
done
}

0 comments on commit af99824

Please sign in to comment.