My Reddit Wallpaper Downloader Script

My i3wm setup with amazing gruvbox color scheme and a wallpaper from Reddit

i3wm setup with amazing gruvbox color scheme and a wallpaper from Reddit

Update [2019/05/08]: Many people have been asking for the wallpaper in the above screenshot. It is from System Failure II, oil on canvas, 31x43” on r/Art.

Well, I am really fascinated by Reddit art and genuine creative ideas such as Scrolller which was made possible thanks to gazillions of art pieces scattered throughout various art subreddits. I am also fascinated by Unix philosophy and have been a *nix enthusiast for as long as I can remember. In addition to all this, the discovery of r/unixporn - realizing I am not the only one who cares about aesthetics of their Unix box - was a huge blow for me; to the point that studying the GitHub dotfiles posted along the screenshots on r/unixporn by fellow nix-enthusiast redditors felt like a day to day hubby for me.

All the while, I had a successful experiment with writing a complex piece of real-world software in pure Bash with an amazingly wide range of features for around 3.5K lines of code. The real excitement came when it made to the official FreeBSD Ports Tree. In spite of the fact that many people find Bash syntax annoyingly ugly and unmaintainable and often wonder why do people still write shell scripts by asking it on Quora, since MS-DOS 6.22 era, I did develop a certain love–hate relationship with shell scripting languages such as Batch Files, Bash, etc. Thus, still I do automate almost everything with these ancient technologies.

So, here is my fully-configurable wallpaper changer software written in bash which automagically fetches and display wallpapers from your favorite subs. It has been powering and brightening up my i3wm setup for the past eight months which led me to the conclusion that it deserves a proper introduction.

Requirements and Dependencies

Taking a look at the source code reveals the necessitas for running the script successfully:

readonly BASENAME="basename"
readonly CALLER="caller"
readonly CUT=$(which cut 2>/dev/null)z
readonly CURL=$(which curl 2>/dev/null)
readonly DATE=$(which date 2>/dev/null)
readonly ECHO="echo"
readonly ECHO_FMT="echo -e"
readonly ESETROOT=$(which esetroot 2>/dev/null)
readonly FEH=$(which feh 2>/dev/null)
readonly FIREFOX=$(which firefox 2>/dev/null)
readonly HSETROOT=$(which hsetroot 2>/dev/null)
readonly JQ=$(which jq 2>/dev/null)
readonly LOGGER="logger"
readonly PERL=$(which perl 2>/dev/null)
readonly PRINT="print"
readonly REV=$(which rev 2>/dev/null)
readonly TR=$(which tr 2>/dev/null)
readonly XSETROOT=$(which xsetroot 2>/dev/null)

Most of the dependencies in this list are either internal shell commands or are already present on a nix system such as *BSDs or common GNU/Linux systems. Otherwise, it must be installed from your distro’s package manager. The ones that may not be present by default are curl, feh, and jq. Between esetroot, hsetroot and xsetroot only one of them is required since they could be used interchangeably. Firefox is not a hard-dependency and can be avoided at cost of a warning:

[WARNING] 288 Firefox executable not found!
[WARNING] 290 Setting Firefox version string to: Mozilla Firefox 66.0

Firefox is being used to fake the user agent while downloading meta data or actual images from Reddit. If the script won’t be able to find it, it sets the version number to the latest version of Firefox which at the time of this writing is: Mozilla Firefox 66.0.

Usage Syntax

Before running the script, please note that it must has the executable permission set on it. If not, in order to grant executable permission for all users:

$ chmod a+x /path/to/reddit-wallpaper.sh

Or, only the current user (the user who owns the file):

$ chmod u+x /path/to/reddit-wallpaper.sh

Or, the users under the group who owns the file:

$ chmod g+x /path/to/reddit-wallpaper.sh

Getting away from the basics, one can simply run the script by issuing the following command and it will download and set a wallpaper from Reddit for you, since it assumes some defaults in case it’s not given the required arguments:

$ /path/to/reddit-wallpaper.sh

