djcev.com

//

Git Repos / dotfiles / bin / back.sh

Last update to this file was on 2024-01-02 at 05:12.

Show back.sh

#!/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/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh

self="${0##*'/'}"
action='aspect'
bgdir="$HOME/Pictures/Wallpapers"
method="dconf-mate"

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
dconf-mate)
setter="$(which dconf) write "
setter="$setter /org/mate/desktop/background/picture-filename"
aspect=""
center=""
full=""
tile=""
namepre="'"
namepost="'"
;;
hsetroot)
setter="$(which hsetroot)"
aspect="-full"
center="-center"
full="-fill"
tile="-tile"
namepre=""
namepost=""
;;
pcmanfm)
setter="$(which pcmanfm-qt) --set-wallpaper"
aspect=""
center=""
full=""
tile=""
namepre=""
namepost=""
;;
wmsetbg)
setter="$(which wmsetbg)"
aspect="-b black -a -S"
center="-b black -e"
full="-s -S"
tile="-t"
namepre=""
namepost=""
;;
*) die_invalid_method "$method" ;;
esac

if [ "$verbose" ]; then
print_self
printf "wallpaper name %s\n" "$@"
$setter $(eval echo \$${action}) "$namepre""$@""$namepost"
else
$setter $(eval echo \$${action}) \
"$namepre""$@""$namepost" 1>/dev/null 2>&1
fi
}

[ -z "$DISPLAY" ] && die_no_x

while getopts "acd:fhm:tvV?" 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 the top of this page or return to the overview of this repo.

Log back.sh

Date Commit Message Author + -
2024-01-02 Change from FreeBSD to Linux, other minor changes cev +25 -6
2023-04-15 jgmenu.csv, misc. small changes cev +8 -1
2021-12-13 Expanded redshift background.sh, misc. changes cev +9 -3
2021-12-08 Initial commit. Dotfiles, scripts, basic Makefile. cev +149  

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