~q3cpma/mus

ref: e99afe099fd0b6cf6a4cce58902c9612ecd9fca9 mus/mus_album_pick -rwxr-xr-x 2.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
# Dependencies:
# Portability:  GNU, *BSD
#+-------------+-----+---------+--------+--------------+---------+-------+---------+-------+-----+------+-------+-------+
#| cmd\OS      | GNU | OpenBSD | NetBSD | DragonflyBSD | FreeBSD | MacOS | Illumos | HP-UX | AIX | IRIX | Tru64 | POSIX |
#+-------------+-----+---------+--------+--------------+---------+-------+---------+-------+-----+------+-------+-------+
#| readlink -f | o   | o       | o      | o            | o       |       |         |       |     |      |       |       |
#+-------------+-----+---------+--------+--------------+---------+-------+---------+-------+-----+------+-------+-------+
# o: supported, X: different behaviour
set -eu
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__] [__ALBUM_PATH__...]

**DESCRIPTION**
    If there is no __ALBUM_PATH__ argument or the only argument is '-', read
    stdin as a newline separated argument list.
    Update the album database and output the __ALBUM_PATH__ list as absolute
    paths to stdout.

**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.

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"

albums=$(readargs "$@" | while IFS= read -r line
		 do
			 requirefile -d "$line"
			 readlink -f -- "$line"
		 done)
[ ! "$albums" ] && die "Empty stdin"

set +e
updated_db=$(echop "$albums" | awk '
	FILENAME == ARGV[1]\
	{
		save = $0
		sub("^[0-9]+ ", "")
		sub("^[0-9]+", "&", save)
		db[$0] = save
		next
	}
	db[$0]\
	{
		if(!db[$0])
		{
			print "\"" $0 "\": " "album not found in database" >"/dev/stderr"
			exit(1)
		}
		++db[$0]
	}

	END\
	{
		for (line in db)
			print db[line], line
	}
' "$album_db" - | LC_ALL=C sort -k2)

[ $? -eq 1 ] && exit 1
set -e

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