packages feed

spade-0.1.0.9: samples/filechecker.spd

proc getNumberSelection()
  loop
    let selection = try(stringtonum(csinputline("Please enter your selection?")), 0)
    csdraw()
    if (selection !== 0) then
      return selection
    endif
  endloop
endproc

proc selectActionScreen()
  println("What do you want to do?")
  println("1. Add files to the catalog")
  println("2. Check files in catalog")
  println("3. Mount sources")
  if (selection == 1) then
    return "ADD_FILES"
  elseif (selection == 2) then
    return "CHECK_FILES"
  elseif (selecton == 3) then
    return "MOUNT_SOURCE"
  endif
endproc

proc getCatalogPath()
  return inputline("\nPlease enter path to catalog: ")
endproc

proc readCatalog(cp)
  return jsondecode(readfile(cp))
endproc

proc selectPath(dirsonly)
  let current_path = getcurrentdir()
  loop
    csclear()
    csgoto(0, 0)
    csprintln("Select path to ", (if (dirsonly) then "directory" else "file"), " :")
    csprintln("===================")
    csprintln("Current selected: ", current_path)
    csprintln("")
    csprintln("Select action or path")
    csprintln("---------------------")
    let dh = opendir(current_path)
    let subdirs = []
    foreach dh as ditem 
      if dirsonly then
        if (ditem.type == "dir") then
          let subdirs = insertright(subdirs, ditem)
        endif
      else
        let subdirs = insertright(subdirs, ditem)
      endif
    endforeach
    csprintln(1, ". Proceed")
    csprintln(2, ". Go up")
    let offset = 2
    let i = offset
    foreach subdirs as subdir 
      let i = (i + 1)
      csprintln(i, ". ", subdir.path)
    endforeach
    csdraw()
    let selection = getNumberSelection()
    if (selection == 1) then
      return current_path
    elseif (selection == 2) then
      let current_path = takedirectory(current_path)
    else
      let realindex = (selection - offset)
      if ((realindex <= size(subdirs)) and (realindex > 0)) then
        let di = subdirs[realindex]
        if (di.type == "dir") then
          let current_path = di.path
        else
          return di.path
        endif
      endif
    endif
    csdraw()
  endloop
endproc

proc checkfile(filehash, path)
  let currentpos = csget()
  csgoto(0, currentpos.y)
  let fh = openfilehandle(path, "r")
  let filesize = gethandlesize(fh)
  csprintln("Checking file:", path)
  csprintln("Size:", filesize)
  csclearline()
  let hctx = hashinit("md5")
  let readbytes = 0
  loop
    let chunk = readfilehandle(fh, 4096)
    let hctx = hashupdate(hctx, chunk)
    if (size(chunk) == 0) then
      let finalhash = hexencode(hashfinalize(hctx))
      if (finalhash == filehash) then
        csprintln("OK")
      else
        csprintln("HASH MISMATCH!")
        csdraw()
        wait(1)
      endif
      break
    else
      let readbytes = readbytes + size(chunk)
      csgoto(0, currentpos.y + 2)
      csclearline()
      let p = round(readbytes/filesize * 100)
      csprintln("Hashing: ", take(round(p/10), "=========="), " ", p, "%")
      csclearline()
      csdraw() 
    endif
  endloop
  csdraw()
  csgoto(currentpos.x, currentpos.y)
endproc

proc mainscreen(catalogref, mountedpathsref)
  let catalogpath = readref(catalogref)
  let mountedpaths = readref(mountedpathsref)
  csclear()
  csgoto(0, 0)
  csprintln("Catalog path = ", (if ((catalogpath == "")) then "(empty)" else catalogpath))
  csprintln("Mounted paths:")
  csprintln("")
  let i = 0
  foreach mountedpaths as mp 
    let i = (i + 1)
    csprintln(i, ". ", mp.key, " -> ", mp.value)
  endforeach
  csprintln("-----------------")
  csprintln("Selection action")
  csprintln("-----------------")
  csprintln("1. Select catalog path")
  csprintln("2. Mount sources")
  csprintln("3. Run check")
  csprintln("4. Add files")
  csprintln("5. Exit")
  let selection = getNumberSelection()
  if (selection == 1) then
    let catalogPath = selectPath(false)
    writeref(catalogref, catalogPath)
    return 0
  elseif (selection == 2) then
    let mountsource = selectPath(false)
    if (takebasename(mountsource) == "PMSRCID") then
      let pmsrcid = trim(readtextfile(mountsource))
      let currentpaths = readref(mountedpathsref)
      writeref(mountedpathsref, addkey(currentpaths, pmsrcid, takedirectory(mountsource)))
    endif
    return 0
  elseif (selection == 3) then
    let catalogPath = readref(catalogref)
    let mountedPaths = readref(mountedpathsref)
    if (catalogPath !== "") then
      let catalog = readCatalog(catalogPath)
      foreach catalog as v 
        foreach v.value as s 
          if haskey(mountedPaths, s.key) then
            foreach s.value as si 
              checkfile(v.key, joinpaths(mountedPaths[s.key], si))
            endforeach
          endif
        endforeach
        wait(0.001)
      endforeach
    endif
    return 0
  elseif (selection == 4) then
    break
  else
    return -1
  endif
  csdraw()
endproc
csinit()
let catalogpathref = newref("")
let mountedpathsref = newref({})
loop
  if (mainscreen(catalogpathref, mountedpathsref) == -1) then
    break
  endif
endloop

-- let catalogPath = getCatalogPath()
-- let catalogPath = selectPath(false)
-- csclear()
-- csprintln("Catalog path = ", catalogPath)
-- csdraw()
-- getkey()