include "inc/cmdsys.plh"
include "inc/args.plh"
include "inc/fileio.plh"
include "inc/matchfiles.plh"
include "dhgr.tk/inc/dhgrlib.plh"
include "dhgr.tk/inc/dhgrutils.plh"

sysflags restxt1|resxtxt1|reshgr1|resxhgr1 //  Reserve all the pages

const FRAME_WIDTH    = 80
const FRAME_HEIGHT   = 48
const FRAME_SIZE     = FRAME_WIDTH * FRAME_HEIGHT / 2
const DLGRFRAME_SIZE = 2047
const PAUSE          = 1
const HEIGHT_X2      = 2
const WRITE_DHGR     = 4

var arg, srcfilelist, srcpath, srcexp, srccnt
var entrylist, entrycnt
byte[FRAME_SIZE*2] frame
byte flags

//
// Screen row address arrays.
//
word rowofst = $0000,$0080,$0100,$0180,$0200,$0280,$0300,$0380
word         = $0028,$00A8,$0128,$01A8,$0228,$02A8,$0328,$03A8
word         = $0050,$00D0,$0150,$01D0,$0250,$02D0,$0350,$03D0
byte  rotclr = $00,$22,$44,$66,$888,$AA,$CC,$EE
byte         = $11,$33,$55,$77,$99,$BB,$DD,$FF

def dlgr2dcgr(dlgrptr)#0
    byte i, j, cl, ch
    var pixptr, rowptr

    pixptr = @frame
    for j = 0 to 47 step 2
        rowptr = dlgrptr + rowofst[j / 2]
        for i = 0 to 39
            cl = ^(rowptr + i + 1024)
            cl = (rotclr[cl & $0F] & $0F) | (rotclr[cl >> 4] & $F0)
            ch = ^(rowptr + i)
            ^(pixptr + i) = (cl & $0F) | (ch << 4)
            if flags & HEIGHT_X2
                ^(pixptr + i + 40)  = (cl & $0F) | (ch << 4)
                ^(pixptr + i + 80)  = (cl >> 4)  | (ch & $F0)
                ^(pixptr + i + 120) = (cl >> 4)  | (ch & $F0)
            else
                ^(pixptr + i + 40) = (cl >> 4)  | (ch & $F0)
            fin
        next
        if flags & HEIGHT_X2
            pixptr = pixptr + 160
        else
            pixptr = pixptr + 80
        fin
    next
end

def dlgrRead(filestr)#1
    var frameptr
    byte refnum

    frameptr = 0
    refnum = fileio:open(filestr)
    if refnum
        frameptr = heapalloc(DLGRFRAME_SIZE)
        if frameptr and fileio:read(refnum, frameptr, DLGRFRAME_SIZE) <> DLGRFRAME_SIZE
            heaprelease(frameptr)
            frameptr = NULL
            dhgrMode(DHGR_TEXT_MODE)
            puts("Error reading: "); puts(filestr); putln
        fin
        fileio:close(refnum)
    fin
    return frameptr
end

def dhgrWrite(filename)#1
    char[64] screenfile

    strcpy(@screenfile, filename)
    strcat(@screenfile, ".DHGR")
    if screenWrite(@screenfile)
        dhgrMode(DHGR_TEXT_MODE)
        puts("Error writing: "); puts(@screenfile); putln
        return FALSE
    fin
    return TRUE
end

def dlgrConvert(filename)#1
    var dlgrptr
    byte i, j, numrows

    dcgrColor(CLR_GREY1)
    dhgrClearBl7(0, 0, SCR_WIDTH7, SCR_HEIGHT)
    dlgrptr = dlgrRead(filename)
    if dlgrptr
        dlgr2dcgr(dlgrptr)
        heaprelease(dlgrptr)
        if flags & HEIGHT_X2
            dcgrPixMap(30, 48, FRAME_WIDTH, FRAME_HEIGHT*2, @frame)
        else
            dcgrPixMap(30, 72, FRAME_WIDTH, FRAME_HEIGHT, @frame)
        fin
        if flags & WRITE_DHGR
            return dhgrWrite(filename)
        fin
        return TRUE
    fin
    return FALSE
end

arg = argNext(argFirst)
while arg and ^(arg + 1) == '-'
    when toupper(^(arg + 2))
        is 'P'
            flags = flags | PAUSE
            break
        is '2'
            flags = flags | HEIGHT_X2
            break
        is 'W'
            flags = flags | WRITE_DHGR
            break
    wend
    arg = argNext(arg)
loop
while ^arg
    //
    // Add to source list
    //
    srcpath, srcexp = splitPathFile(arg)
    entrylist, entrycnt = matchList(srcpath, srcexp)
    if not entrylist
        puts(arg); puts(" ???\n")
    else
        if not srcfilelist
            srcfilelist = entrylist
        fin
        srccnt = srccnt + entrycnt
        while entrycnt
            //
            // Prepend path to filename
            //
            strcpy(entrylist, strcat(strcpy(heapmark, srcpath), entrylist))
            entrylist = entrylist + t_fileentry
            entrycnt--
        loop
    fin
    arg = argNext(arg)
loop
if srccnt
    dhgrMode(DHGR_COLOR_MODE)
    while srccnt and dlgrConvert(srcfilelist)
        if flags & PAUSE; getc; fin
        srcfilelist = srcfilelist + t_fileentry
        srccnt--
    loop
    dhgrMode(DHGR_TEXT_MODE)
    return 0
fin
puts("Usage: DHGRGREASY [-P] [-2] [W] FRAMEFILE\n")
done
