djcev.com

//

Git Repos / dotfiles / config / redshift / hooks / background.sh

Last update to this file was on 2021-12-13 at 16:05.

Show background.sh

#!/bin/sh
#
# Copyright (c) 2021, Cameron Vanderzanden. Available under the
# terms of the 2-clause BSD license. Please see the file "LICENSE" in
# the root dir of this repository for more information.
#
#- config/redshift/hooks/background.sh 2020/05/12 cev, last updated 2021/12/12
## Usage: background.sh [-hvV] command argument1 argument2
##
## Redshift hook script for automated background setting based on
## day/night period. Requires a directory of background files and
## a directory of character files.
##
## -h Show help.
## -v Verbose output (for debugging? purposes).
## -V Print version info.
##
## Valid commands are currently:
## clear Clear temporary files.
## period-changed Set background to one corresponding with argument2.
##
## This script expects wallpaper filenames to have three fields separated
## by an underscore: name, scene name, and period (day, interval, and
## night). For example:
## game1_scene1_day.jpg
## game1_scene1_interval.jpg
## game1_scene1_night.jpg
## game1_scene2_day.jpg
## ... and so on.
##
## Character filenames are similarly separated by underscore and have four
## fields: name, character name, period, and s1 through s3 (where s1 is
## displayed on low system load average and s3 on high). An example:
## game1_character1_day_s1.jpg
## game1_character1_day_s2.jpg
## game1_character1_day_s3.jpg
## game1_character1_interval_s1.jpg
## ... and so on, filling out the necessary combinations.
##
## For more information read the script, it's not too complicated.
##
#

[ -x $HOME/bin/cev_print.sh ] && . $HOME/bin/cev_print.sh

self="${0##*'/'}"
verbose=""

# default directories
tmpdir="/tmp/back_$(id -u -n)"
walldir="/z/gfx/wallpapers"
chardir="/z/gfx/rootchar"

# default tempfiles
gamef="$tmpdir/game"
charf="$tmpdir/character"
scenef="$tmpdir/scene"

# placeholders; used & defined below
game=""
char=""
scene=""

randlist() {
# max for rng; exit if zero.
max="$(wc -l $1)"
max=${max%' '*}
[ $max -lt 1 ] && exit 1
# random number within 1-$max
if [ -x "$(which jot)" ]; then
n=$(jot -r 1 1 $max) # BSD
elif [ -x "$(which shuf)" ]; then
n=$(shuf -i 1-$max -n 1) # GNU; may or may not work
else
# this really doesn't work. awk rand() seems to be the problem.
etime=$(date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s")
r=$(awk "BEGIN {srand($etime); print rand()}")
n=$(expr ${r##*.} % $max)
unset etime && unset r
fi
sed -n "${n}p" $1
}

select_game() {
find "$walldir" -type f -iname "*jpg" -print | while read r; do
basename "$r" | cut -d '_' -f 1 >> ${gamef}s
done
# don't do a uniq pass here
randlist ${gamef}s > ${gamef}
rm ${gamef}s
}

select_character() {
find "$chardir" -type f -iname "${game}*" -print | while read r; do
basename "$r" | cut -d '_' -f 2 >> ${charf}1
done
cat ${charf}1 | uniq >> ${charf}s
randlist ${charf}s > ${charf}
rm ${charf}1 ${charf}s
}

select_scene() {
find "$walldir" -type f -iname "${game}*" -print | while read r; do
basename "$r" | cut -d '_' -f 2 >> ${scenef}1
done
cat ${scenef}1 | uniq >> /${scenef}s
randlist ${scenef}s | cut -d '.' -f 1 > ${scenef}
rm ${scenef}1 ${scenef}s
}

select_set() {
select_game
game="$(cat ${gamef})"
[ "$verbose" ] && print_self && printf "select_set: game %s\n" "$game"
select_character
char="$(cat ${charf})"
[ "$verbose" ] && print_self && printf "select_set: character %s\n" "$char"
select_scene
scene="$(cat ${scenef})"
[ "$verbose" ] && print_self && printf "select_set: scene %s\n" "$scene"
}

check_set() {
if [ ! -f ${gamef} -o ! -f ${charf} -o ! -f ${scenef} ]; then
select_set
else
last=$(stat -f %m ${charf})
curr=$(date +%s)
elapsed=$(($curr - $last))
elapsed=$(($elapsed / 3600))
if [ $elapsed -gt 22 ]; then
[ "$verbose" ] && print_self && \
printf "selection is over 22 hours old.\n"
select_set
else
game="$(cat ${gamef})"
char="$(cat ${charf})"
scene="$(cat ${scenef})"
fi
fi
}

clear_character_images() {
if [ -d "$tmpdir" ]; then
if [ "$verbose" ]; then
print_self
printf "clearing character images %s\n" "$tmpdir"
fi
for i in 1 2 3; do
if [ -f "${tmpdir}/s$i.png" ]; then
rm ${verbose} ${tmpdir}/s$i.png
fi
done
fi
}

while getopts "hvV?" option; do
case $option in
h) print_help && exit ;;
v) verbose="-v" ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done

[ ! -d "$tmpdir" ] && mkdir -p "$tmpdir"

case $1 in
clear)
clear_character_images
[ "$verbose" ] && print_self && printf "clearing temp files\n"
rm ${verbose} ${gamef} ${charf} ${scenef}
rmdir ${verbose} ${tmpdir}
exit 0
;;
period-changed)
check_set
case $3 in
[Dd]aytime) period="day" ;;
[Nn]ight) period="night" ;;
[Tt]ransition) period="interval" ;;
esac
clear_character_images
$HOME/bin/back.sh ${verbose} $walldir/${game}_${scene}_$period.???
[ "$verbose" ] && print_self && printf "copying character files\n"
for i in 1 2 3; do
cp ${verbose} "$chardir"/${game}_${char}_${period}_s$i.png \
"$tmpdir"/s$i.png
done
exit 0
;;
*)
print_help && print_self && printf 'invalid argument(s): %s\n' "$*"
exit 1
;;
esac

Return to the top of this page or return to the overview of this repo.

Log background.sh

Date Commit Message Author + -
2021-12-13 Fix a few names that were copy-and-pasted. cev +1 -1
2021-12-13 Invalid argument catch in background.sh, misc cev +10 -4
2021-12-13 Expanded redshift background.sh, misc. changes cev +135 -54
2021-12-08 Initial commit. Dotfiles, scripts, basic Makefile. cev +111  

Return to the top of this page or return to the overview of this repo.