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