'-----------------------------------------------------------------
' mkimg.vbs
'
' Windows script to generate image file compatible with Atom SDDOS
' from AtomDOS formatted disk images.
' SCRIPT IS DESIGNED FOR AND TESTED WITH WINDOWS XP!!
'
' Usage:
'  double click mkimg.vbs
'  select list file
'  select output binary file
'
' list file format:
'
' disk_image_filename, image description
' ....
' ....
'
' Description is capped at 13 characters.
'
' Script is quick and dirty - there is very little fancy
' error checking.
'
' You will see message indicating number of images found
' upon successful completion.
'
' The script is based upon a python script mkimg.py by SirMorris
' (C) KC 2010
'-----------------------------------------------------------------

  dim a, b, info, intResult


  function shortToFile (out,short)
    a=int(short / 256) and 255
    b=int(short) and 255
    out.write chr(b) & chr(a)
  end function


  function stringToFile (out,info)
    out.write  info
  end function


  function infoToFile (out,info,state)
    info=left(info & "             ",13)
    out.write info & chr(&h88) & chr(&h88) & chr(state)
  end function


  function showFileOpen()
    Set objDialog = CreateObject("UserAccounts.CommonDialog")
    objDialog.Filter = "Image list file|*.txt|All Files|*.*"
    objDialog.FilterIndex = 1
    objDialog.InitialDir = ""
    intResult = objDialog.ShowOpen
    If intResult = 0 Then
      Wscript.Quit
    Else
      showFileOpen=objDialog.FileName
    End If
  end function


  function showFileSave()
    Set objDialog = CreateObject("SAFRCFileDlg.FileSave")
    objDialog.FileName = "output.bin"
    objDialog.FileType = "Image file"
    intReturn = objDialog.OpenFileSaveDlg
    If intReturn Then
      showFileSave=objDialog.FileName
    Else
      Wscript.Quit
    End If
  end function


'-----------------------------------------------------------------
'Main
'-----------------------------------------------------------------
  Dim ForReading, adTypeBinary, adSaveCreateOverwrite
  Dim listfile, imagefile
  Dim oFilesys, oFile_out, objFSO, oFile_in, out,title
  Dim strLine, filenamedesc, filename, source, count, fCount
  Dim copyBuffer, tmpBuffer

  ForReading = 1
  adTypeBinary = 1
  adSaveCreateOverwrite = 2

  listfile = showFileOpen
  imagefile = showFileSave

  Set oFilesys = CreateObject("Scripting.FileSystemObject") 
  Set oFile_out = oFilesys.CreateTextFile(imagefile, True) 

  shortToFile oFile_out,0
  shortToFile oFile_out,1
  shortToFile oFile_out,2
  shortToFile oFile_out,3
  stringToFile oFile_out,"SDDOS   "

  count = 0

  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set oFile_in = objFSO.OpenTextFile(listfile, ForReading)

  Do While oFile_in.AtEndOfStream = False

    strLine = oFile_in.ReadLine
    filenamedesc = mid(strLine,instr(strLine,",") + 1)
    filename = left(strLine,instr(strLine,",") - 1)

    set source = WScript.CreateObject("ADODB.Stream")
    source.Open
    source.type = adTypeBinary
    source.LoadFromFile filename 
    size = source.size
    source.close
 
    if size <> 102400 then
      wscript.echo "** ERROR ** Disk image is not exactly 10240 bytes: " & filename
      wscript.quit
    end if

    infoToFile oFile_out,filenamedesc,&h0F
    count = count + 1

  Loop

  oFile_in.Close

  fcount = count

  while count < 1023
    infoToFile oFile_out," ",&hF0
    count = count + 1
  wend

  oFile_out.close

  Set oFile_in = objFSO.OpenTextFile(listfile, ForReading)

  Do While oFile_in.AtEndOfStream = False

    strLine = oFile_in.ReadLine
    filename = left(strLine,instr(strLine,",") - 1)

    set source = WScript.CreateObject("ADODB.Stream")
    source.Open
    source.type = adTypeBinary
    source.LoadFromFile filename 
    copyBuffer = source.Read()
    source.close
  
    set out = WScript.CreateObject("ADODB.Stream")
    out.Open
    out.type = adTypeBinary
    out.LoadFromFile imagefile 
    tmpBuffer = out.Read()
    out.Position = 0
    out.SetEOS
    out.write tmpBuffer
    out.write copyBuffer
    out.SaveToFile imagefile,adSaveCreateOverwrite
    out.close

  Loop

  oFile_in.close

  wscript.echo "OK! " & fcount & " images found and compiled to " & imagefile