format PE console on 'microwav.bin'
entry start
include '..\..\common\fasmw\win32a.inc' ; x for invoke createfilea...

section '.code' code readable executable
start:
  push ebx
  call dword [kGetCommandLineW]
  or eax, eax
  jz EndItI

  mov cx, [eax]
  cmp cx, 34
  jne NoQuote
  inc eax
  inc eax
  call FindIt
;  dec eax
;  dec eax

NoQuote:
  mov cx, 32
  call FindIt
  mov ebx, eax

  mov cx, [eax]
  cmp cx, 34
  jne NoQuote2
  inc eax
  inc eax
  mov ebx, eax
  call FindIt
  dec eax
  dec eax
  mov [eax], word 0
NoQuote2:
;  mov cx, 32
;  call FindIt






  call [kGetVersion]
  and eax, $800000ff
  cmp eax, $80000004
  jnz NTMode

  push ebx
  call [klstrlenW]
  or eax, eax
  jz EndItIA
  cmp eax, $200
  ja EndItZ

  sub esp, $104
  mov ecx, esp
  mov [esp + eax], byte 0

  push dword 0
  push dword 0
  push dword $102
  push ecx
  push eax
  push ebx
  push dword 0
  push dword 0
  call [kWideCharToMultiByte]
  or eax, eax
  jz EndItA

  push StringEnd1 - StringStart1
  push StringStart1
  call CWriteStrA

  mov ecx, esp
  push dword $20000
  push dword 0
  push ecx
  call [mPlaySoundA]
  add esp, $104
  jmp PostPlayOut

EndItA:
  add esp, $104
  jmp EndIt

NTMode:
  push StringEnd1 - StringStart1
  push StringStart1
  call CWriteStrA

  push dword $20000
  push dword 0
  push ebx
  call [mPlaySoundW]

PostPlayOut:
  or eax, eax
  jz PostPlayOutFail

  push StringEnd2 - StringStart2
  push StringStart2
  call CWriteStrA
  mov eax, 1
  jmp EndItEx

EndItIA:
  add esp, $104
EndItI:
  push StringEnd5 - StringStart5
  push StringStart5
jmp EndItIW

PostPlayOutFail:
  push StringEnd3 - StringStart3
  push StringStart3
  call CWriteStrA
  jmp EndItExZ
;  xor eax, eax
;  mov eax, $11

EndItZ:
EndIt:
  push StringEnd4 - StringStart4
  push StringStart4
EndItIW:
  call CWriteStrA
EndItExZ:
  xor eax, eax
EndItEx:
  push eax
  call dword [kExitProcess]
  pop ebx
  ret $c

proc FindIt
FindLoop:
  mov dx, [eax]
  or dx, dx
  jz FailFind
  inc eax
  inc eax
  cmp dx, cx
  jne FindLoop
  ret

FailFind:
  add esp, 4
  jmp EndIt
endp

proc CWriteStrA ;start, len
  push dword -11
  call dword [kGetStdHandle]
  or eax, eax
  jz NoConsole

  xor ecx, ecx
  push ecx
  mov edx, esp

  push ecx ;0
  push edx ;ptr on esp for out
  push dword [esp + $14]
  push dword [esp + $14]
  push eax
  call dword [kWriteConsoleA]
  pop eax
NoConsole:
  ret $8
endp

section '.idata' import data readable writeable

  library kernel, 'KERNEL32.DLL',\
    winmm,'WINMM.DLL'

  import kernel,\
	 kExitProcess,'ExitProcess',\
	 kGetCommandLineW,'GetCommandLineW',\
	 kGetStdHandle,'GetStdHandle',\
	 kGetVersion,'GetVersion',\
	 kWideCharToMultiByte,'WideCharToMultiByte',\
	 kWriteConsoleA,'WriteConsoleA',\
	 klstrlenW,'lstrlenW'

  import winmm,\
	 mPlaySoundA,'PlaySoundA',\
	 mPlaySoundW,'PlaySoundW'

  StringStart1 db 'Playing sound file...'
  StringEnd1:
  StringStart2 db 'ok', 13, 10
  StringEnd2:
  StringStart3 db 'failed', 13, 10
  StringEnd3:
  StringStart4 db 'An error has occured', 13, 10
  StringEnd4:
  StringStart5 db 'MicroWav 1.02, Copyright 2008, 2009 by Jan Vorel', 13, 10
  db 'Plays a sound file using PlaySound api (Ansi/Wide)', 13, 10
  db 'microwav <filename>', 13, 10
  StringEnd5:

section '.rsrc' data readable resource from 'd:\dev\software\stuff\MicroWav\MicroWav_Version.res'
;section '.reloc' fixups data discardable ;readable discardable...?
