summaryrefslogtreecommitdiff
path: root/src/lib/bootstrap.source.sh
blob: a047f5da81ed58e4ec92ff7b1aaf3a4d03aa4d7f (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
usage() {
    cat - <<USAGE >&2
    Usage:
    $0 --generate [--config PATH] [OPTIONS]
    $0 --refresh-splash [--config PATH] [OPTIONS]
    $0 --sync [--config PATH] [OPTIONS]
    $0 --dry-run [--config PATH] [OPTIONS]
    $0 --print-config [--config PATH] [OPTIONS]
    $0 --clean [--config PATH] [OPTIONS]
    $0 --version
    $0 --init

    Options:
    --config PATH
    --incoming PATH
    --dist PATH
    --template PATH
    --title TEXT
    --height VALUE
    --thumbheight VALUE
    --maxpreviews N
    --image-jobs N
    --random-seed VALUE
    --splash
    --no-splash
    --stats
    --no-stats
    --shuffle
    --no-shuffle
    --tarball
    --no-tarball
    --force
    --sync-destination DEST
    --sync-delete
    --no-sync-delete
    --verbose
    --quiet
USAGE
}

output_is_quiet() {
    [ "$SHURIKEN_OUTPUT_MODE" = quiet ]
}

output_is_verbose() {
    [ "$SHURIKEN_OUTPUT_MODE" = verbose ]
}

log_info() {
    if ! output_is_quiet; then
        printf '%s\n' "$*"
    fi
}

log_verbose() {
    if output_is_verbose; then
        printf 'Verbose: %s\n' "$*"
    fi
}

log_warning() {
    printf 'WARNING: %s\n' "$*" >&2
}

# Read a configuration value into the named array, accepting both Bash array
# and whitespace-separated scalar declarations of the same variable.
# This is shared by resolve_tar_opts and resolve_sync_destinations so the
# "array or scalar config" parsing lives in exactly one place.
#
# Arguments:
#   $1  name of the source config variable (e.g. TAR_OPTS)
#   $2  name of the destination array variable (nameref)
# Returns:
#   0 if the variable was declared (the destination may still be empty),
#   1 if the variable was not declared at all (lets callers apply defaults).
resolve_config_array() {
    local -r config_var="$1"; shift
    local -n config_array_ref="$1"; shift
    local config_decl

    config_array_ref=()

    # declare -p fails when the variable was never set; callers use the
    # non-zero return to distinguish "unset" from "set but empty".
    if ! config_decl=$(declare -p "$config_var" 2>/dev/null); then
        return 1
    fi

    case "$config_decl" in
        declare\ -a*\ "$config_var"=*)
            # Already a real array: copy it element by element.
            local -n config_source_ref="$config_var"
            # shellcheck disable=SC2034
            config_array_ref=("${config_source_ref[@]}")
            ;;
        *)
            # Scalar string: word-split it into the destination array.
            local -n config_scalar_ref="$config_var"
            if [ -n "${config_scalar_ref:-}" ]; then
                # shellcheck disable=SC2034
                read -r -a config_array_ref <<< "$config_scalar_ref"
            fi
            ;;
    esac
}

resolve_default_rc_file() {
    local source_root

    if [ -f "$DEFAULTRC" ]; then
        printf '%s\n' "$DEFAULTRC"
        return
    fi

    source_root=$(resolve_source_root)

    if [ -n "$source_root" ]; then
        printf '%s\n' "$source_root/src/shuriken.default.conf"
        return
    fi

    printf '%s\n' "$DEFAULTRC"
}

resolve_source_root() {
    local script_dir
    local source_root

    script_dir="$SHURIKEN_SOURCE_DIR"
    source_root=$(cd "$script_dir/.." && pwd)

    if [[ -f "$source_root/src/shuriken.default.conf" \
        && -d "$source_root/share/templates/default" ]]; then
        printf '%s\n' "$source_root"
    fi
}

resolve_default_template_dir() {
    local source_root

    if [ -d "$DEFAULT_TEMPLATE_DIR" ]; then
        printf '%s\n' "$DEFAULT_TEMPLATE_DIR"
        return
    fi

    source_root=$(resolve_source_root)

    if [ -n "$source_root" ]; then
        printf '%s\n' "$source_root/share/templates/default"
        return
    fi

    printf '%s\n' "$DEFAULT_TEMPLATE_DIR"
}

resolve_default_asset_dir() {
    local source_root

    if [ -d "$DEFAULT_ASSET_DIR" ]; then
        printf '%s\n' "$DEFAULT_ASSET_DIR"
        return
    fi

    source_root=$(resolve_source_root)

    if [ -n "$source_root" ]; then
        printf '%s\n' "$source_root/assets/site"
        return
    fi

    printf '%s\n' "$DEFAULT_ASSET_DIR"
}

template_dir_uses_default() {
    if [ -z "${TEMPLATE_DIR+x}" ] || [ -z "$TEMPLATE_DIR" ]; then
        return 1
    fi

    case "$TEMPLATE_DIR" in
        "$PACKAGED_TEMPLATE_DIR"|"$DEFAULT_TEMPLATE_DIR")
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

apply_template_dir_default() {
    if template_dir_uses_default; then
        TEMPLATE_DIR=$(resolve_default_template_dir)
    fi
}

init_config() {
    local -r rc_file=shuriken.conf
    local default_rc_file
    local rewritten_rc_file
    local source_root
    local source_template_dir

    if [ -f "$rc_file" ]; then
        printf 'Error: %s already exists\n' "$rc_file" >&2
        exit 1
    fi

    default_rc_file=$(resolve_default_rc_file)

    if [ ! -f "$default_rc_file" ]; then
        printf 'Error: Can not find config file %s\n' "$default_rc_file" >&2
        exit 1
    fi

    cp "$default_rc_file" "$rc_file"

    source_root=$(resolve_source_root)
    if [[ -n "$source_root" \
        && "$default_rc_file" = "$source_root/src/shuriken.default.conf" ]]; then
        source_template_dir="$source_root/share/templates/default"
        rewritten_rc_file=$(mktemp "${rc_file}.XXXXXX")
        if ! awk -v template_dir="$source_template_dir" \
            '/^TEMPLATE_DIR=/ {
                print "TEMPLATE_DIR=" template_dir
                next
            }
            { print }' "$rc_file" > "$rewritten_rc_file"; then
            rm -f "$rewritten_rc_file"
            exit 1
        fi
        cat "$rewritten_rc_file" > "$rc_file"
        rm -f "$rewritten_rc_file"
    fi

    log_info "Created ./$rc_file"
}