~q3cpma/mus

ref: e99afe099fd0b6cf6a4cce58902c9612ecd9fca9 mus/mus_album_rand -rwxr-xr-x 1.4 KiB
e99afe09q3cpma Finish build.sh and README, move TODO to root 1 year, 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
# Dependencies: fair_shuf
# Portability:  POSIX
set -eu
rpath_bin=$(dirname -- "$0")
rpath_share=$(dirname -- "$0")
. "$rpath_share"/mus_util.sh


usage()
{
	cat <<EOF | text_format
**NAME**
    $(basename -- "$0") - Album picker

**SYNOPSIS**
    $(basename -- "$0") [**-h**] [**-d** __FILE__] [**-o** __FAIR_SHUF_OPTS__] [__COUNT__]

**DESCRIPTION**
    Randomly pick and output to stdout __COUNT__ albums (if no __COUNT__ is
    provided, pick only one) but skew the probabilities so less played
    albums have more chances of being picked (see fair_shuf).

**OPTIONS**
    **-h**
        Print this notice and exit.

    **-d** __FILE__
        Set the album database path. Override the \$__MUS_ALBUM_DB__ environment
        variable.

    **-o** __FAIR_SHUF_OPTS__
        Additional options passed to **fair_shuf**.

**ENVIRONMENT**
    __MUS_ALBUM_DB__
        Album database path. Defaults to
        \${XDG_CONFIG_HOME:-\$HOME/.config}/mus/album_db.

EOF
	exit $1
}

album_db=${MUS_ALBUM_DB:-${XDG_CONFIG_HOME:-$HOME/.config}/mus/album_db}

fs_opts=
while getopts "d:ho:" OPT
do
	case "$OPT" in
		d)
			album_db=$OPTARG
			;;
		h)
			usage 0
			;;
		o)
			fs_opts=$OPTARG
			;;
		\?)
			usage 1
			;;
	esac
done
shift $((OPTIND - 1))
[ $# -gt 1 ] && usage 1

requirefile -f "$album_db"
flock -- "$album_db" "$rpath_bin"/fair_shuf -n "${1:-1}" $fs_opts "$album_db"