~q3cpma/mus

ref: e99afe099fd0b6cf6a4cce58902c9612ecd9fca9 mus/mus_album_db_update -rwxr-xr-x 1.5 KiB
e99afe09q3cpma Finish build.sh and README, move TODO to root 1 year, 1 month 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
69
70
71
72
73
74
75
76
#!/bin/sh
# Dependencies:
# 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 db updater

**SYNOPSIS**
    $(basename -- "$0") [**-h**] [**-d** __FILE__] [__MUSIC_DIR__...]

**DESCRIPTION**
    Update the album database to remove albums not found in the __MUSIC_DIR__s
    and add the ones not already in the database (with a play count of 0).
    If no __MUSIC_DIR__ is given, \$__MUS_MUSIC_DIR__ is used.

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

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

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

    __MUS_MUSIC_DIR__
        Music directory. Defaults to \$HOME/Music.

EOF
	exit $1
}

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

while getopts "d:h" OPT
do
	case "$OPT" in
		d)
			album_db=$OPTARG
			;;
		h)
			usage 0
			;;
		\?)
			usage 1
			;;
	esac
done
shift $((OPTIND - 1))

requirefile -f "$album_db"
[ $# -eq 0 ] && set -- "${MUS_MUSIC_DIR:-$HOME/Music}"

updated_db=$("$rpath_bin"/mus_album_find "$@" | awk '
    FILENAME == ARGV[1]\
	{
		full = $0
		sub("^[0-9]+ ", "")
		db[$0] = full
		next
	}
	db[$0] {print db[$0]; next}
	{print 0, $0}' "$album_db" - | LC_ALL=C sort -k2)

echop "$updated_db" | flock -- "$album_db" sh -c 'cat >"$1"' dummy "$album_db"