/* TransferKick.rexx
Program to calculate the hashes for Kickstart files on FAT32 drive and transfer matching files to 
DEVS:Kickstarts for use in WHDLoad.
*/

if exists('EMU68BOOT:Kickstarts/') then nop
else do
   Say "Kickstarts folder not found! Nothing to do!"
   exit
   end
vCmd='delete T:#?.md5 QUIET >NIL:'
address command vCmd
vCmd='delete T:RomHashes.txt QUIET >NIL:'
address command vCmd
vCmd='delete T:RunHashes QUIET >NIL:'
address command vCmd

fENVKickstart='ENV:KickstartVersion'

open(vKickstartLine,fENVKickstart,'read')
vKickstartFound=readln(vKickstartLine)
close(vKickstartLine)

vKickstartVersion=vKickstartFound

/*
address command 'rx "push `getenv KickstartVersion`"'
pull rxvar
vKickstartVersion=rxvar
*/

Say "Checking Kickstarts on FAT32 Kickstarts Folder to Transfer to WHDLoad"
Say ""
Say "Press Enter to continue"
Pull input
Say "Creating hashes of Roms in Kickstarts Folder"
Say ""

if vKickstartVersion=3.2 then do
   vCmd='C:md5sum EMU68BOOT:Kickstarts >T:RomHashes.txt'
   address command vCmd
end
if vKickstartVersion=3.1 | vKickstartVersion=3.9 then do
/*    vCmd='List >>T:RunHashes EMU68boot:Kickstarts PAT=~(#?.info) LFORMAT="C:MD5sum -b *"%p%n*" >*"T:%n.MD5*""' */
    vCmd='List >>T:RunHashes EMU68boot:Kickstarts PAT=~(#?.info) LFORMAT="C:MD5sum -b *"%p%n*" >>T:RomHashes.txt"' 
    address command vCmd
    vCmd='execute T:RunHashes'
    address command vCmd
/*
    vCmd='join "T:#?.md5" as T:RomHashes.txt'
    address command vCmd
*/
end
Say "Hashes created!"

File_ListofKickstartHashes='S:KickStartRomHashes'
File_ListofKickstartstoCheck='T:RomHashes.Txt'

IF EXISTS(File_ListofKickstartstoCheck) THEN nop
else do
    SAY "No files in directory!"
    exit 0
END

Say "Determining valid Kickstart files and copying to DEVS:Kickstarts folder"

open(vKickstartstoCheck,File_ListofKickstartstoCheck,'read')      
    Do until eof(vKickstartstoCheck)
        vKickstarttoCheck=readln(vKickstartstoCheck)
        if vKickstartVersion=3.1 | vKickstartVersion=3.9 then do
            parse var vKickstarttoCheck vKickstarttoCheckHash' *'vKickstarttoCheckPath
        end
        if vKickstartVersion=3.2 then do
            parse var vKickstarttoCheck vKickstarttoCheckHash'  'vKickstarttoCheckPath
        end
        parse var vKickstarttoCheckPath '/'vKickstarttoCheckName
        if vKickstarttoCheckPath~='' then do
            say "Checking "vKickstarttoCheckPath
            Match=0
            open(ListofKickstartHashes,File_ListofKickstartHashes,'read')  
                DO until Match=1 | eof(ListofKickstartHashes)
                    vKickstartHashLine = READLN(ListofKickstartHashes)
                    parse var vKickstartHashLine vKickstarttoCheckAgainstHash';'vKickstarttoCheckAgainstName
                    if vKickstarttoCheckHash=vKickstarttoCheckAgainstHash then do
                        Match=1
                        Say "Found match for "vKickstarttoCheckName"! ("vKickstarttoCheckAgainstName")"
                    end 
                end
            close(ListofKickstartHashes)          
            if Match=1 then do
                vPath='DEVS:Kickstarts/'vKickstarttoCheckName
                vCmd='TomCopy "'vKickstarttoCheckPath'" "DEVS:Kickstarts/'vKickstarttoCheckAgainstName'" QUIET' 
                Say vCmd
                address command vCmd
                vCmd='TomDelete "'vKickstarttoCheckPath'"'   
                Say vCmd
                address command vCmd
            end
            If Match=0 then do
                say "No match found for file "vKickstarttoCheckName
            end
        end
    end
Close(vKickstartstoCheck)    
/* 
vCmd='delete T:#?.md5 QUIET'
address command vCmd
vCmd='delete T:RomHashes.txt QUIET'
address command vCmd
vCmd='delete T:RunHashes QUIET'
address command vCmd
*/
Say "Program complete! You can now close this window"
