/* Scandir.rexx
 * Support script for pictures.fdb
 * Copyright © 1995-1996 Nils Bandener
 * $VER: scandir.rexx 5.7 (22.12.96)
 */

Options Results
Address FIASCO

Signal on Syntax
Signal on Halt
Signal on Break_C
Signal on Failure

F_LockGUI

/*
 *  Get the directory to search in
 */

F_RequestFile Drawersonly ProjectRelative

if rc = 0 then do

    /*
     *  User did not cancel the requester
     */

    directory = Result

    F_RequestChoice '"Please select directory scan mode:*n1 - Selected only*n2 - Scan subdirectories*n3 - Scan subdirectories and lha archives" "1|2|3"'
    Mode = Result

    /*
     *  Create a list
     */

    say "Scanning directory..."

    if mode = 2 | mode = 3 then
    do
        moreargs = "all"
    end
    else
    do
        moreargs = ""
    end

    Address Command 'list >t:fpdb_files "' || directory || '" lformat %s%s files pat ~(#?.info) ' || moreargs

    if rc = 0 then
    do

        Address Command "sort t:fpdb_files t:fpdbs_files"

        Address FIASCO

        say "Adding pictures... (Press Ctrl-C for break)"

        if Open("g", "t:fpdbs_files", "read") then do

            do while ~eof("g")
                file = ReadLn("g")

                if file ~= "" then
                do
                    if right(file, 4) = ".lha" then
                    do
                        Address Command 'lha >t:fpdb_lha_files vq "' || file || '"'

                        if rc == 0 then
                        do
                            if Open("arc", "t:fpdb_lha_files", "read") then
                            do
                                dummy = readln("arc")
                                dummy = readln("arc")
                                dummy = readln("arc")

                                do while ~eof("arc")

                                    arcfile = readln("arc")

                                    if arcfile ~= "" then
                                    do
                                        say "Adding picture " || arcfile || " in archive " || file || "..."

                                        Address Command 'rx addpicture.rexx ' || file || '/__in_archive__/' || arcfile
                                    end
                                end

                                call Close("arc")
                            end
                        end
                    end
                    else
                    do
                        say "Adding picture " || file || "..."

                        Address Command 'rx addpicture.rexx ' || file
                    end
                end
            end

            call Close("g")
        end
    end
end

F_UnlockGUI

exit

/*
 *  Error handling
 */

Syntax:
Failure:
say "Error" rc "in line" sigl ":" errortext(rc)
say file pat

Halt:
Break_C:

call Close("g")

Address FIASCO

F_UnlockGUI
F_ResetStatus

say "Break"

exit



