summaryrefslogtreecommitdiff
path: root/prepare
blob: fa9e6ed700b331236fa12fd3b558ea692c66530d (plain) (blame)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/bin/sh -e

#===============================================================
# Filename : prepare
# Purpose  : Prepare wallpapers for Adélie desktop environments.
# Authors  : Zach van Rijn <me@zv.io>
# License  : Apache 2.0
# Revision : 20231202
#===============================================================

#===============================================================
# README
#===============================================================

##
# This script transforms source images, including Adélie logos,
# into a set of wallpapers suitable for packaging.
#
# There are two distinct output products:
#
# (1) a "core" or "slim" bundle, containing a lightweight set of
#     default wallpaper(s) and/or branding; and
#
# (2) a "full" bundle containing all available wallpapers, which
#     is suitable for use when disk space isn't a major concern.
# 
#
#
# requirements
# ------------
#
#   * imagemagick
#
#
# usage
# -----
#
# To process all source images and generate a tarball for upload
# to 'distfiles', simply run the following command:
#
#   $ ./prepare
#
# Any tarball of the same name will be silently overwritten.
#


#---------------------------------------------------------------
# configuration: source image input
#---------------------------------------------------------------

##
# Distfiles mirror.
#
HOST="https://distfiles.adelielinux.org";


##
# Adélie brand asset bundle version.
#
# This corresponds to the commit of 'adelie/site-ng' from which
# the brand assets were generated. A tarball matching this hash
# must exist in the '<HOST>/source/site-ng-images/' directory.
#
# See https://git.adelielinux.org/adelie/site-ng for details.
#
HASH=ca57e2bd46f11bf2efdebbca8c7813235744fd5f;


##
# Name of the containing directory and tarball on distfiles.
#
NAME="site-ng-images";


#---------------------------------------------------------------
# configuration: processed image output
#---------------------------------------------------------------

##
# Name of output directory. Please ensure it is in '.gitignore'.
#
KEEP="output";


##
# Choose an output file type.
#
TYPE="jpg";


##
# Table of aspect ratios to generate wallpapers for.
#
#     Format: RATIO SCALE [SCALE ...]
#
# Scale is distributed, e.g.:
#
#     "4:3 640" --> (640*4)x(640*3) = 2560x1920
#
# Note that many popular aspect ratios are approximate. We are
# only concerned with getting to the right ballpark.
#
# Table rows MUST BE SORTED FROM GREATEST TO LEAST.
#
MAKE=$(grep -v ^# <<EOF
4:3 640 400 256 200
5:4 480 256
3:2 854
16:10 160 120 105 80
16:9 240 160 120 90
21:9 122
EOF
);


#---------------------------------------------------------------
# internal: meta
#---------------------------------------------------------------

HERE="$(dirname $(readlink -f ${0}))";
cd "${HERE}";


##
# Check for required tools.
#
for k in curl composite convert magick; do
    if ! command -v ${k} 2>&1 >/dev/null; then
        printf "E: required utility '%s' not found!\n" "${k}";
        exit 1;
    fi
done


##
# Tag first, then generate release the tarball. Sanity checks do
# not prevent someone from being stupid, but they can avoid some
# common errors. We want to reduce surprises/ensure consistency.
#
VTAG=$(git describe --abbrev=0 2>/dev/null || true);
if test -z "${VTAG}"; then
    printf "E: you need to tag at least one commit!\n";
    exit 1;
fi
if test $(git rev-list ${VTAG}..HEAD | wc -l) -gt 0; then
    printf "E: you need to be checked out at a tag!\n";
    exit 1;
fi
if test $(git ls-remote --tags origin ${VTAG} | wc -l) -eq 0; then
    printf "E: tag '%s' does not exist on origin!\n" "${VTAG}";
    exit 1;
fi


##
# Name of build directory. Please ensure it is in '.gitignore'.
#
TEMP="build";


##
# Name of source directory.
#
IMGS="src";


##
# Ensure that no images have conflicting names.
#
# FIXME: factor out 'background lockscreen' here and elsewhere.
#
for k in background lockscreen; do
    if test -d "${IMGS}"/${k}; then
        printf "E: not allowed to name image '%s'!\n" "${k}";
        exit 1;
    fi
done


#---------------------------------------------------------------
# internal: support routines
#---------------------------------------------------------------