[INFO] 304 Run 'reddit-wallpaper.sh -h' for more information on available options.
[INFO] 306 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'...
[INFO] 310 Downloading meta file 'https://www.reddit.com/r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology/hot.json'...
[INFO] 381 Found a wallpaper on 'r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology' at 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg'!
[INFO] 382 Fetching 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg'...
[INFO] 392 Setting desktop background color to '#282828'...
[INFO] 402 Using 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg' as the desktop wallpaper...
[INFO] 412 Done!
[INFO] 413 Hope you enjoy it :)

So far so good! Now, the correct usage syntax for the script is as follows:

Correct usage:

    reddit-wallpaper.sh -h | [-r {r/subreddit}] [-s {sorty by}] [-n] [-b {background color}] [-f 'feh args']

    -h: shows this usage note

    -r: subreddit name or names prefixed with r/ and combined by a + sign (e.g. r/Art or r/Art+ArtPorn; default: r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology)

    -s: reddit sort algorithm (e.g. hot, new, controversial, top, rising; default: hot)

    -n: allow nsfw wallpapers (no nsfw wallpaper is allowed by default, unless this flag is passed)

    -b: hex rgb color in 'ffffff' format (default: 282828)

    -f: feh arguments to pass; run 'man feh' for a list of available options (default: --no-fehbg --image-bg black --bg-max)

One can always get this information by utilizing -h option:

$ /path/to/reddit-wallpaper.sh -h

Here is the detailed explanation for the rest of the options:

  • -r: A typical sub-reddit url looks like https://reddit.com/r/SomeCoolSubRedditA or https://reddit.com/r/SomeCoolSubRedditB. If one desires, they may mix-up or simply join various subs by concatenating them; e.g., https://reddit.com/r/SomeCoolSubRedditA+SomeCoolSubRedditB+SomeCoolSubRedditC. This is one of the coolest features unique to Reddit among the social media. In the -r parameter the user should specify the name of the sub-reddit prefixed with r/ without the https://reddit.com/ part of the URL; e.g. r/SomeCoolSubRedditA or r/SomeCoolSubRedditA+SomeCoolSubRedditB+SomeCoolSubRedditC. For starters, one can take a look at the script’s source code which provides a list consisting of some of the best subreddits in various categories such as Animals, Art, Food, Imaginary, Man-made, and Nature. By default the script uses everything under the Art category. If one desires to find more subreddits they could navigate to Scrolller and after clicking the gear icon on the top-right corner, reveal the category information by enabling the Category info option which not only lists the top subreddits but also provides a list of sub-reddits in various sub-categories for the chosen category. There is also a NSFW (Not Safe For Work) category on Scrolller which is not included in the script since many are not comfortable with it

  • -s: Specifies the Reddit’s sort algorithm for the chosen subreddit(s). The valid values for this parameter are hot, new, controversial, top, and rising. Reddit’s default is hot. So, if not specified by the user, the script chooses hot as the default for for -s parameter. This is important since the script chooses the first wallpaper from the sorted list. As an example, if someone goes with top option, the wallpaper they get may not change for days or even months, even if they run the script one million times. Or, if the new option is chosen you may get a different wallpaper on each run of the script as it fetches the latest uploaded images by redditors, but it might not be much of an interesting image.

readonly SUBREDDIT_CATEGORY_ANIMALS="AnimalsBeingBros+AnimalsBeingDerps+AnimalsBeingJerks+aww+Eyebleach+likeus+rarepuppers"
readonly SUBREDDIT_CATEGORY_ART="Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture"
readonly SUBREDDIT_CATEGORY_FOOD="Breadit+eatsandwiches+food+FoodPorn+grilledcheese+Pizza+slowcooking"
readonly SUBREDDIT_CATEGORY_IMAGINARY="ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology"
readonly SUBREDDIT_CATEGORY_MAN_MADE="AbandonedPorn+carporn+CityPorn+CozyPlaces+DesignPorn+powerwashingporn+RoomPorn"
readonly SUBREDDIT_CATEGORY_NATURE="chemicalreactiongifs+EarthPorn+MacroPorn+physicsgifs+spaceporn+waterporn+WeatherGifs"
  • -n: The script won’t download any NSFW (Not Safe For Work) image by default unless this flag is passed in the parameter list.

  • -b: Specifies the background color so in case that the downloaded image size does not match your screen size exactly, the extra space around the picture is going to be filled with this color instead. This parameter expects a color in HEX RGB format without the # character. For example, for red or magenta color one could pass ff0000, or ff00ff respectively.

  • -f Anything passed as this parameter’s value will be directly passed to feh. feh is an amazing tool with a lot of features. It also supports multi-display setups. As a sane default which works in both single and multi-head setups, the script passes along –no-fehbg –image-bg black –bg-max arguments to feh by default if this parameter is not specified. For more information, consult the feh man page by entering the following command:

