-- cmdsource 
--    CMD.EXE のバッチファイル実行して、定義された環境変数を NYAOS に
--    取り込むコマンド

function nyaos.command.cmdsource(...)
    local arg={...}
    if #arg < 1 then
        print('usage: cmdsource BATCHFILENAME ARG...')
        print('')
        print('  the command which execute the batch-file')
        print('  and load environment-variables defined on it.')
        return
    end
    if not nyaos.access(arg[1]) then
        print(arg[1] .. ": no such file or directory")
        return
    end 
    local tmpfile = os.tmpname()
    tmpfile = table.concat({string.byte(tmpfile,1,tmpfile:len())})
    local TEMP=os.getenv("TEMP")
    if TEMP and string.len(TEMP) > 0 then
        tmpfile = TEMP .. '\\' .. tmpfile
    end

    for i=1,#arg do
        if string.match(arg[i],' ') then
            arg[i] = '\034'..arg[i]..'\034'
        end
    end

    os.execute(table.concat(arg,' ')..' & set > '..tmpfile)

    for line in io.lines( tmpfile ) do
        local left,right = string.match(line,'([^=]+)=(.*)$')
        if left and right and os.getenv(left) ~= right then
            print('SET '..left..'='..right)
            nyaos.exec('SET '..left..'='..right)
        end
    end

    os.remove(tmpfile)
end
