]> git.draconx.ca Git - scripts.git/blob - musicstats.zsh
Import the scripts directory from my website.
[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=0
10 while getopts 's' opt $@; do
11         case $opt in
12         s) sizes=1 ;;
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 alias mfind='find $@ -type f'
25 flac=`mfind -name '*.flac' | wc -l`
26 vorbis=`mfind -name '*.ogg' | wc -l`
27 mp3=`mfind -name '*.mp3' | wc -l`
28 total=$((flac+vorbis+mp3))
29
30 if [[ $total == 0 ]]; then
31         printf '0 tracks.\n'
32         exit
33 fi
34
35 printf '%d tracks: %d (%.1f%%) flac, %d (%.1f%%) vorbis, %d (%.1f%%) mp3.\n' \
36         $total \
37         $flac   $((100.0*flac  /total)) \
38         $vorbis $((100.0*vorbis/total)) \
39         $mp3    $((100.0*mp3   /total))
40
41 if [[ $sizes == 1 ]]; then
42         alias getsize="du -mc --files0-from=- | sed -n '\$s/\ttotal$//p'"
43         float totalsize=`mfind -print0 | getsize`
44         float flacsize=`mfind -name '*.flac' -print0 | getsize`
45         float vorbissize=`mfind -name '*.ogg' -print0 | getsize`
46         float mp3size=`mfind -name '*.mp3' -print0 | getsize`
47
48         if [[ $totalsize -gt 4000 ]]; then
49                 printf 'size: %.1fG total, %.1fG flac, %.1fG vorbis, %.1fG mp3.\n' \
50                         $((totalsize/1024)) \
51                         $((flacsize/1024)) \
52                         $((vorbissize/1024)) \
53                         $((mp3size/1024))
54         else
55                 printf 'size: %.1fM total, %.1fM flac, %.1fM vorbis, %.1fM mp3.\n' \
56                         $((totalsize)) \
57                         $((flacsize)) \
58                         $((vorbissize)) \
59                         $((mp3size))
60         fi
61 fi