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