/* Mod-Player interface for Directory Opus 5.
 By Leo 'Nudel' Davidson for Gods'Gift Utilities
 email: leo.davidson@keble.oxford.ac.uk  www: http://info.ox.ac.uk/~kebl0364/

$VER: PlayMod_DT.dopus5 1.3 (25.12.95)

   This version is for use with DeliTracker by Peter Kunath & Frank Riffel
   (Delirium Softdesign), but should be easy to adapt to any other player
   with an ARexx port, such as MultiPlayer, EagleTracker, DMP, etc.
   (In fact, a MultiPlayer version should come with this script!).

   If you include the path of a module on the command line, using {f}, only
   this mod will be played. If you omit the {f}, the program will play each
   selected file in the first SOURCE lister, giving you a requester to go to
   the next file or stop playing.

   If the player's ARexx port is not found, the program will be run for
   you (you should edit the path below). This means the script may fail if
   you disable the player's ARexx port, or give an incorrect command line
   to it.
   If the player's ARexx port takes over a minute to appear, an error
   requester will appear on the DOpus screen and the script will quit.
   REMEMBER to add the "RUN" command to the player command line if it does
   not automatically detach from the shell (DeliTracker doesn't)!

   If the player was running to start with, or the user selects
   "Leave Playing", the module will continue playing when the script finishes.
   Otherwise, the player will be quit.

Call as:
------------------------------------------------------------------------------
ARexx	DOpus5:ARexx/PlayMod_DT.dopus5 {Qp} [{f}]
------------------------------------------------------------------------------
Turn off all switches.
"[]" means this part is optional.

   v1.00 -> v1.01 - Now uses stem variables to get the lister handle.
                    Some minor tidying up.
                    Now checks to make sure the DOPUS.x port exists.
    v1.01 -> v1.2 - Now uses Show() instead of ShowList(). (Thanks Stoebi)
                    Style Guide compliant version numbering and $VER string.
     v1.2 -> v1.3 - Changed the way it loads the mod-player into memory
                    as it was causing problems.
                  - I think there were some problems with filenames containing
                    spaces but they're no longer here as far as I can tell.
                    (Maybe I fixed them and forgot that I'd done it...)

*/
/*- Path to DeliTracker command --------------------------------------------*/
DeliTracker = "RUN >NIL: <NIL: DH0:Tools/Music/DeliTracker2"
/*--------------------------------------------------------------------------*/
options results
options failat 99
signal on syntax;signal on ioerr		/* Error trapping */
parse arg DOpusPort FilePath
DOpusPort = Strip(DOpusPort,"B",'" ')
FilePath = Strip(FilePath,"B",'" ')

If DOpusPort="" THEN Do
	Say "Not correctly called from Directory Opus 5!"
	Say "Load this ARexx script into an editor for more info."
	EXIT
	END
If ~Show("P",DOpusPort) Then Do
	Say DOpusPort "is not a valid port."
	EXIT
	End
Address value DOpusPort

Quit_After = "NO"
If ~Show("P","DELITRACKER") Then Do
	Address Command DeliTracker
	Address Command "WaitForPort DELITRACKER"
	Quit_After = "YES"
	If ~Show("P","DELITRACKER") then do
		dopus request '"Error loading DeliTracker!'|| '0a'x ||'Check its command-path in the ARexx script itself." OK'
		EXIT
		END
	END

/* If file-path was specified on command line, just play that mod, else
   go through all the selected ones. */

If FilePath = "" Then Do

	lister query source stem source_handle.
	IF source_handle.count = 0 | source_handle.count = "SOURCE_HANDLE.COUNT" Then Do
		dopus request '"You must have a SOURCE lister!" OK'
		EXIT
		End
	lister set source_handle.0 busy 1

	lister query source_handle.0 numselentries	/* Get info about selected */
	Lister_NumSelEnt = RESULT			/* entries & path to them */
	lister query source_handle.0 path
	Lister_Path = Strip(RESULT,"B",'"')

	Do i=1 to Lister_NumSelEnt
		lister query source_handle.0 firstsel
		Temp_Name = Strip(RESULT,"B",'"')
		lister select source_handle.0 Temp_Name 0
		Temp_Path = Lister_Path||Temp_Name
		Address DELITRACKER PlayMod Temp_Path

		If Lister_NumSelEnt - i > 0 Then Do
			If Quit_After = "YES" Then
				dopus request '"Playing module ' i ' of ' Lister_NumSelEnt ':'||X2C(0A)||Temp_Name||'" Next|Leave Playing|Stop'
			Else
				dopus request '"Playing module ' i ' of ' Lister_NumSelEnt ':'||X2C(0A)||Temp_Name||'" Next|Leave Playing'
			IF RC = "0" THEN BreakIt = "YES"
			IF RC = "2" THEN Do
				BreakIt = "YES"
				Quit_After = "NO"
				END
			End

		Else Do
			If Quit_After = "YES" Then
				dopus request '"Playing module ' i ' of ' Lister_NumSelEnt ':'||X2C(0A)||Temp_Name||'" Leave Playing|Stop'
			Else
				dopus request '"Playing module ' i ' of ' Lister_NumSelEnt ':'||X2C(0A)||Temp_Name||'" Leave Playing'
			IF RC = "1" Then Quit_After = "NO"
			End

		IF BreakIt = "YES" THEN BREAK
		END
	End

ELSE Do
	Address DELITRACKER PlayMod FilePath
	If Quit_After = "YES" Then
		dopus request '"Playing module:'||X2C(0A)||FilePath'" Leave Playing|Stop'
	Else
		dopus request '"Playing module:'||X2C(0A)||FilePath'" Leave Playing'
	IF RC ~= "0" THEN Quit_After = "NO"
	End

/*-- Restore the Lister for normal use --------------------------------------*/
syntax:;ioerr:				/* In case of error, jump here */
END_PART_2:
If FilePath = "" Then Do
	lister refresh source_handle.0
	lister set source_handle.0 busy 0
	END
END_PART:
If Quit_After = "YES" Then Address DELITRACKER QUIT

EXIT
