@echo off
@rem -- A script to allow a student to practice a language by playing
@rem -- the sound files and displaying a transcription and a translation
@rem -- for the corresponding sound. The transcription and translation
@rem -- information is provided by a data (text) file.
@rem --
@rem --
@rem -- Batch script tricks taken from www.ss64.com/nt/set.html
@rem -- gplay sound file command line player from http://unxutils.sourceforge.net
@rem -- Requirements:
@rem --   a datafile
@rem --   sound files containing language recordings
@rem --   sed, the stream editor, see http://sed.sourceforge.net
@rem --   gplay, a sound file playing command line program
@rem --   Microsoft Windows "NT" or "XP" command line box. 
@rem -- 

set savefolder=c:\

@rem set seddir=f:\unixutil\usr\local\wbin
set sounddir=f:\euskara\1
set seddir=f:\unixutil\usr\local\wbin
set wgetdir=f:\unixutil\usr\local\wbin
set playdir=f:\unixutil\usr\local\wbin
set datafile=%1
	
if NOT "%datafile%"=="" goto EndifDf

  set datafile=f:\euskara\1\data1.txt

:EndifDf



  if NOT exist %sounddir% set /p result="sound directory %sounddir% does not seem to exist"
  if NOT exist %savefolder% set /p result="save directory %savefolder% does not seem to exist"
  if NOT exist %datafile% set /p result="the datafile %datafile% does not seem to exist"
  if NOT exist %seddir% set /p result="the directory for "sed" %seddir% does not seem to exist"
  @rem if NOT exist %wgetdir% set /p result="the directory for "wget" %wgetdir% does not seem to exist"
  if NOT exist %playdir% set /p result="the directory for the soundplayer %playdir% does not seem to exist"


  cd %sounddir%

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

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

  rem del uuurls.txt
  rem 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
  rem \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 %sounddir% | %seddir%\sed -n "$=" > %savefolder%\xx-count-dir.txt
  set /p ifiles=<%savefolder%\xx-count-dir.txt

  @rem --
  @rem -- but "type xx-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 %sounddir%\*.mp3 %sounddir%\*.wav | %seddir%\sed -n "%rr%p" > %savefolder%\xx-current.txt
  set /p current=<%savefolder%\xx-current.txt
  @rem -- set current


  @echo un programa para ayudarle aprender un lenguaje
  @echo ...
  @echo la idea es basica; escuchas palabras o frases en ingles y luego
  @echo puedes ver como esta escrito la frase o palabra y su traduccion en castellano. 
  @echo ...
  @echo teclas "n" para escuchar la proxima frase
  @echo teclas [intro] o "a" para escuchar una frase otra vez
  @echo teclas "t" para la transcripcion 
  @echo teclas "x" para la traduccion 
  @echo teclas "q" para terminar 
  @echo ...
  @echo pulsar ? para ver todos los comandos disponibles.

:Loop

  @rem -- calculate number of files in the sound dir. This is only
  @rem -- necessary if the files are being downloaded while  the
  @rem -- user is using the program, since the number of files will
  @rem -- gradually increase.
  @rem --
  @rem --
  @rem --
  @rem --  dir /b /a-d . | %seddir%\sed -n $= > %savefolder%\xx-count-dir.txt
  @rem --  set /p ifiles=<%savefolder%\xx-count-dir.txt
  @rem -- set ifiles

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


  @rem ---------------------------------
  if "%command%"==" " set command=n
  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 %sounddir%\*.mp3 %sounddir%\*.wav | %seddir%\sed -n %rr%p > %savefolder%\xx-current.txt
    set /p current=<%savefolder%\xx-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 %current% 1>nul 2>nul

  :EndIfNext

  @rem ---------------------------------
  if NOT "%command%"=="v" goto EndIfVar

     @echo  variables:
     @echo    savefolder=%savefolder%
     @echo    seddir=%seddir%
     @echo    wgetdir=%wgetdir%
     @echo    playdir=%playdir%
     @echo    current=%current%
     @echo    datafile=%datafile%
     @echo.
  :EndIfVar

  @rem ---------------------------------
  if NOT "%command%"=="cls" goto EndIfCls
    cls
  :EndIfCls

  @rem ---------------------------------
  if NOT "%command%"=="ver" goto EndIfVer
    ver
  :EndIfver

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

    %playdir%\gplay %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
    rem type data1.txt | %seddir%\sed -n "/%current%/{N;p;}"
    type %datafile% | %seddir%\sed -n "/%current%/,/-xx/p"
    @rem -- echo off

  :EndIfInfo

  @rem ---------------------------------
  if NOT "%command%"=="t" goto EndIfScri
    @rem -- echo on
    type %datafile% | %seddir%\sed -n "/%current%/,/-xx/{/-eu/s/-eu//p;}"
    @rem -- echo off

  :EndIfScri

  @rem ---------------------------------
  if NOT "%command%"=="x" goto EndIfTran

    @rem -- echo on
    type %datafile% | %seddir%\sed -n "/%current%/,/-xx/{/-en/s/-en//p;}"
    @rem -- echo off

  :EndIfTran

  if "%command%"=="ayuda" set command=?
  if "%command%"=="socorro" set command=?
  if "%command%"=="hola" set command=?
  if "%command%"=="help" set command=?
  if "%command%"=="hello" set command=?
  if "%command%"=="h" set command=?
  if NOT "%command%"=="?" goto EndIfHelp

    echo A simple language practicer...
    echo .
    echo ?: display this help message
    echo a: plays the current word; or press [enter]
    echo v: shows the values of program variables (debugging)
    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 t: shows a transcription of the current phrase
    echo x: shows a translation of the current phrase
    echo d: shows information about the data file
    echo q: quit, exit the program
    echo cls: clear the screen
    echo ver: displays the system version

    goto EndIfHelp

    echo .
    echo ?: mostrar este mensaje de ayuda
    echo a: escuchar la misma palabra o frase otra vez, o pulsar [enter]
    echo n: escuchar la proxima palabra (las palabras o frases estan escuchadas en una orden aleatoria)
    echo t: mostrar como esta escrito en ingles la frase o palabra
    echo x: mostrar la traduccion de la frase o palabra en castellano
    echo q: salir del programa
    echo cls: despejar la pantalla

  :EndIfHelp

  if "%command%"=="d" set command=data
  if NOT "%command%"=="data" goto EndIfData

    echo .
    echo This program uses a data file in order to play the 
    echo sound files and to display the corresponding transcriptions
    echo and translations. The data file currently being used is %datafile%
    echo You can view this data file using any text editor (such as "notepad").
    @echo
    @echo  the data file should be in the format
    @echo
    @echo  .filename 
    @echo  -ee text
    @echo  -ee text
    @echo  -ee text
    @echo  -xx
    @echo. 
    @echo    where the letters "ee" stand for a language code such as
    @echo    "eu" for euskara, "es" for espaƱol, or "en" for english

    echo.
  :EndIfData


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

:Quit