djcev.com // dotfiles / back.sh


Git Repos / dotfiles / bin / back.sh

Last commit to this repo was on 2021-12-13 at 16:05.

#!/bin/sh
#
# Copyright (c) 2015-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.
#
#- back.sh 2015/01/13 cev, last updated 2021/12/07
## Usage: back.sh [-acfhtvV] [-d directory] [-m method] wallpaper...
##
## Set wallpaper (xorg 'root window' image). If no wallpaper is specified,
## select one at random from [-d directory]. If more than one file is
## specified, select one at random.
##
## This script is a frontend to hsetroot (or, alternately, pcmanfm-qt).
## hsetroot can be found at https://github.com/himdel/hsetroot .
##
## -a Set maximized wallpaper, preserving aspect. (default)
## -c Set centered wallpaper.
## -d Directory to select an image at random from.
## -f Set fullscreen wallpaper.
## -h Show help.
## -m Background setter method. Valid methods: hsetroot, pcmanfm.
## -t Set tiled wallpaper.
## -v Be verbose.
## -V Print version info.
##
# Useful notes:
# http://www.linuxforums.org/forum/programming-scripting/ \
# 121380-random-file-folder-sub-folders.html

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

self="${0##*'/'}"
action='aspect'
bgdir="/z/gfx/wallpapers/"
method="hsetroot"

cleanup() {
if [ "$tfile" -a -r "$tfile" ]; then rm "$tfile"; fi
}

die_invalid_method() {
print_self error
printf "%binvalid method %b%s%b\n" "$_norm" "$_error" "$1" "$_clear" >&2
exit 1
}

die_no_dir() {
print_directorynotfound "$1"
exit 1
}

die_no_papers() {
print_self error
printf "%bno papers found%b\n" "$_norm" "$_clear" 1>&2
exit 1
}

die_no_x() {
print_self error
printf "%bDISPLAY%b not set. Is X running?%b\n" \
"$_error" "$_norm" "$_clear" >&2
exit 1
}

randwall() {
# max for rng; exit if zero.
max="$(wc -l $1)"
max=${max%' '*}
[ $max -lt 1 ] && die_no_papers
# 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
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
}

setbg() {
# next section is based on work from an old copy of awsetbg by
# Han Boetes & Julien Danjou, see:
# https://github.com/awesomeWM/awesome/blob/ \
# 2853d58f52cfbc4515fad7f45875d5cd31b1085e/utils/awsetbg
case $method in
hsetroot)
setter="$(which hsetroot)"
aspect="-full"
center="-center"
full="-fill"
tile="-tile"
;;
pcmanfm)
setter="$(which pcmanfm-qt) --set-wallpaper"
aspect=""
center=""
full=""
tile=""
;;
*) die_invalid_method "$method" ;;
esac

if [ "$verbose" ]; then
$setter $(eval echo \$${action}) "$@"
else
$setter $(eval echo \$${action}) "$@" 1>/dev/null 2>&1
fi
}

[ -z "$DISPLAY" ] && die_no_x

while getopts "acd:fhm:tV?" option; do
case $option in
a) action='aspect' ;;
c) action='center' ;;
d) bgdir="$OPTARG" ;;
f) action='full' ;;
h) print_help && exit ;;
m) method="$OPTARG" ;;
t) action='tile' ;;
v) verbose='true' ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done

trap cleanup EXIT

if [ $# -lt 1 ]; then
# no file specified. select a random file from $bgdir.
[ ! -d "$bgdir" ] && die_no_dir "$bgdir"
tfile=$(mktemp ${TMPDIR:-/tmp}/${self}.XXXXXXXX)
find "$bgdir" -type f >>$tfile
find "$bgdir" -type l >>$tfile
wallpaper="$(randwall $tfile)"
rm $tfile
elif [ $# -gt 1 ]; then
# more than one file specified. select one at random.
tfile=$(mktemp ${TMPDIR:-/tmp}/${self}.XXXXXXXX)
for wall in $@; do printf "%s\n" "$wall" >>$tfile; done
wallpaper="$(randwall $tfile)"
rm $tfile
else
# exactly one argument
wallpaper="$@"
fi

setbg "$wallpaper"

Return to top of this page or return to the overview of this repository