]> git.draconx.ca Git - scripts.git/blob - musicstats.zsh
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / musicstats.zsh
1 #!/usr/bin/env zsh
2 #
3 # Copyright © 2009 Nick Bowler
4 #
5 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 # This is free software: you are free to do what the fuck you want to.
7 # There is NO WARRANTY, to the extent permitted by law.
8
9 sizes=false
10 while getopts 's' opt $@; do
11         case $opt in
12         s) sizes=true ;;
13         \?) exit 1 ;;
14         esac
15 done
16
17 shift $((OPTIND-1))
18
19 if [[ $# == 0 ]]; then
20         printf 'usage: %s [-s] directory [directory ...]\n' $0
21         exit 1
22 fi
23
24 # Normalize target names.
25 targets=()
26 for arg in $@; do
27         case $arg in
28         /*) targets+=$arg   ;;
29         *)  targets+=./$arg ;;
30         esac
31 done
32
33 # Simple wrapper around find to search for ordinary files in the target
34 # directories.
35 mfind() {
36         find $targets -type f $@
37 }
38
39 # Simple ngettext wrapper that should work even if ngettext is not installed.
40 ngettext() {
41         command ngettext $@ 2>/dev/null && return 0;
42
43         while [[ $# -gt 3 ]]; do shift; done
44         case $3 in
45         1) printf '%s\n' "$1" ;;
46         *) printf '%s\n' "$2" ;;
47         esac
48 }
49
50 # Get the total size (in mebibytes) of a list of files specified on standard
51 # input.  The input is expected to be in the format of find -print0.
52 getsize() {
53         du -mc --files0-from=- | sed -n '$s/\ttotal$//p'
54         (exit ${${pipestatus%0}[1]})
55 }
56
57 formats=(flac flac ogg vorbis opus opus mp3 mp3)
58 total=0
59 for ext name in $formats; do
60         typeset $ext=`mfind -name \*.$ext | wc -l`
61         total=$((total + $ext))
62 done
63
64 if [[ $total == 0 ]]; then
65         printf '0 tracks.\n'
66         exit
67 fi
68
69 printf '%d %s in %d %s' "$total" `ngettext track tracks $total` \
70                         "$#" `ngettext directory directories $#`
71 if $sizes; then
72         printf ' '
73
74         float totalsize=`mfind -print0 | getsize`
75         if [[ $totalsize -gt 4000 ]]; then
76                 printf '(%.1fGiB total disk usage)' $((totalsize/1024))
77         else
78                 printf '(%.1fMiB total disk usage)' $((totalsize))
79         fi
80 fi
81 printf ':\n'
82
83 for ext name in $formats; do
84         printf '  %*d %5.1f%% %s' $#total $(($ext)) \
85                                   $((100.0*$ext/total)) $name
86
87         if $sizes; then
88                 printf '%*s' $((8-$#name))
89
90                 float size=`mfind -name \*.$ext -print0 | getsize`
91                 if [[ $totalsize -gt 4000 ]]; then
92                         integer t=$((totalsize/1024))
93                         printf '(%*.1fGiB)' $(($#t+2)) $((size/1024))
94                 else
95                         integer t=$((totalsize))
96                         printf '(%*.1fMiB)' $(($#t+2)) $((size))
97                 fi
98         fi
99
100         printf '\n'
101 done