$ man feh

For the convenience of description, the following example tries to fetch the most recent picture submitted to any of r/Art, r/ImaginaryLandscapes, r/ImaginaryMonsters, or r/itookapicture subs. It does not care if it is safe for work or not (e.g. containing nudity, violence, …). It fills the extra space around the wallpaper to black if there are any. And, finally it tells feh to not write a ~/.fehbg file and center the wallpaper on the background. Notice that we did wrap feh parameters in a pair of double quotes, otherwise the script stops with an error complaining about the invalid options it was given.

$ /path/to/reddit-wallpaper.sh \
    -r r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture \
    -s new \
    -n \
    -b "000000" \
    -f "--no-fehbg --bg-center"

[INFO] 308 Run 'reddit-wallpaper.sh -h' for more information on available options.
[INFO] 310 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'...
[INFO] 314 Downloading meta file 'https://www.reddit.com/r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture/new.json'...
[INFO] 385 Found a wallpaper on 'r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture' at 'https://i.redd.it/w38h6jmwa1x21.jpg'!
[INFO] 386 Fetching 'https://i.redd.it/w38h6jmwa1x21.jpg'...
[INFO] 396 Setting desktop background color to '#000000'...
[INFO] 406 Using 'https://i.redd.it/w38h6jmwa1x21.jpg' as the desktop wallpaper...
[INFO] 416 Done!
[INFO] 417 Hope you enjoy it :)

Running through a Cron Job

I have already wrote a guide on how to properly add a cron job on *nix systems, so I won’t go through this in details. Fire up the crontab file in your favorite editor by issuing:

$ sudo -u user -g group -H crontab -e

In my case, the user and the group for my account is mamadou; so it would be:

$ sudo -u mamadou -g mamadou -H crontab -e

I am running the script every 4 hours:

# At minute 0 past every 4th hour
0   */4   *   *   *   export DISPLAY=:0; /path/to/reddit-wallpaper.sh > /dev/null 2>&1

If you are not familiar with the crontab syntax, crontab.guru is a great visual aid.

Note that the export DISPLAY=:0; part before calling the script is mandatory since we are going to run our script through a cron job. If one forgets to export the correct display before running the script from the crontab, they will get the following error:

Cannot open X display!

On another note, due to the fact that this script is designed to run as a cron job, in addition to stdout and stderr, the scripts logs are getting passed through to the system’s log file. On my Gentoo or FreeBSD instances this file is located at /var/log/messages. This is a sample system log output taken from the example in the previous section:

$ tail -f /var/log/messages

May  8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 308 Run 'reddit-wallpaper.sh -h' for more information on available options.
May  8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 310 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'...
May  8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 314 Downloading meta file 'https://www.reddit.com/r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture/new.json'...
May  8 20:57:45 mamadou-pc REDDIT-WALLPAPER: INFO 385 Found a wallpaper on 'r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture' at 'https://i.redd.it/w38h6jmwa1x21.jpg'!
May  8 20:57:45 mamadou-pc REDDIT-WALLPAPER: INFO 386 Fetching 'https://i.redd.it/w38h6jmwa1x21.jpg'...
May  8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 396 Setting desktop background color to '#000000'...
May  8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 406 Using 'https://i.redd.it/w38h6jmwa1x21.jpg' as the desktop wallpaper...
May  8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 416 Done!
May  8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 417 Hope you enjoy it :)

