#!/usr/bin/env bash
set -euo pipefail

config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/labwc"
theme_dir="${XDG_DATA_HOME:-$HOME/.local/share}/themes/noctalia/openbox-3"
theme_file="$theme_dir/themerc"
source_theme="$config_dir/noctalia.conf"
rc_file="$config_dir/rc.xml"

mkdir -p "$config_dir" "$theme_dir"

if [ -f "$source_theme" ]; then
    cp -f "$source_theme" "$theme_file"
fi

if [ ! -f "$rc_file" ]; then
    cat >"$rc_file" <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<labwc_config>
  <theme>
    <name>noctalia</name>
  </theme>
</labwc_config>
EOF
    exit 0
fi

if grep -q '<theme>' "$rc_file"; then
    if ! grep -q '</theme>' "$rc_file"; then
        echo "Cannot update Labwc theme: $rc_file contains <theme> without </theme>" >&2
        exit 1
    fi

    if sed -n '/<theme>/,/<\/theme>/p' "$rc_file" | grep -q '<name>.*</name>'; then
        sed -i '/<theme>/,/<\/theme>/s|<name>.*</name>|<name>noctalia</name>|' "$rc_file"
    else
        sed -i '/<theme>/a\    <name>noctalia</name>' "$rc_file"
    fi
else
    if ! grep -qE '<labwc_config([[:space:]>])' "$rc_file"; then
        echo "Cannot update Labwc theme: $rc_file does not contain <labwc_config>" >&2
        exit 1
    fi

    sed -i '/<labwc_config[[:space:]>]/a\  <theme>\n    <name>noctalia</name>\n  </theme>' "$rc_file"
fi