##
# size_only DIR RATIO SCALE [SCALE ...]
#
size_only ()
{
    data="${1}"; shift;
    size="${1}"; shift;
    from="${1}"; # NO SHIFT
    list="${@}";

    ##
    # FIXME: assumes 'a:b' where 'a' and 'b' are integers. This
    # should check that this actually holds.
    #
    case "${size}" in
    *:*)
        ;;
    *)
        printf "E: invalid aspect ratio '%s'!\n" "${size}";
        exit 1;
        ;;
    esac

    ##
    # Generated images have known sizes that do not change.
    #
    from_x=$((${from}*${size%:*}));
    from_y=$((${from}*${size#*:}));

    printf "Processing %s...\n" "${size}";
    for k in ${list}; do
        x=$((${k}*${size%:*}));
        y=$((${k}*${size#*:}));
        printf "  * Generating %4sx%4s (scale: %s)\n" "${x}" "${y}" "${k}";
        find "${data}" -mindepth 1 -maxdepth 1 -type d | sort | while read image; do
            name=$(printf "%s\n" "${image}" | cut -d/ -f3); # sloppy, sorry
            case "${name}" in
                background|lockscreen)
                    type="png"; # override for quality
                    ;;
                *)
                    type="${TYPE}";
                    ;;
            esac
            printf "      - '%s'\n" "${name}";
            mkdir -p "${KEEP}"/${name};
            convert "${image}/${from_x}x${from_y}.png" -resize ${x}x${y}^ "${KEEP}"/${name}/${x}x${y}.${type};
        done
    done
}

#---------------------------------------------------------------
# internal: core
#---------------------------------------------------------------

mkdir -p "${HERE}/${TEMP}";
mkdir -p "${HERE}/${KEEP}";

##
# Fetch the specified brand asset bundle from distfiles.
#
# TODO: error checking, return codes etc.
#
(
    cd "${TEMP}";

    if ! test -e ${NAME}-${HASH}.tar.gz; then
        curl -O "${HOST}"/source/${NAME}/${NAME}-${HASH}.tar.gz;
    fi

    tar -xf ${NAME}-${HASH}.tar.gz;
)


#---------------------------------------------------------------
# internal: generate minimal wallpapers
#---------------------------------------------------------------

##
# The default "core" or "slim" wallpaper is made on the fly and
# not provided as a rasterized image from the start.
#

##
# make_fake GRADIENT_FROM GRADIENT_TO XxY TEMPLATE OUTPUT
#
make_fake ()
{
    size="${1}"; shift; # geometry
    hexa="${1}"; shift; # gradient from
    hexb="${1}"; shift; # gradient to
    temp="${1}"; shift; # template
    name="${1}"; shift; # output filename

    printf "Generating %5s ('%s')...\n" "${size}" "${name}";
    magick \
        -define gradient:angle=65 \
        -size ${size} \
        gradient:"${hexa}"-"${hexb}" \
        "${name}" \
        ;

    # FIXME: this is literal magic?
    composite \
        -compose atop \
        -gravity southwest \
        -background none \
        "${TEMP}"/polyguin.svg \
        "${name}" \
        "${name}" \
        ;
    composite \
        -compose atop \
        -gravity southeast \
        -background none \
        "${TEMP}"/gen_polylogo_template_${temp}.png \
        -geometry +50+20 \
        "${name}" \
        "${name}" \
        ;
}


##
# Generate starting templates. That is, generate the largest one
# possible for each aspect ratio.
#
printf "%s\n" "${MAKE}" | while read ratio scale _; do
    # compute largest required
    x=$((${scale}*${ratio%:*}));
    y=$((${scale}*${ratio#*:}));

    ############################################################
    # FIXME!
    # FOR SOME REASON, CHANGING '#EEEEEF' TO '#EEEEEE' OR FROM
    # '#303031' TO '#303030' WILL DISCARD COLOR INFORMATION OF
    # THE COMPOSITED SVG. WTF. WE LEVERAGE THIS THOUGH...
    ############################################################
    
    # background (we want the buggy black/white version)
    mkdir -p "${TEMP}"/generated/background;
    make_fake ${x}x${y} "#bbbbbb" "#eeeeee" black_x200 "${TEMP}"/generated/background/${x}x${y}.png;
    #make_fake ${x}x${y} "#bbbbbb" "#eeeeef" black_x200 "${TEMP}"/generated/background/${x}x${y}.png;

    # lockscreen (we want the color version)
    mkdir -p "${TEMP}"/generated/lockscreen;
    #make_fake ${x}x${y} "#151515" "#303030" white_x200 "${TEMP}"/generated/lockscreen/${x}x${y}.png;
    make_fake ${x}x${y} "#151515" "#303031" white_x200 "${TEMP}"/generated/lockscreen/${x}x${y}.png;
done


#---------------------------------------------------------------
# internal: scale all wallpapers
#---------------------------------------------------------------

##
# To generate "real" wallpapers, we simply resize them. This can
# be be improved significantly, but that is a future problem.
#
make_real ()
{
    size="${1}"; shift; # geometry
    temp="${1}"; shift; # template
    name="${1}"; shift; # output filename

    # TODO: add filename extension checker?

    printf "Generating %5s ('%s')...\n" "${size}" "${name}";
    convert "${file}" -resize ${size}^ "${name}";
}


##
# Source images may not all be consistent in their aspect ratios
# or dimensions. We will scale (crop) source images to match the
# sizes of the generated core templates.
#
printf "%s\n" "${MAKE}" | while read ratio scale _; do
    # compute largest required
    x=$((${scale}*${ratio%:*}));
    y=$((${scale}*${ratio#*:}));

    find "${IMGS}" -mindepth 1 -maxdepth 1 -type d | sort | while read k; do
        file="$(find ${k} -type f -name 'image.*')";

        if test $(printf "%s\n" "${file}" | wc -l) -ne 1; then
            printf "E: not exactly one image at '%s'!\n" "${k}";
            exit 1;
        fi

        # FIXME: is it OK to hard-code '.png' here?
        name=$(printf "%s\n" "${file}" | cut -d/ -f2); # sloppy, sorry
        mkdir -p "${TEMP}"/generated/${name};
        make_real ${x}x${y} "${file}" "${TEMP}"/generated/${name}/${x}x${y}.png;
    done
done


#---------------------------------------------------------------
# output: scale all wallpapers
#---------------------------------------------------------------

##
# For each input image, generate a full set of output images.
#
# Note that this loop includes all columns; rely on 'size_only'
# to handle the rest.
#
printf "%s\n" "${MAKE}" | while read k; do
    size_only "${TEMP}"/generated ${k};
done


#---------------------------------------------------------------
# output: metadata
#---------------------------------------------------------------

##
# FIXME: make this not ugly
#
temp=$(mktemp);
cat >> "${temp}" <<"EOF"
gnome-background-properties/Adelie.xml 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wallpapers SYSTEM "gnome-wp-list.dtd">
<wallpapers>
EOF

find "${TEMP}"/generated -mindepth 1 -maxdepth 1 -type d | sort | while read k; do
    name=$(printf "%s\n" "${k}" | cut -d/ -f3); # sloppy, sorry
    # fix structure
    mv "${KEEP}"/${name} "${KEEP}"/${name}.tmp;
    mkdir -p "${KEEP}"/${name}/contents;
    mv "${KEEP}"/${name}.tmp "${KEEP}"/${name}/contents/images;

    # compute metadata
    case "${name}" in
        background)
            description="Default Background";
            author_name="Adélie Platform Team";
            ;;
        lockscreen)
            description="Default Lockscreen";
            author_name="Adélie Platform Team";
            ;;
        *)
            . "${IMGS}"/${name}/MANIFEST;
            ;;
    esac

    # write metadata to 'manifest.desktop'
    # FIXME: we can do better than 'tr' below, but also what are
    # the actual requirements? I'm just emulating what exists...
    cat > "${KEEP}"/${name}/contents/metadata.desktop <<EOF
[Desktop Entry]
Name=${description}
X-KDE-PluginInfo-Name=$(printf "%s\n" "${description}" | tr ' ' '-')
X-KDE-PluginInfo-Author=${author_name}
X-KDE-PluginInfo-License=CC-BY-SA-4.0
EOF

    # write metadata to 'gnome-background-properties/Adelie.xml'
    find "${KEEP}"/${name}/contents/images -type f | grep -v metadata.desktop | sort | while read file; do
        cat >> "${temp}" <<EOF
  <wallpaper deleted="false">
    <name>${description}</name>
    <filename>/usr/share/wallpapers/${name}/contents/images/${file##*/}</filename>
    <options>zoom</options>
  </wallpaper>
EOF
    done
done

##
# FIXME: make this not ugly
#
cat >> "${temp}" <<EOF
</wallpapers>
EOF


#---------------------------------------------------------------
# output: create tarball
#---------------------------------------------------------------

mkdir -p adelie-wallpapers-${VTAG}/usr/share;

# wallpapers
mv "${KEEP}" adelie-wallpapers-${VTAG}/usr/share/wallpapers;

# gnome-background-properties
mkdir -p adelie-wallpapers-${VTAG}/usr/share/gnome-background-properties;
mv "${temp}" adelie-wallpapers-${VTAG}/usr/share/gnome-background-properties/Adelie.xml;

# generate tarball
tar -pcJf adelie-wallpapers-${VTAG}.tar.xz adelie-wallpapers-${VTAG};

# clean up
rm -fr "${TEMP}"/generated;
rm -fr adelie-wallpapers-${VTAG};