Wallpaper Cache Location

When the scripts finds and fetches an image from Reddit successfully it saves the wallpaper in the wallpaper cache located inside the user’s home directory located at ~/.cache/reddit_wallpapers in the YEAR-MONTH-DAY-HOUR-MINUTE-SECONDS.EXTENSION format; e.g. 2019-05-08-04-00-01.jpg. For obvious reasons this cache grows over time, thus, one should take care of cleaning it up manually, themselves.

In the future releases, I may add various clean up options and strategies, but for the time being I am happy with keeping the cache and removing it by hand after making a copy whenever I have to.

Obtaining the Source Code

The source code is available on both GitHub and GitLab for the sake of convenience. In order to download the source code using curl, aria2, wget directly:

# GitHub
$ curl -fLo /path/to/reddit-wallpaper.sh \
    --create-dirs \
    https://raw.githubusercontent.com/NuLL3rr0r/reddit-wallpaper/master/reddit-wallpaper.sh

# GitLab
$ curl -fLo /path/to/reddit-wallpaper.sh\
    --create-dirs \
    https://gitlab.com/NuLL3rr0r/reddit-wallpaper/raw/master/reddit-wallpaper.sh

It is also possible to obtain the whole repository by cloning it from git:

# GitHub
$ git clone \
    https://github.com/NuLL3rr0r/reddit-wallpaper.git \
    /path/to/clone/reddit-wallpaper

# GitLab
$ git clone \
    https://gitlab.com/NuLL3rr0r/reddit-wallpaper.git \
    /path/to/clone/reddit-wallpaper

Alternatively, it can be copy-pasted directly from here, which is strongly discouraged due to Pastejacking Exploitation Technique:

reddit-wallpaper.sh
  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
#!/usr/bin/env bash

#  (The MIT License)
#
#  Copyright (c) 2018 - 2019 Mamadou Babaei
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.


set +e

readonly FMT_OFF='\e[0m'
readonly FMT_INFO='\e[1;32m'
readonly FMT_WARN='\e[1;33m'
readonly FMT_ERR='\e[1;91m'
readonly FMT_FATAL='\e[1;31m'

readonly LOG_INFO="INFO"
readonly LOG_WARN="WARNING"
readonly LOG_ERR="ERROR"
readonly LOG_FATAL="FATAL"

readonly E_TRUE="true"
readonly E_FALSE="false"

readonly BASENAME="basename"
readonly CALLER="caller"
readonly CUT=$(which cut 2>/dev/null)
readonly CURL=$(which curl 2>/dev/null)
readonly DATE=$(which date 2>/dev/null)
readonly ECHO="echo"
readonly ECHO_FMT="echo -e"
readonly ESETROOT=$(which esetroot 2>/dev/null)
readonly FEH=$(which feh 2>/dev/null)
readonly FIREFOX=$(which firefox 2>/dev/null)
readonly HSETROOT=$(which hsetroot 2>/dev/null)
readonly JQ=$(which jq 2>/dev/null)
readonly LOGGER="logger"
readonly PERL=$(which perl 2>/dev/null)
readonly PRINT="print"
readonly REV=$(which rev 2>/dev/null)
readonly TR=$(which tr 2>/dev/null)
readonly XSETROOT=$(which xsetroot 2>/dev/null)

if [[ -n "${ESETROOT}" ]] ;
then
    readonly SETROOT=${ESETROOT}
elif [[ -n "${HSETROOT}" ]] ;
then
    readonly SETROOT=${HSETROOT}
elif [[ -n "${XSETROOT}" ]] ;
then
    readonly SETROOT=${XSETROOT}
fi

readonly SCRIPT="${BASH_SOURCE[0]}"
readonly SCRIPT_NAME="$(${BASENAME} -- "${SCRIPT}")"
readonly SYSLOG_TAG="$(${BASENAME} -- "${SCRIPT}" | ${TR} '[:lower:]' '[:upper:]' | ${REV} | ${CUT} -d "." -f2- | ${REV})"

