rsfiltool.rb / last modification: 2020-01-30 14:16
# program   : rsfiltool
# copyright : PRAGMA Publishing On Demand
# version   : 1.01 - 2002
# author    : Hans Hagen
#
# project   : eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-pod.com / www.pragma-ade.com

unless defined? ownpath
    ownpath = $0.sub(/[\\\/]\w*?\.rb/i,'')
    $: << ownpath
end

# --name=a,b,c.xml wordt names [a.xml, b.xml, c.xml]
# --path=x/y/z/a,b,c.xml wordt [x/y/z/a.xml, x/y/z/b.xml, x/y/z/c.xml]

# todo : split session stuff from xmpl/base into an xmpl/session module and "include xmpl/session" into base and here and ...

require &#39;fileutils'
# require 'ftools'
require &#39;xmpl/base'
require &#39;xmpl/switch'
require &#39;xmpl/request'

session = Example.new(&#39;rsfiltool', '1.01', 'PRAGMA POD')

filterprefix = &#39;rsfil-'

commandline = CommandLine.new

commandline.registerflag(&#39;submit')
commandline.registerflag(&#39;fetch')
commandline.registerflag(&#39;report')
#commandline.registerflag('split')
commandline.registerflag(&#39;stamp')
commandline.registerflag(&#39;silent')
commandline.registerflag(&#39;request')
commandline.registerflag(&#39;nobackup')

commandline.registervalue(&#39;filter')

commandline.registervalue(&#39;root')
commandline.registervalue(&#39;path')
commandline.registervalue(&#39;name')

commandline.expand

session.set(&#39;log.silent',true) if commandline.option('silent')

session.inherit(commandline)

session.identify

# session.exit unless session.loadenvironment

def prepare (session)

    # Normally the system provides the file, but a user can provide the rest; in
    # order to prevent problems with keying in names, we force lowercase names.

    session.set(&#39;option.file',session.get('argument.first')) if session.get('option.file').empty?

    root = session.get(&#39;option.root').downcase
    path = session.get(&#39;option.path').downcase
    name = session.get(&#39;option.name').downcase
    file = session.get(&#39;option.file').downcase

    session.error(&#39;provide file') if file.empty?
    session.error(&#39;provide root') if root.empty?

    filter = session.get(&#39;option.filter').downcase
    trash  = session.get(&#39;option.trash').downcase

    trash = &#39;' unless FileTest.directory?(trash)

    if not filter.empty? then
        begin
            require filter
        rescue Exception
            begin
                require filterprefix + filter
            rescue Exception
                session.error(&#39;invalid filter')
            end
        end
        begin
            if RSFIL::valid?(file) then
                split = RSFIL::split(file,name)
                path = if split[0].downcase then split[0] else &#39;' end
                file = if split[1].downcase then split[1] else &#39;' end
                name = if split[2].downcase then split[2] else &#39;' end
                session.report(&#39;split result',split.inspect)
                session.error(&#39;unable to split off path') if path.empty?
                session.error(&#39;unable to split off file') if file.empty?
                session.error(&#39;unable to split off name') if name.empty?
                session.set(&#39;option.path',path) if path
                session.set(&#39;option.file',file) if file
                session.set(&#39;option.name',name) if name
            else
                session.error(&#39;invalid filename', file)
                unless trash.empty? then
                    File.copy(file,trash + &#39;/' + file)
                end
            end
        rescue
            session.error(&#39;unable to split',file,'with filter',filter)
        end
    end

    session.error(&#39;provide path') if path.empty?

    session.error(&#39;invalid root') unless test(?d,root)

    exit if session.error?

    session.set(&#39;fb.filename',file)

    path.gsub!(/\\/o, &#39;/')
    path.gsub!(/\s/o, &#39;')

    path = root + &#39;/' + path

    # multiple paths

    if path =~ /^(.*)\/(.*?)$/o then
        prepath = $1
        postpath = $2
        paths = postpath.split(/\,/)
        paths.collect! do |p|
            prepath + &#39;/' + p
        end
    else
        paths = Array.new
        paths.push(path)
    end

    paths.collect! do |p|
        p.gsub(/[^a-zA-Z0-9\s\-\_\/\.\:]/o, &#39;-')
    end

    file.gsub!(/\\/o, &#39;/')
    file.gsub!(/[^a-zA-Z0-9\s\-\_\/\.\:]/o, &#39;-')

#    if session.get('option.split')
#        if file =~ /(.*)\.(.*?)$/o
#            path = path + '/' + $1
#        else
#            session.error('nothing to split in filename')
#        end
#    end

    paths.each do |p|
        begin
            session.report(&#39;creating path', p)
            File.makedirs(p)
        rescue
            session.error(&#39;unable to create path', p)
        end
    end

    name.gsub!(/\s+/,&#39;')

    # can be a,b,c.exa.saved => a.exa.saved,b.exa.saved,c.exa.saved

    if name =~ /(.*?)\.(.*)$/
        name = $1
        suffix = $2
        names = name.split(/\,/)
        names.collect! do |n|
            n + &#39;.' + suffix
        end
        name = names.join(&#39;,')
    else
        names = name.split(/\,/)
    end

    session.set(&#39;fb.path',path)
    session.set(&#39;fb.paths',paths)
    session.set(&#39;fb.name',name)
    session.set(&#39;fb.names',names)

end

def thefullname(path,file,name=&#39;')

    filename = file.gsub(/.*?\//, &#39;')

    if name.empty?
        path + &#39;/' + filename
    else
        unless name =~ /\..+$/o  # unless name.match(/\..+$/o)
            if filename =~ /(\..+)$/o  # if file.match(/(\..+)$/o)
                name = name + $1
            end
        end
        path + &#39;/' + name
    end

end

def submitfile (session)

    filename = session.get(&#39;fb.filename')
    paths = session.get(&#39;fb.paths')
    names = session.get(&#39;fb.names')

    paths.each do |path|
        session.report(&#39;submitting path',path)
        names.each do |name|
            session.report(&#39;submitting file',filename,'to',name)
            submit(session,path,filename,name)
        end
    end

end

def submitlist (session)

    requestname = session.get(&#39;fb.filename')
    paths = session.get(&#39;fb.paths')

    if test(?e,requestname)
        session.report(&#39;loading request file', requestname)
        if request = ExaRequest.new(requestname)
            filelist = request.files
            if filelist && (filelist.size > 0)
                filelist.each do |filename|
                    paths.each do |path|
                        session.report(&#39;submitting file from list', filename)
                        submit(session,path,filename,request.naturalname(filename))
                    end
                end
            else
                session.warning(&#39;no filelist in', requestname)
            end
        else
            session.warning(&#39;unable to load', requestname)
        end
    else
        session.warning(&#39;no file', requestname)
    end

end

def submit (session, path, filename, newname)

    fullname = thefullname(path,newname)

    unless test(?e,filename)
        session.warning(&#39;no file to submit', filename)
        return
    end

    begin
        File.copy(fullname,fullname+&#39;.old') if ! session.get('nobackup') && test(?e,fullname)
        if test(?e,filename)
            File.copy(filename,fullname)
            session.report(&#39;submit', filename, 'in', fullname)
            if session.get(&#39;option.stamp')
                f = open(fullname+&#39;.tim','w')
                f.puts(Time.now.gmtime.strftime("%a %b %d %H:%M:%S %Y"))
                f.close
            end
        else
            session.error(&#39;unable to locate', filename)
        end
    rescue
        session.error(&#39;unable to move', filename, 'to', fullname)
    end

end

def fetch (session)

    filename = session.get(&#39;fb.filename')
    paths = session.get(&#39;fb.paths')
    name = session.get(&#39;fb.name')

    begin
        File.copy(filename,filename+&#39;.old') if ! session.get('nobackup') && test(?e,filename)
        paths.each do |path|
            #  fullname = thefullname(path,request.naturalname(filename))
            # fullname = thefullname(path,filename)
            fullname = thefullname(path,name)
            if test(?e,fullname)
                File.copy(fullname,filename)
                session.report(&#39;fetch', filename, 'from', fullname)
                return
            else
                session.report(&#39;file',fullname, 'is not present')
            end
        end
    rescue
        session.error(&#39;unable to fetch file from path')
    end
    session.error(&#39;no file',filename, 'fetched') unless test(?e,filename)

end

def report (session)

    filename = session.get(&#39;fb.filename')
    paths = session.get(&#39;fb.paths')

    paths.each do |path|
        fullname = thefullname(path,request.naturalname(filename))
        if test(?e,fullname)
            begin
                session.report(&#39;file', fullname)
                session.report(&#39;size', test(?s,fullname))
                if test(?e,fullname+&#39;.tim')
                    str = IO.readlines(fullname+&#39;.tim')
                    # str = IO.read(fullname+'.tim')
                    session.report(&#39;time', str)
                end
            rescue
                session.error(&#39;unable to report about', fullname)
            end
        end
    end

end

if session.get(&#39;option.submit')
    prepare(session)
    if session.get(&#39;option.request')
        submitlist(session)
    else
        submitfile(session)
    end
elsif session.get(&#39;option.fetch')
    prepare(session)
    fetch(session)
elsif session.get(&#39;option.report')
    prepare(session)
    report(session)
else
    session.report(&#39;provide action')
end