nirv.git

nctl.sh

espurr
#!/bin/sh
# nirv - music player
# Copyright (C) 2023 ArcNyxx
# see LICENCE file for licensing information

alias kill=/bin/kill
MUSIC="${MUSIC:=$HOME/music}"

ps -C nctl -o 'pid=' | grep -v $$ | while read PID ; do kill $PID ; done
PID=`ps -C nirv -o pid=` ; if [ $? != 0 ] ; then nirv & PID=$! ; fi

case "$1" in
-q) kill $PID ; rm -f /tmp/nctl-list /tmp/nctl-loop /tmp/nirv-song ; exit ;;
-p) kill -RTMIN+1 $PID ;;
-t) kill -RTMIN+2 -q "$2" $PID ;;
-e) kill -RTMIN+2 -q  2147483647 $PID ;;
-r) kill -RTMIN+2 -q -2147483647 $PID ;;

-k)
	shuf /tmp/nctl-list -o /tmp/nctl-list
	if ! [ -e /tmp/nctl-loop ] && [ "`cat /tmp/nctl-list`" ] ; then
		ln -sf "`sed q /tmp/nctl-list`" /tmp/nirv-song
	fi ;;
-c)
	sed -i d /tmp/nctl-list
	if ! [ -e /tmp/nctl-loop ] ; then
		rm /tmp/nirv-song
	fi ;;
-l)
	if [ -e /tmp/nctl-loop ] ; then
		rm /tmp/nctl-loop
		if [ "`cat /tmp/nctl-list`" ] ; then
			ln -sf "`sed q /tmp/nctl-list`" /tmp/nirv-song
		else
			rm /tmp/nirv-song
		fi
	else
		touch /tmp/nctl-loop
		ln -sf "`sed q /tmp/nirv-info`" /tmp/nirv-song
	fi ;;

-f) realpath "$MUSIC"/*/*/*-*"$2"*.flac | sed q >>/tmp/nctl-list ;;
-d) realpath   "$MUSIC"/*/*"$2"*/*.flac         >>/tmp/nctl-list ;;
-s) realpath   "$MUSIC"/*/*"$2"*/*.flac |  shuf >>/tmp/nctl-list ;;
esac

[ "`cat /tmp/nctl-list 2>/dev/null`" ] || exit
! [ -e /tmp/nirv-song ] && ln -sf "`sed q /tmp/nctl-list`" /tmp/nirv-song

while : ; do
	sleep 1
	if [ "`sed q /tmp/nirv-info`" = "`sed q /tmp/nctl-list`" ] ; then
		sed -i 1d /tmp/nctl-list
		if [ -z "`cat /tmp/nctl-list`" ] ; then
			rm /tmp/nirv-song
		else
			ln -sf "`sed q /tmp/nctl-list`" /tmp/nirv-song
		fi
	fi
done