readonly SUBREDDIT_CATEGORY_ANIMALS="AnimalsBeingBros+AnimalsBeingDerps+AnimalsBeingJerks+aww+Eyebleach+likeus+rarepuppers"
readonly SUBREDDIT_CATEGORY_ART="Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture"
readonly SUBREDDIT_CATEGORY_FOOD="Breadit+eatsandwiches+food+FoodPorn+grilledcheese+Pizza+slowcooking"
readonly SUBREDDIT_CATEGORY_IMAGINARY="ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology"
readonly SUBREDDIT_CATEGORY_MAN_MADE="AbandonedPorn+carporn+CityPorn+CozyPlaces+DesignPorn+powerwashingporn+RoomPorn"
readonly SUBREDDIT_CATEGORY_NATURE="chemicalreactiongifs+EarthPorn+MacroPorn+physicsgifs+spaceporn+waterporn+WeatherGifs"

readonly DEFAULT_SUBREDDIT="r/${SUBREDDIT_CATEGORY_ART}+${SUBREDDIT_CATEGORY_IMAGINARY}"
readonly DEFAULT_SORT_BY="hot"
readonly DEFAULT_IS_NSFW_OK="${E_FALSE}"
readonly DEFAULT_BACKGROUND_COLOR="282828"
readonly DEFAULT_FEH_ARGS="--no-fehbg --image-bg black --bg-max"

readonly DEFAULT_FIREFOX_VERSION_STRING="Mozilla Firefox 66.0"

readonly LOCAL_WALLPAPER_DIR="${HOME}/.cache/reddit_wallpapers/"
readonly LOCAL_WALLPAPER_NAME="$(${DATE} +%Y-%m-%d-%H-%M-%S)"

function usage() {
    readonly local message="${1}"

    ${ECHO}

    if [[ -n "${message}" ]] ;
    then
        err "${message}${FMT_OFF}"
        ${ECHO}
    fi

    ${ECHO_FMT} "${FMT_INFO}Correct usage:${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}${SCRIPT_NAME} -h | [-r {r/subreddit}] [-s {sorty by}] [-n] [-b {background color}] [-f 'feh args']${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-h: shows this usage note"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-r: subreddit name or names prefixed with r/ and combined by a + sign (e.g. r/Art or r/Art+ArtPorn; default: ${DEFAULT_SUBREDDIT})${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-s: reddit sort algorithm (e.g. hot, new, controversial, top, rising; default: ${DEFAULT_SORT_BY})${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-n: allow nsfw wallpapers (no nsfw wallpaper is allowed by default, unless this flag is passed)${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-b: hex rgb color in 'ffffff' format (default: ${DEFAULT_BACKGROUND_COLOR})${FMT_OFF}"
    ${ECHO}
    ${ECHO_FMT} "    ${FMT_INFO}-f: feh arguments to pass; run 'man feh' for a list of available options (default: ${DEFAULT_FEH_ARGS})${FMT_OFF}"
    ${ECHO}

    if [[ -n "${message}" ]] ;
    then
        exit 1
    else
        exit 0
    fi
}

function log()
{
    local log_type=$1; shift
    local line=$1; shift
    local fmt=$1; shift

    if [[ -n "$1" && -n "$@" ]] ;
    then
        ${ECHO_FMT} "${fmt}[${log_type}] ${line} $@${FMT_OFF}"
        ${LOGGER} -t "${SYSLOG_TAG}" "${log_type} ${line} $@"
    else
        ${ECHO_FMT} "${FMT_WARN}[${LOG_WARN}] ${line} A null log detected!${FMT_OFF}"
        ${LOGGER} -t "${SYSLOG_TAG}" "${LOG_WARN} ${line} A null log detected!"
    fi
}

function info()
{
    local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )

    log "${LOG_INFO}" "${line}" "${FMT_INFO}" "$@"
}

function warn()
{
    local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )

    log "${LOG_WARN}" "${line}" "${FMT_WARN}" "$@"
}

function err()
{
    local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )

    log "${LOG_ERR}" "${line}" "${FMT_ERR}" "$@"
}

