[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / mpd-state-wrapper.sh
blob4cda5bf6004e2ec9113b9d25fd41e71299d2a1fb
1 #!/bin/sh
2 ## Wrapper for mpd-state to make it behave like state-utils.
3 ## Copyright (c) 2005 by Michal Nazareicz <mina86@mina86.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ## This is part of Tiny Applications Collection
19 ## -> http://tinyapps.sourceforge.net/
23 # To install copy this file to (eg.) /usr/local/bin and make the
24 # fallowing symlinks: state-save, state-restore, state-sync and
25 # state-amend all pointing to this wrapper.
28 if [ "$1" = "--help" ]; then
29 cat<<EOF
30 usage: state-save [ <state-name> ]
31 state-restore [ <state-name> ]
32 state-sync [ <mpd-host> [ <mpd-port> ] ]
33 state-amend [ <state-name> ]
34 state-save saves MPD state to a file.
35 state-restores resotres saved state to MPD.
36 state-sync tries to sync two MPD servers as close as possible.
37 state-amend adds playlist from specified state to MPD.
38 EOF
39 exit 0;
43 ARG0=${0##*/}
44 SHIFTED=
45 while true; do case "$ARG0" in
46 (state-save|save)
47 mkdir -p ~/.mpd_states && mpd-state >~/.mpd_states/"${1-default}"
48 exit $?
51 (state-restore|restore)
52 if [ -f ~/".mpd_states/${1-default}" ]; then
53 mpd-state -r <~/.mpd_sates/"${1-default}"
54 exit $?
55 else
56 # shellcheck disable=SC2088
57 echo File "~/.mpd_states/${1-default}" does not exist >&2
58 exit 1
62 (state-sync|sync)
63 mpd-state | mpd-state -r "${1-localhost}" "${2-6600}"
64 exit $?
67 (state-amend|amend)
68 if [ -f ~/".mpd_states/${1-default}" ]; then
69 mpd-state -raP <~/.mpd_sates/"${1-default}"
70 exit $?
71 else
72 # shellcheck disable=SC2088
73 echo File "~/.mpd_states/${1-default}" does not exist >&2
74 exit 1
78 (*)
79 if [ -z "$SHIFTED" ] && [ $# -ne 0 ]; then
80 SHIFTED=y
81 ARG0="$1"
82 shift
83 else
84 echo 'This script should be run as one of:'
85 echo ' state-save, state-restore, state-sync or state-amend'
86 echo 'or with one of those strings as firt argument optionaly w/o state- prefix'
87 exit 1
90 esac; done