Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 7cac55358f
Fetching contributors…

Cannot retrieve contributors at this time

executable file 208 lines (184 sloc) 7.641 kB
#!/bin/bash
#
# backdrop-randomizer
# By graysky <graysky AT archlinux DOT us>
#
# https://github.com/graysky2/backdrop-randomizer
#
# see manpage for usage
#
VERS="2.15"
export BLD="\e[01m" RED="\e[01;31m" GRN="\e[01;32m" YLW="\e[01;33m" NRM="\e[00m"
[[ -z "$XDG_CONFIG_HOME" ]] && XDG_CONFIG_HOME="$HOME/.config" # make sure env var is setup correctly
cfg_file=$XDG_CONFIG_HOME/backdrop-randomizer.conf
check() {
if [[ ! -f "$cfg_file" ]]; then
# create $XDG_CONFIG_HOME/backdrop-randomizer.conf
echo '------------------------------------------------------------'
echo ' No config file found so creating a fresh one.'
echo
echo '#' > "$cfg_file"
echo '# $XDG_CONFIG_HOME/backdrop-randomizer.conf' >> "$cfg_file"
echo '#' >> "$cfg_file"
echo '# For documentation see the backdrop-randomizer manpage.' >> "$cfg_file"
echo '#' >> "$cfg_file"
echo '# Provide the full path to pics to be used.' >> "$cfg_file"
echo 'PIC_PATH=/path/to/pics' >> "$cfg_file"
echo >> "$cfg_file"
echo '# Uncomment and redefine if you want to find pics recursively.' >> "$cfg_file"
echo '#MAXDEPTH=1' >> "$cfg_file"
echo >> "$cfg_file"
echo '# Uncomment and redefine if you want to provide an alternative' >> "$cfg_file"
echo '# location where the database of pics will reside.' >> "$cfg_file"
echo '# You _MUST_ have rw permissions to the dir you define!' >> "$cfg_file"
echo "#BD_DIR=$XDG_CONFIG_HOME" >> "$cfg_file"
echo >> "$cfg_file"
echo '# Uncomment and redefine if you want to provide an alternative' >> "$cfg_file"
echo '# location of the symlink that will be updated by this script.' >> "$cfg_file"
echo '# You _MUST_ have rw permissions to the dir you define!' >> "$cfg_file"
echo "#SLINK_DIR=$XDG_CONFIG_HOME" >> "$cfg_file"
# done
echo ' Define the path to your pics via the PIC_PATH variable in'
echo ' $XDG_CONFIG_HOME/backdrop-randomizer.conf and try again.'
echo '------------------------------------------------------------'
exit 1
else
. "$cfg_file"
fi
# check each var for correctness
[[ -z "$MAXDEPTH" ]] && MAXDEPTH=1
result=$(echo "$MAXDEPTH" | egrep ^[[:digit:]]+$)
if [ "$result" = "" ] ; then
echo 'Bad MAXDEPTH in $XDG_CONFIG_HOME/backdrop-randomizer.conf'
echo
echo 'You have defined a non-integer value.'
exit 1
else
/bin/true
fi
[[ -z "$BD_DIR" ]] && BD_DIR=$XDG_CONFIG_HOME
if [[ ! -w "$BD_DIR" ]]; then
echo "Bad BD_DIR in $XDG_CONFIG_HOME/backdrop-randomizer.conf"
echo
echo 'You have defined a path to which you cannot write.'
exit 1
fi
[[ -z "$SLINK_DIR" ]] && SLINK_DIR=$XDG_CONFIG_HOME
if [[ ! -w "$SLINK_DIR" ]]; then
echo "Bad SLINK_DIR in $XDG_CONFIG_HOME/backdrop-randomizer.conf"
echo
echo 'You have defined a path to which you cannot write.'
exit 1
fi
# use only one find command to both test for pics and to generate pic_database
pic_database="$BD_DIR/backdrop-randomizer.list"
[[ ! -f "$pic_database" ]] && find "${PIC_PATH}" -maxdepth "$MAXDEPTH" -name "*.jpg" -o -name "*.JPG" -o -name ".png" -o -name ".PNG" -o -name ".gif" -o -name ".GIF" > "$pic_database" 2>/dev/null
howmany=$(cat "$pic_database" | wc -l)
if [[ "$howmany" -eq 0 ]]; then
echo "Bad PIC_PATH in $XDG_CONFIG_HOME/backdrop-randomizer.conf"
echo
echo "You have defined a path that contains no pics and/or one that your user cannot access."
echo "Supported formats are: jpg, png, and gif."
# remove the empty file
rm -f "$pic_database"
exit 1
fi
# remove dups in database if they are present
uniques=$(awk ' !x[$0]++' "$pic_database" | wc -l)
if [[ "$howmany" != "$uniques" ]]; then
awk ' !x[$0]++' "$pic_database" > /tmp/.backdrop_nodups
mv /tmp/.backdrop_nodups "$pic_database"
howmany=$(cat "$pic_database" | wc -l)
fi
# do not rotate if screensaver is active or screen is locked
# works for xscreensaver only
if [[ -n $(pidof xscreensaver) ]] && [[ -x /usr/bin/xscreensaver-command ]]; then
[[ -n $(xscreensaver-command -time | grep "screen blanked since" 2>/dev/null) ]] || [[ -n $(xscreensaver-command -time | grep "screen locked since" 2>/dev/null) ]] && exit 1
fi
}
stats() {
echo -e " ${BLD}Pics left before next db refresh: ${NRM}${GRN}$howmany"${NRM}
echo
echo -e " ${BLD}Database: ${NRM}${RED}$pic_database"${NRM}
echo -e " ${BLD}Current: ${NRM}${GRN}$(readlink -f $SLINK_DIR/backdrop-randomizer.jpg)"${NRM}
echo -e " ${BLD}Symlink: ${NRM}${YLW}$SLINK_DIR/backdrop-randomizer.jpg"${NRM}
}
list() {
echo " Individual list of pics:"
cat "$pic_database"
}
cycle() {
find "${PIC_PATH}" -maxdepth "$MAXDEPTH" -name "*.jpg" -o -name "*.JPG" -o -name ".png" -o -name ".PNG" -o -name ".gif" -o -name ".GIF" > "$pic_database" 2>/dev/null
howmany=$(cat "$pic_database" | wc -l)
}
randomize() {
# two conditions:
# 1) 1 line left (one pic left) so use the pic and regenerate the list
# 2) 2 or more lines left so select a random number between 1 and $howmany
if [[ $howmany -eq 1 ]]; then
# condition 1 is true
useit=$(sed -n "1 p" "$pic_database") && filename=$(basename $useit)
find "${PIC_PATH}" -maxdepth "$MAXDEPTH" -name "*.jpg" -o -name "*.JPG" -o -name ".png" -o -name ".PNG" -o -name ".gif" -o -name ".GIF" > "$pic_database"
sed -i "/$filename/d" "$pic_database" # exclude the one we just picked to create the illusion that we NEVER repeat :)
ln -sf "$useit" "$SLINK_DIR"/backdrop-randomizer.jpg
[[ ! -z $(pidof xfdesktop) ]] && DISPLAY=:0.0 /usr/bin/xfdesktop --reload
elif [[ $howmany -ge 2 ]]; then
# condition 2 is true
rndpick=1
while [[ $rndpick -lt 2 ]]; do
rndpick=$((RANDOM%$howmany+1))
done
useit=$(sed -n "$rndpick p" "$pic_database") && filename=$(basename $useit)
sed -i "/$filename/d" "$pic_database" # exclude the one we just picked to create the illusion that we NEVER repeat :)
ln -sf "$useit" "$SLINK_DIR"/backdrop-randomizer.jpg
[[ ! -z $(pidof xfdesktop) ]] && DISPLAY=:0.0 /usr/bin/xfdesktop --reload
fi
}
add() {
if [[ -z "$arg" ]]; then
echo -e "${RED}You must provide a supported image."${NRM}
exit 1
fi
file=$(/usr/bin/readlink -f "$arg")
if [[ -n $(echo "$file"| grep -E '*.jpg|*.JPG|*.gif|*.GIF|*.png|*.PNG') ]]; then
# check for dups
if [[ -n $(grep "$file" "$pic_database") ]]; then
echo -e " ${RED}${file##*/}${NRM}${BLD} ---X not added since already present."${NRM}
exit 1
fi
before=$(cat "$pic_database" | wc -l)
echo "$file" >> "$pic_database"
after=$(cat "$pic_database" | wc -l)
echo -e " ${GRN}${file##*/}${NRM}${BLD} ---> added to database."${NRM}
echo -e " ${BLD}Database: ${NMR}${RED}$pic_database${NRM}${BLD} now contains ${NRM}${YLW}$after${NRM}${BLD} pics."${NRM}
else
echo -e " ${BLD}Not a supported image file: ${NRM}${RED}${file##*/}"${NRM}
echo -e " ${BLD}Currently, only jpeg, gif, and png images are supported."${NRM}
fi
}
case "$1" in
A|a)
check ; arg="$2" ; add ;;
R|r)
check ; randomize ;;
S|s)
check ; stats ;;
L|l)
check ; list ;;
C|c)
check ; cycle ; randomize ;;
*)
check
echo -e "${RED}backdrop-randomizer v$VERS"${NRM}
echo
echo -e "${BLD} $0${NRM} ${GRN}{r|s|l|a|c}${NRM}"
echo
echo -e "${BLD} r) ${GRN}R${NRM}${BLD}andomly select a pic from the database and refresh xfdesktop."
echo -e "${BLD} s) Display database ${GRN}s${NRM}${BLD}tatistics."
echo -e "${BLD} l) ${GRN}L${NRM}${BLD}ist all pics in the database."
echo -e "${BLD} a) ${GRN}A${NRM}${BLD}dd the next token to database."
echo -e "${BLD} c) ${GRN}C${NRM}${BLD}lear the database and start anew."${NRM}
echo
echo -e " Add all new pics to the database: ${YLW}find -L /path/to/pics -type f -mtime -1 | xargs -n1 bdr a"${NRM}
;;
esac
Jump to Line
Something went wrong with that request. Please try again.