]> git.draconx.ca Git - fvwmconf.git/blob - scripts/thumbnail.zsh
Update thumbnail.zsh to use zsh conventions.
[fvwmconf.git] / scripts / thumbnail.zsh
1 #!/bin/zsh
2
3 music=/stuff/music
4 thumbs="$HOME/.fvwm/.thumbs"
5
6 if ! [[ -d "$thumbs" ]]; then
7         mkdir "$thumbs" || exit 1
8 fi
9
10 size="x128"
11 printimg=""
12 ismusic=""
13
14 while [[ "${1#--}" != "$1" && "$1" != "-" ]]; do
15         if   [[ "$1" == "--small" ]]; then
16                 size="56"
17         elif [[ "$1" == "--size"  ]]; then
18                 size="$2"
19                 shift
20         elif [[ "$1" = "--music"  ]]; then
21                 ismusic="yes"
22         elif [[ "$1" = "--image"  ]]; then
23                 printimg="yes"
24         else
25                 echo "unrecognised option: $1" 1>&2
26                 exit 1
27         fi
28         shift
29 done
30 [ "$1" = "-" ] && shift
31
32 if ! [[ "$size" =~ '^([0-9]*(x[0-9]+)?)$' ]]; then
33         echo "invalid size specification: $size" 1>&2
34         exit 1
35 fi
36
37 if [[ -z "$1" ]]; then
38         echo "usage: thumbnail.zsh [--small|--size <spec>] [--image] [--music] path" 1>&2
39         exit 1
40 fi
41
42 if [[ -n "$ismusic" ]]; then
43         imgpath="$music/$(dirname "${1#$music}")/cover.jpg"
44         [[ ! -f "$imgpath" ]] && imgpath="${imgpath%jpg}png"
45 else
46         imgpath="$1"
47 fi
48
49 [[ ! -f "$imgpath" ]] && exit 0
50
51 image="$(readlink -f -- "$imgpath")"
52 [[ ! -f "$image" ]] && exit 0
53
54 thumb="$thumbs/$(echo -n $image | md5sum - | cut -d ' ' -f 1)_$size.png"
55 if [[ -f "$thumb" ]]; then
56         mtime_s="$(stat -c %Y -- "$image")"
57         mtime_t="$(stat -c %Y -- "$thumb")"
58         if [ "$mtime_s" -gt "$mtime_t" ]; then
59                 convert -scale "$size" "$image" "$thumb"
60         fi
61 else
62         convert -scale "$size" "$image" "$thumb"
63 fi
64
65 echo "$thumb"
66 [ -n "$printimg" ] && echo "$image"
67 exit 0