function fatal()
{
    local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )

    log "${LOG_FATAL}" "${line}" "${FMT_FATAL}" "$@"

    exit 1
}

if [[ -z "${CUT}" ]] ;
then
    fatal "Could not find command cut!"
fi

if [[ -z "${CURL}" ]] ;
then
    fatal "Could not find command curl!"
fi

if [[ -z "${DATE}" ]] ;
then
    fatal "Could not find command date!"
fi

if [[ -z "${FEH}" ]] ;
then
    fatal "Could not find command feh!"
fi

if [[ -z "${JQ}" ]] ;
then
    fatal "Could not find command jq!"
fi

if [[ -z "${PERL}" ]] ;
then
    fatal "Could not find command perl!"
fi

if [[ -z "${REV}" ]] ;
then
    fatal "Could not find command rev!"
fi

if [[ -z "${SETROOT}" ]] ;
then
    fatal "Could not find any setroot command!"
fi

if [[ -z "${TR}" ]] ;
then
    fatal "Could not find command tr!"
fi

while getopts ":h :r: :s: :n 🅱️ :f:" ARG;
do
    case ${ARG} in
        h)
            usage
            ;;
        r)
            SUBREDDIT=${OPTARG}
            ;;
        s)
            SORT_BY=${OPTARG}
            ;;
        n)
            IS_NSFW_OK="${E_TRUE}"
            ;;
        b)
            BACKGROUND_COLOR="#${OPTARG}"
            ;;
        f)
            FEH_ARGS="${OPTARG}"
            ;;
        \?)
            usage "Invalid option: '-${OPTARG}'!"
        ;;
    esac
done

if [[ -z ${SUBREDDIT} ]] ;
then
    SUBREDDIT=${DEFAULT_SUBREDDIT}
fi

readonly SUBREDDIT_REGEX="^r\/([a-zA-Z0-9_+]+)$"

if [[ ! "${SUBREDDIT}" =~ ${SUBREDDIT_REGEX} ]] ;
then
    fatal "Invalid subreddit name! use r/subreddit or r/subreddit1+subreddit2 format!"
fi

if [[ -z ${SORT_BY} ]] ;
then
    SORT_BY=${DEFAULT_SORT_BY}
fi

if [[ "${SORT_BY}" != "hot"
        && "${SORT_BY}" != "new"
        && "${SORT_BY}" != "controversial"
        && "${SORT_BY}" != "top"
        && "${SORT_BY}" != "rising" ]] ;
then
    fatal "Invalid reddit sort algorithm!"
fi

if [[ -z ${IS_NSFW_OK} ]] ;
then
    NSFW_OK=${DEFAULT_IS_NSFW_OK}
fi

if [[ -z ${BACKGROUND_COLOR} ]] ;
then
    BACKGROUND_COLOR="#${DEFAULT_BACKGROUND_COLOR}"
fi

readonly BACKGROUND_COLOR_REGEX="^#([0-9a-f]{6})$"

if [[ ! "${BACKGROUND_COLOR}" =~ ${BACKGROUND_COLOR_REGEX} ]] ;
then
    fatal "Invalid background color format! the only accepted format is 'ffffff'!"
fi

if [[ -z ${FEH_ARGS} ]] ;
then
    FEH_ARGS=${DEFAULT_FEH_ARGS}
fi

if [[ -z "${FIREFOX}" ]] ;
then
    warn "Firefox executable not found!"
    readonly FIREFOX_VERSION_STRING="${DEFAULT_FIREFOX_VERSION_STRING}"
    warn "Setting Firefox version string to: ${FIREFOX_VERSION_STRING}"
else
    readonly FIREFOX_VERSION_STRING=$(${FIREFOX} -version)
fi

readonly FIREFOX_VERSION_NUMBER=$(${ECHO} "${FIREFOX_VERSION_STRING}" | ${PERL} -nle "m/[-+]?([0-9]*\.[0-9]+|[0-9]+)/; ${PRINT} \$1")
readonly FIREFOX_USER_AGENET="Mozilla/5.0 (X11; Linux x86_64; rv:${FIREFOX_VERSION_NUMBER}) Gecko/20100101 Firefox/${FIREFOX_VERSION_NUMBER}"

