@echo off
@rem -- tricks from www.ss64.com/nt/set.html
@rem -- gplay from http://unxutils.sourceforge.net
@rem -- downloads sounds files and plays them in a
@rem -- random order. requires 
@rem -- data.txt, wget, sed, gplay or some other command
@rem -- line sound playing application. this runs in an nt batch
@rem -- environment. try start cmd to start a command shell in a
@rem -- new window

rem set seddir=\unixutils\usr\local\wbin
set seddir=\unixut~1\usr\local\wbin
set wgetdir=\unixut~1\usr\local\wbin
set playdir=\unixut~1\usr\local\wbin
set datafile=%1

if NOT "%datafile%"=="" goto EndifDf
  @echo usage: try datafile
  @echo  the data file should be in the format
  @echo  url [newline] transcription [newline] translation ...
  goto quit
:EndifDf

del wget-log
  
  if NOT exist .\sound mkdir sound

  @rem -- download soundfiles found in data file, if
  @rem -- not already present

  del uuurls.txt
  type %datafile% | %seddir%\sed -n /http:/p > uuurls.txt
  @rem --
  @rem -- b is background, w is wait, N is dont redownload,
  @rem -- nd is dont create directories
  \wget\wget -b -w5 -N -nd -Psound -iuuurls.txt



  @rem -- calculate number of files in the sound dir
  @rem --
  set ifiles=0
  dir /b /a-d sound | %seddir%\sed -n $= > count-dir.txt
  set /p ifiles=<count-dir.txt

  @rem --
  @rem -- but "type count-dir.txt | set /p files=" doesnt work

  set /A rr=%random% %% %ifiles%
  set /A rr=%rr% + 1

  @rem -- select a random file from the sound directory and
  @rem -- store in a variable
  dir /b /a-d sound | %seddir%\sed -n %rr%p > current.txt
  set /p current=<current.txt
  @rem -- set current
  @echo simple language program (type ? for help)

:Loop

  @rem -- calculate number of files in the sound dir
  @rem --
  dir /b /a-d sound | %seddir%\sed -n $= > count-dir.txt
  set /p ifiles=<count-dir.txt
  @rem -- set ifiles

  @rem -- in the freedos shell quote characters are displayed
  @rem -- with the set command
  set /p command=">"


  @rem ---------------------------------
  if NOT "%command%"=="n" goto EndIfNext

    set /A rr=%random% %% %ifiles%
    set /A rr=%rr% + 1

    @rem -- select a random file from the sound directory and
    @rem -- store in a variable
    dir /b /a-d sound | %seddir%\sed -n %rr%p > current.txt
    set /p current=<current.txt
    @rem -- set current

    @rem -- 1>nul 2>nul send both stdout and stderr to the
    @rem -- null device which is a way of suppressing the
    @rem -- text output to the screen. I want to do this
    @rem -- because the gplay program shows some distracting
    @rem -- messages.
    %playdir%\gplay sound\%current% 1>nul 2>nul

  :EndIfNext


  @rem ---------------------------------
  if NOT "%command%"=="p" goto EndIfPlay

    %playdir%\gplay sound\%current% 1>nul 2>nul

  :EndIfPlay

  @rem ---------------------------------
  if NOT "%command%"=="a" goto EndIfAgain

    %playdir%\gplay sound\%current% 1>nul 2>nul

  :EndIfAgain

  @rem ---------------------------------
  if NOT "%command%"=="s" goto EndIfShow

    echo current (sound) file is %current%

  :EndIfShow

  @rem ---------------------------------
  if NOT "%command%"=="f" goto EndIfFiles
    echo number of files is %ifiles%
  :EndIfFiles

  @rem ---------------------------------
  if NOT "%command%"=="i" goto EndIfInfo

    @rem -- echo on
    type data.txt | %seddir%\sed -n /.*%current%.*/{N;p;}
    @rem -- echo off

  :EndIfInfo

  if "%command%"=="h" set command=?
  if NOT "%command%"=="?" goto EndIfHelp

    echo A simple language practicer...
    echo .
    echo p: plays the current word
    echo a: plays the current word again
    echo s: shows the current (sound) file
    echo f: shows the number of (sound) files available
    echo n: plays the next word
    echo i: shows info about the current word
    echo q: quit
    echo ?: display this help message

  :EndIfHelp


if NOT "%command%"=="q" goto Loop

:Quit