aboutsummaryrefslogtreecommitdiff
path: root/programs/cwal/components/common.sh
blob: b3805de5ee5179af93b5a91aff2970e04abf9d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
# Copyright (C) 2026 deltegui
# SPDX-License-Identifier: GPL-3.0-or-later

# Shared helpers for cwal component scripts. The orchestrator exports the
# CWAL_* context variables before invoking a component phase.

component_available()
{
    local command_name=$1
    local destination=$2

    command -v "$command_name" >/dev/null 2>&1 &&
        [[ -d ${destination%/*} ]]
}

require_readable()
{
    local description=$1
    local path=$2

    if [[ ! -r $path ]]; then
        printf 'apply-theme: %s is not readable: %s\n' "$description" "$path" >&2
        return 1
    fi
}

require_destination_directory()
{
    local component=$1
    local destination=$2
    local directory=${destination%/*}

    if [[ ! -d $directory ]]; then
        printf 'apply-theme: %s configuration directory does not exist: %s\n' \
            "$component" "$directory" >&2
        return 1
    fi
}

component_state_path()
{
    printf '%s/%s.%s\n' "$CWAL_CONTEXT" "$CWAL_COMPONENT" "$1"
}

component_temp()
{
    local artifact=$1
    local destination=$2
    local state
    local old_path=
    local temporary

    state=$(component_state_path "$artifact")
    if [[ -r $state ]]; then
        IFS= read -r old_path < "$state" || true
        [[ -z $old_path ]] || rm -f -- "$old_path"
    fi
    temporary=$(mktemp "${destination}.tmp.XXXXXX")
    printf '%s\n' "$temporary" > "$state"
    printf '%s\n' "$temporary"
}

component_temp_path()
{
    local artifact=$1
    local state
    local temporary=

    state=$(component_state_path "$artifact")
    if [[ ! -r $state ]]; then
        printf 'apply-theme: %s temporary file is missing\n' "$artifact" >&2
        return 1
    fi
    IFS= read -r temporary < "$state" || true
    if [[ -z $temporary || ! -f $temporary ]]; then
        printf 'apply-theme: %s temporary file is missing\n' "$artifact" >&2
        return 1
    fi
    printf '%s\n' "$temporary"
}

component_install()
{
    local artifact=$1
    local destination=$2
    local temporary

    temporary=$(component_temp_path "$artifact")
    mv -- "$temporary" "$destination"
    rm -f -- "$(component_state_path "$artifact")"
}

component_render()
{
    local artifact=$1
    local destination=$2
    local template=$3
    local temporary

    temporary=$(component_temp "$artifact" "$destination")
    if [[ -n ${CWAL_OVERRIDES:-} ]]; then
        "$CWAL_BIN" --base "$CWAL_THEME" "$CWAL_OVERRIDES" "$template" > "$temporary"
    else
        "$CWAL_BIN" "$CWAL_THEME" "$template" > "$temporary"
    fi
    chmod 0644 "$temporary"
}

component_render_stdout()
{
    local template=$1

    if [[ -n ${CWAL_OVERRIDES:-} ]]; then
        "$CWAL_BIN" --base "$CWAL_THEME" "$CWAL_OVERRIDES" "$template"
    else
        "$CWAL_BIN" "$CWAL_THEME" "$template"
    fi
}

component_set_status()
{
    local name=$1
    local value=$2

    printf '%s\n' "$value" > "$(component_state_path "$name")"
}

component_get_status()
{
    local name=$1
    local value=

    if [[ -r $(component_state_path "$name") ]]; then
        IFS= read -r value < "$(component_state_path "$name")" || true
    fi
    printf '%s\n' "${value:-not reloaded}"
}

icon_theme_available()
{
    local theme_name=$1
    local data_home=${XDG_DATA_HOME:-$HOME/.local/share}
    local data_directories=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
    local directory
    local -a directories

    [[ -d "$data_home/icons/$theme_name" ]] && return 0
    IFS=: read -r -a directories <<< "$data_directories"
    for directory in "${directories[@]}"; do
        [[ -d "$directory/icons/$theme_name" ]] && return 0
    done
    return 1
}

gtk_theme_available()
{
    local theme_name=$1
    local data_home=${XDG_DATA_HOME:-$HOME/.local/share}
    local data_directories=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
    local directory
    local -a directories

    [[ -d "$HOME/.themes/$theme_name" ]] && return 0
    [[ -d "$data_home/themes/$theme_name" ]] && return 0
    IFS=: read -r -a directories <<< "$data_directories"
    for directory in "${directories[@]}"; do
        [[ -d "$directory/themes/$theme_name" ]] && return 0
    done
    return 1
}