# Known bugs:
# - File or directory names like <test'test"test> crash the script.
# - The pytgon scripts are slow => implement fifo based communication.
UseGettext {$FVWM_USERDIR/locale;fvwm-crystal-script:$FVWM_SYSTEMDIR/locale;fvwm-crstal-script:+}
WindowLocaleTitle {Add to playlist}
WindowSize 600 700
#WindowPosition 470 400
Colorset	1
#Font	"xft:Verdana:size=16"
Font	"xft:$[panel_font]:pixelsize:$[panel_font_size:style:$[panel_font_style]"

# currdir=''
#  1: if item == currdir; then
#	ChangeTitle 1
#     else if item == ..; then
#	currdir = previousdir
#	ChangeTitle 3
#	else	currdir = item
#		ChangeTitle 3
#  3: clic 2 -> add song
#  4: Display current folder
#  5: Display { Songs }
#  6: clic 1 -> Quit
#  7: Display { Dir: }
#  8: clic 1 -> currdir = previousdir; ChangeTitle 1+3
#  9: clic 1 -> add currdir
# 10 and 11 are not necessary
# 10: Display { Song: }
# 11: clic 1 -> add song

Init
 Begin
  #load the directories and files in music_root
  ChangeTitle 1 (GetOutput { $FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.py '' | tr '\n' '|' } 1 -1 )
  ChangeTitle 3 (GetOutput { $FVWM_SYSTEMDIR/scripts/FvwmMPD/getfile.py '' | tr '\n' '|' } 1 -1 )
  # init variables
  Set $currdir=''
  Set $newdir='12+3-4.5_6' # absurd value
  Set $newsong='12+3-4.5_6'
End

Widget 1
Property
 Flags NoReliefString
 Position 0 40
 Size 300 620
 Type List
 Title { }
Main
 Case message of
  SingleClic :
  Begin
    #figure out which directory was selected
    # To quote $var with "" crash the script.
    # if a non existing dir is clicked, we do nothing
    Set $clicked=(GetValue 1)
    If $clicked > 0 Then
    Begin
        Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.sh '} $currdir {' } (GetValue 1)
	Set $item1=(GetOutput $cmd 1 -1)
	#Do {Echo item1 = } $item1
	# if 2nd clic on same dir
	If $item1 == $newdir Then
	Begin
	    #Do {Echo clic 2 item1 } $item1 {= currdir } $currdir
	    #list it's contents
	    Set $currdir = $item1
	    Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.py '} $currdir {' | tr '\n' '|'}
	    ChangeTitle 1 (GetOutput $cmd 1 -1)
	End
	Else
	Begin
	    Set $newdir = $item1
	    Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getfile.py '} $newdir {' | tr '\n' '|'}
	    ChangeTitle 3 (GetOutput $cmd 1 -1)
	    ChangeTitle 4 $newdir
	End
    End
  End
End

Widget 3
Property
 Flags NoReliefString
 Position 300 40
 Size 300 620
 Type List
 Title { }
Main
 Case message of
  SingleClic :
  Begin
	#figure out which song was selected
	If (GetValue 3) > 0 Then
	Begin
	    Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getfile.py '} $newdir {' | head -n} (GetValue 3) { |tail -n1}
	    Set $item3=(GetOutput $cmd 1 -1)
	    # if 2nd clic on same item
	    If $item3 == $newsong Then
	    Begin
		#add the song to the playlist
		Set $cmd={mpc add '} $newsong {' >/dev/null}
		Do {Exec } $cmd
	    End
	    Else
	    Begin
		Set $newsong = $item3
	    End
	End
  End
End

Widget 4
Property
 Flags NoReliefString
 Position 0 0
 Size 515 40
 Type ItemDraw
 LocaleTitle { Folders }
Main
 Case message of
  SingleClic :
  Begin
  End
End

Widget 5
Property
 Flags NoReliefString
 Position 515 0
 Size 85 40
 Type ItemDraw
 LocaleTitle { Songs }
Main
 Case message of
  SingleClic :
  Begin
  End
End

Widget 6
Property
 Flags NoReliefString
 Position 0 660
 Size 100 40
 Type PushButton
 LocaleTitle { Close }
Main
 Case message of
  SingleClic :
  Begin
	Quit
  End
End

Widget 7
Property
 Flags NoReliefString
 Position 100 660
 Size 95 40
 Type ItemDraw
 LocaleTitle { Dir: }
Main
 Case message of
  SingleClic :
  Begin
  End
End

Widget 8
Property
 Flags NoReliefString
 Position 195 660
 Size 85 40
 Type PushButton
 LocaleTitle { Up }
Main
 Case message of
  SingleClic :
  Begin
	#return to the previous directory
	Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.sh '} $newdir {' ..}
	Set $currdir=(GetOutput $cmd 1 -1)
	Set $newdir=$currdir

	#change to that directory and list it's contents
	Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.py '} $newdir {' | tr '\n' '|'}
	ChangeTitle 1 (GetOutput $cmd 1 -1)
	Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getfile.py '} $newdir {' | tr '\n' '|'}
	ChangeTitle 3 (GetOutput $cmd 1 -1)
	ChangeTitle 4 $currdir
  End
End

Widget 9
Property
 Flags NoReliefString
 Position 280 660
 Size 100 40
 Type PushButton
 LocaleTitle { Add dir }
Main
 Case message of
  SingleClic :
  Begin
	#figure out which directory was selected
	Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getdir.sh '} $currdir {' } (GetValue 1)
	Set $seldir={'} (GetOutput $cmd 1 -1) {'}
	If $seldir == '' Then
	Begin
	    Set $seldir={'} $currdir {'}
	End
	Set $cmd={mpc add } $seldir { >/dev/null}
	Do {Exec } $cmd
  End
End

Widget 10
Property
 Flags NoReliefString
 Position 430 660
 Size 85 40
 Type ItemDraw
 LocaleTitle { Song: }
Main
 Case message of
  SingleClic :
  Begin
  End
End

Widget 11
Property
 Flags NoReliefString
 Position 515 660
 Size 85 40
 Type PushButton
 LocaleTitle { Add }
Main
 Case message of
  SingleClic :
  Begin
	#figure out which song was selected
	If (GetValue 3) > 0 Then
	Begin
	    Set $cmd={$FVWM_SYSTEMDIR/scripts/FvwmMPD/getfile.py '} $newdir {' | head -n} (GetValue 3) { |tail -n1}
	    Set $currfile=(GetOutput $cmd 1 -1)

	    #add the song to the playlist
	    Set $cmd={mpc add '} $currfile {' >/dev/null}
	    Do {Exec } $cmd
	End
  End
End
