3 # Copyright © 2008, 2017 Nick Bowler
5 # Simple thumbnail generator for use with FVWM. Thumbnails can be generated at
6 # any desired size, and are cached for future use. Prints the cached filename
9 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
10 # This is free software: you are free to do what the fuck you want to.
11 # There is NO WARRANTY, to the extent permitted by law.
15 # If the argument is a symbolic link, print the target of that link.
16 # Otherwise, prints the basename of file.
18 test $# -eq 1 || return
20 # Ensure filename won't be confused for any kind of find argument...
23 *) set x "./$1"; shift
26 find "$1" -prune \( -type l -printf '%l' -o -printf '%f' \)
29 if [[ -z "$MUSIC" ]]; then
33 thumbs="$HOME/.fvwm/.thumbs"
35 if ! [[ -d "$thumbs" ]]; then
36 mkdir "$thumbs" || exit 1
43 while [[ "${1#--}" != "$1" && "$1" != "-" ]]; do
44 if [[ "$1" == "--small" ]]; then
46 elif [[ "$1" == "--size" ]]; then
49 elif [[ "$1" = "--music" ]]; then
51 elif [[ "$1" = "--image" ]]; then
54 echo "unrecognised option: $1" 1>&2
59 [ "$1" = "-" ] && shift
61 if ! [[ "$size" =~ '^([0-9]*(x[0-9]+)?)$' ]]; then
62 echo "invalid size specification: $size" 1>&2
66 if [[ -z "$1" ]]; then
67 echo "usage: thumbnail.zsh [--small|--size <spec>] [--image] [--music] path" 1>&2
71 if [[ -n "$ismusic" ]]; then
72 imgpath="$MUSIC/$(dirname "${1#$MUSIC}")/cover.jpg"
73 [[ ! -f "$imgpath" ]] && imgpath="${imgpath%jpg}png"
78 [[ ! -f "$imgpath" ]] && exit 0
79 image=`resolve_file "$imgpath"`
82 *) image=`dirname $imgpath`/$image
84 [[ ! -f "$image" ]] && exit 0
86 thumb="$thumbs/$(echo -n $image | md5sum - | cut -d ' ' -f 1)_$size.png"
87 if [[ -f "$thumb" ]]; then
88 mtime_s="$(stat -c %Y -- "$image")"
89 mtime_t="$(stat -c %Y -- "$thumb")"
90 if [ "$mtime_s" -gt "$mtime_t" ]; then
91 convert -scale "$size" "$image" "$thumb"
94 convert -scale "$size" "$image" "$thumb"
98 [ -n "$printimg" ] && echo "$image"