-#!/bin/zsh
+#!/usr/bin/env zsh
+#
+# Copyright © 2008, 2017 Nick Bowler
+#
+# Simple thumbnail generator for use with FVWM. Thumbnails can be generated at
+# any desired size, and are cached for future use. Prints the cached filename
+# to standard output.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+
+# resolve_file [file]
+#
+# If the argument is a symbolic link, print the target of that link.
+# Otherwise, prints the basename of file.
+resolve_file () {
+ test $# -eq 1 || return
+
+ # Ensure filename won't be confused for any kind of find argument...
+ case $1 in
+ /*) :;;
+ *) set x "./$1"; shift
+ esac
+
+ find "$1" -prune \( -type l -printf '%l' -o -printf '%f' \)
+}
+
+if [[ -z "$MUSIC" ]]; then
+ MUSIC=/home/music
+fi
-music=/stuff/music
thumbs="$HOME/.fvwm/.thumbs"
-if [ ! -d "$thumbs" ]; then
+if ! [[ -d "$thumbs" ]]; then
mkdir "$thumbs" || exit 1
fi
printimg=""
ismusic=""
-while [ "${1#--}" != "$1" -a "$1" != "-" ]; do
- if [ "$1" = "--small" ]; then
+while [[ "${1#--}" != "$1" && "$1" != "-" ]]; do
+ if [[ "$1" == "--small" ]]; then
size="56"
- elif [ "$1" = "--size" ]; then
+ elif [[ "$1" == "--size" ]]; then
size="$2"
shift
- elif [ "$1" = "--music" ]; then
+ elif [[ "$1" = "--music" ]]; then
ismusic="yes"
- elif [ "$1" = "--image" ]; then
+ elif [[ "$1" = "--image" ]]; then
printimg="yes"
else
echo "unrecognised option: $1" 1>&2
done
[ "$1" = "-" ] && shift
-if ! expr match "$size" '^\([0-9]*\(x[0-9]\+\)\?\)$' >/dev/null 2>&1; then
+if ! [[ "$size" =~ '^([0-9]*(x[0-9]+)?)$' ]]; then
echo "invalid size specification: $size" 1>&2
exit 1
fi
-if [ -z "$1" ]; then
+if [[ -z "$1" ]]; then
echo "usage: thumbnail.zsh [--small|--size <spec>] [--image] [--music] path" 1>&2
exit 1
fi
-if [ -n "$ismusic" ]; then
- imgpath="$music/`dirname "${1#$music}"`/cover.jpg"
- [ ! -f "$imgpath" ] && imgpath="${imgpath%jpg}png"
+if [[ -n "$ismusic" ]]; then
+ imgpath="$MUSIC/$(dirname "${1#$MUSIC}")/cover.jpg"
+ [[ ! -f "$imgpath" ]] && imgpath="${imgpath%jpg}png"
else
imgpath="$1"
fi
-[ ! -f "$imgpath" ] && exit 0
-
-image="$(readlink -f -- "$imgpath")"
-[ ! -f "$image" ] && exit 0
+[[ ! -f "$imgpath" ]] && exit 0
+image=`resolve_file "$imgpath"`
+case $image in
+/*) :;;
+*) image=`dirname $imgpath`/$image
+esac
+[[ ! -f "$image" ]] && exit 0
-thumb="$thumbs/`echo -n $image | md5sum - | cut -d ' ' -f 1`_$size.png"
-if [ -f "$thumb" ]; then
- mtime_s="`stat -c %Y -- "$image"`"
- mtime_t="`stat -c %Y -- "$thumb"`"
+thumb="$thumbs/$(echo -n $image | md5sum - | cut -d ' ' -f 1)_$size.png"
+if [[ -f "$thumb" ]]; then
+ mtime_s="$(stat -c %Y -- "$image")"
+ mtime_t="$(stat -c %Y -- "$thumb")"
if [ "$mtime_s" -gt "$mtime_t" ]; then
convert -scale "$size" "$image" "$thumb"
fi