info "Run '${SCRIPT_NAME} -h' for more information on available options."

info "Setting user agent to '${FIREFOX_USER_AGENET}'..."

readonly JSON_URL="https://www.reddit.com/${SUBREDDIT}/${SORT_BY}.json"

info "Downloading meta file '${JSON_URL}'..."

readonly JSON_CONTENT=$(${CURL} -A "${FIREFOX_USER_AGENET}" -sSL ${JSON_URL})
RC=$?

if [[ $RC -ne 0 ]] ;
then
    fatal "Subreddit meta file download has failed!"
fi

readarray JSON_POSTS <<< "$(${ECHO} "${JSON_CONTENT}" | ${JQ} --compact-output '.data.children[]')"
RCS=(${PIPESTATUS[*]})

if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
then
    fatal "Failed to parse the subreddit's meta file!"
fi

readonly URL_REGEX="^(https?://)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)\\.(png|apng|jpg|jpeg|jpe|jif|jfif|jfi|gif|tiff|tif)$"

FOUND_A_SUITABLE_WALLPAPER="${E_FALSE}"

for post in "${JSON_POSTS[@]}" ;
do
    if [[ "${IS_NSFW_OK}" != "${E_TRUE}" ]] ;
    then
        IS_OVER_18_POST=$(${ECHO} "${post}" | ${JQ} --compact-output '.data.over_18')
        RCS=(${PIPESTATUS[*]})

        if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
        then
            fatal "Failed to parse the subreddit's meta file!"
        fi

        if [[ "${IS_OVER_18_POST}" == "${E_TRUE}" ]] ;
        then
            continue
        fi
    fi

    WALLPAPER_URL=$(${ECHO} "${post}" | ${JQ} --raw-output '.data.url')
    RCS=(${PIPESTATUS[*]})

    if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
    then
        fatal "Failed to parse the subreddit's meta file!"
    fi

    if [[ ! "${WALLPAPER_URL}" =~ ${URL_REGEX} ]] ;
    then
        continue
    fi

    WALLPAPER_EXTENSION="${WALLPAPER_URL##*.}"

    if [[ -z "${WALLPAPER_EXTENSION}" ]] ;
    then
        continue
    fi

    FOUND_A_SUITABLE_WALLPAPER="${E_TRUE}"
    break;
done

if [[ "${FOUND_A_SUITABLE_WALLPAPER}" != "${E_TRUE}" ]] ;
then
    fatal "Could not find a suitable wallpaper on '${SUBREDDIT}'!"
fi

readonly LOCAL_WALLPAPER_PATH="${LOCAL_WALLPAPER_DIR}/${LOCAL_WALLPAPER_NAME}.${WALLPAPER_EXTENSION}"

info "Found a wallpaper on '${SUBREDDIT}' at '${WALLPAPER_URL}'!"
info "Fetching '${WALLPAPER_URL}'..."

${CURL} -fLo "${LOCAL_WALLPAPER_PATH}" --create-dirs ${WALLPAPER_URL} > /dev/null 2>&1
RC=$?

if [[ $RC -ne 0 ]] ;
then
    fatal "Failed to fetch the wallpaper file from '${WALLPAPER_URL}'!"
fi

info "Setting desktop background color to '${BACKGROUND_COLOR}'..."

${SETROOT} -solid "${BACKGROUND_COLOR}" > /dev/null 2>&1 &
RC=$?

if [[ $RC -ne 0 ]] ;
then
    fatal "Failed to set the background color to '${BACKGROUND_COLOR}'!"
fi

info "Using '${WALLPAPER_URL}' as the desktop wallpaper..."

${FEH} ${FEH_ARGS} "${LOCAL_WALLPAPER_PATH}" > /dev/null 2>&1 &
RC=$?

if [[ $RC -ne 0 ]] ;
then
    fatal "Failed to apply '${LOCAL_WALLPAPER_PATH}' as the desktop background image!"
fi

info "Done!"
info "Hope you enjoy it :)"