#!/usr/local/plan9/bin/rc

jukewelcome=' 
Welcome to Juke.

This window, Juke/, is the playlist window.
It contains a list of songs to be played, along
with any other text.  Songs are lines that
begin with song/nnn, where nnn is a number.
They might have other text (like the name of
the song) as well.

If you button 2 click on a line in this window,
juke starts playing the first song it finds on
that line or following.  The currently playing
song is marked with a >.  When the song ends,
juke picks up the > and scans forward for the
next song, and then starts that song.  If juke finds
a line beginning "repeat" instead, then it moves
back to the top of the file.  Otherwise, if juke
reaches the end of the window, it stops playing.
Other recognized lines are "alarm hh:mm", which
pauses until the given time and can be used to 
implement an alarm clock, and "volume [+-]nn",
which changes the volume.

If the playlist window contains a line beginning
"shuffle", the playing algorithm changes: when
one song ends, juke chooses the next song at
random from all the songs in the window.  Juke
never stops playing in shuffle mode.

This window contents *are* the play list.
If you move songs in this window, that changes
the play order.  If you move the >, then juke
will think that was the song being played when
the current song ends.

Juke recognizes some builtin commands, which are
displayed in the tag of this window.  V- and V+
change the volume.  Not displayed, but still recognized,
is the "Dev" builtin, which sets the sound output 
device.  The argument can be a device like /dev/dsp
(the default on Linux) or the name of a machine
accessible via ssh.  If the latter, the sound is played
on *that* machine''s default sound card.

In addition to song tags (song/nnn), there
are album, artist, composer, and genre tags.
Button 3 clicking on any of these opens a new
window named, e.g., Juke/song/nnn, displaying
information about the song, the contents of
the albums, works by the artist or composer,
or songs in the genre.  In the results window,
button 2 clicking on a line appends that line
to the end of the playlist.  Button 3 clicks
behave as just described.

If you button 3 click on text that is not a tag,
then the new window will be named Juke/search,
and it will be filled with a list of tags whose
titles match the clicked text.  The search
windows behave just like the song windows.

Button 3 clicking on the single words song,
album, artist, composer, and genre shows lists
of the given category.

This is an intimidating amount of explanation.
Just try it.  

Enjoy.

'

. 9.rc
if(~ $debugjuke yes) echo >[1=2] jukeput $*

file=$1
*=`{echo $file | tr / ' '}
if(~ $#* 0){
	file=playlist
	*=(playlist)
}
if not if(~ $#* 1){
	file=$file/index
	*=($1 index)
}

if(~ $1 album artist composer genre song && ~ $2 index){
	echo $1 $2
	echo
	cat $jukedb/$file | sort -d +1
	exit 0
}

switch($1){
case album artist compose genre
	# indexes are easy
	# Q: why does this sort albums with track numbers properly?
	# A: because the track number is printed as %2d, so sort sees
	#	" 1" vs " 2" vs "10" and does the right thing.
	echo -n $file ''
	sed 2q $jukedb/$file
	sed 1,2d $jukedb/$file | sort -d +1

case song
	# songs are stored in a tree to avoid large directories
	n=`{echo $2 | sed ' 
		s;^([0-9]+)([0-9][0-9][0-9])$;\1/\2;;
		s;/0*;/;;
		s;^([0-9]+)$;0/\1;;'
	}
	echo -n $file ''
	cat $jukedb/song/$n

case list playlist
	if(~ $1 playlist && ! test -f $jukedb/$file){
		echo $jukewelcome
		exit 0
	}
	if(test -d $jukedb/$file){
		for(i in $jukedb/$file/*){
			if(test -d $i)
				echo $i'/	directory'
			if not{
				echo -n $i'	'
				sed 1q $i >[2]/dev/null || echo
			}
		}
	}
	if not
		cat $jukedb/$file
}
exit 0
