#(English)
#   This file _nya is the script NYAOS execute on starting up.
#   NYAOS reads _nya(s) on these directories in the same order as written:
#    (1) _nya on the same directory as NYAOS.EXE
#    (2) %HOME%\_nya or %USERPROFILE%\_nya
#    (3) The current directory
# I recommend you to
#    - put the original _nya on (1)
#    - put your customized _nya on (2)
#
#(Japanese)
# _nya  NYAOS ̏ݒt@Cł.
#   (1) NYAOS.EXE ̂fBNg
#   (2) %HOME% ̉(`ł %USERPROFILE%)
#   (3) JgfBNg
# ɂ _nya uɑSāvĂяo܂.
#  A(1)==(3) ̏ꍇ́A(3)Ăяo܂.
#
# ʏ
#    - NYAOS.EXE ɓYt _nya (t@C)  (1) 
#    - ̃[UŒ`ݒ (2) 
# zuĂ.

if /%OS%/ == /Windows_NT/ then
    alias  kill  taskkill /pid
    alias  kill9 taskkill /f /pid
    alias  ps    tasklist
    suffix vbs   cscript //nologo
    suffix js    cscript //nologo
endif

suffix ny  nyaos -f
suffix pl  perl
suffix py  python
suffix rb  ruby
suffix jar java -jar
suffix lua lua
suffix awk awk -f

option +backquote
option +history
option +nullcomplete
option +tilde
option +glob
option prompt $e[31;40;1m[$w]$_$$ $e[37;1m
option uncompletechar ,;=``

foreach i (mkdir rmdir type md rd start mklink)
    alias $i $COMSPEC /c $i
end

foreach cmd (dir copy move del rename ren del attrib)
    $cmd{
        if %glob.defined% -ne 0 then
            option -glob
            $COMSPEC /c $0 $*
            option +glob
        else
            $COMSPEC /c $0 $*
        endif
    }
end

bindkey CTRL_P vzlike-previous-history
bindkey CTRL_N vzlike-next-history

### VzCNȃL[oChɂ֐
### pɂ́uvzbindvƂ̍sĂB
vzbind{
    bindkey CTRL_R xscript:start            xscript:pageup
    bindkey CTRL_S backward                 xscript:backward
    bindkey CTRL_D forward                  xscript:forward
    bindkey CTRL_E vzlike-previous-history  xscript:previous
    bindkey CTRL_X vzlike-next-history      xscript:next
    bindkey CTRL_F forward-word
    bindkey CTRL_A backward-word
}
# vzbind

lua_e "
    --- NYAOS ĨtbN ---
    function nyaos.goodbye.message()
        print('Good bye !!')
    end

    -- L[tbNTv --
    -- (unyaos.keyhook = sample_keyhookvŗp\)
    function sample_keyhook(t)
        if t.key == nyaos.key.F1 and t.text == '' then
            return os.getenv('EDITOR') or 'notepad',true
        end
        return nil
    end

    -- R}hCtB^[Tv:u$lua(c)v(ʓLua) --
    -- (unyaos.filter.sample = sample_filtervŗp\ɂȂ)
    function sample_filter(cmdline)
        return cmdline:gsub('$lua(%b())',function(m)
            local status,result=pcall( loadstring('return '..m) )
            if status then
                return result
            else
                print('Ignore invalid Lua expression: '..m)
                return false;
            end
        end)
    end

    --- which R}h ---
    function nyaos.command.which(cmd)
        local path='.;' .. os.getenv('PATH')

        --- ꍇ́APATH ̈ꗗ\邾 ---
        if not cmd or cmd:len()==0 then
            for path1 in path:gmatch('[^;]+') do
                print(path1)
            end
            return
        end

        local cmdl=cmd:lower()

        --- GCAX ---
        local a=nyaos.alias[ cmdl ]
        if a then
            print('aliased as '..a)
        end
        --- ֐ ---
        local f=nyaos.functions[ cmdl ]
        if f then
            print('defined as function')
        end

        local variation={
            [ cmdl ] = true ,
            [ cmdl .. '.exe' ] = true ,
            [ cmdl .. '.cmd' ] = true ,
            [ cmdl .. '.bat' ] = true ,
            [ cmdl .. '.com' ] = true
        }
        for key,val in pairs(nyaos.suffix) do
            variation[ cmdl .. '.' .. key:lower() ] = true
        end

        for path1 in path:gmatch('[^;]+') do
            for fname in nyaos.dir(path1) do
                if variation[ fname:lower() ] then
                    print( path1 .. '\\' .. fname )
                end
            end
        end
    end

    -- u-tvIvVtŋNꂽF
    for i,e in pairs(nyaos.argv) do
        if e == '-t' then
            -- ls ɃJ[IvVt --
            nyaos.alias.ls = (nyaos.alias.ls or 'ls')..' --color -x'

            if os.getenv('VIMSHELL') == '1' then
                nyaos.option.ls_colors='fi=37:di=32:sy=31:ro=34:hi=33:ex=35:ec=0'
                nyaos.option.prompt='$e[31m[$w]$_$$ $e[37m'
                nyaos.option.term_clear = ''
                nyaos.option.term_cursor_on = ''

                function nyaos.command.complete_test(arg)
                    local list=nyaos.default_complete(arg,1)
                    for i=1,#list do
                        print(list[i][2])
                    end
                end
            else
                nyaos.option.ls_colors='fi=30:di=32:sy=31:ro=34:hi=33:ex=35:ec=0'
                nyaos.option.prompt='$e[31m[$w]$_$$ $e[30m'
            end
        end
    end

    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
        local tmpfile = os.tmpname()
        tmpfile = table.concat({string.byte(tmpfile,1,tmpfile:len())})
        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 os.getenv(left) ~= right then
                print('SET '..left..'='..right)
                nyaos.exec('SET '..left..'='..right)
            end
        end

        os.remove(tmpfile)
    end

    function nyaos.command.lnk(src,dst)
        if not dst then
            print('Usage: lnk FILENAME SHORTCUT')
            return 1
        end
        local fsObj=nyaos.create_object('Scripting.FileSystemObject')
        if not string.match(src,'^http://') then
            src = fsObj:GetAbsolutePathName(src)
        end
        dst = fsObj:GetAbsolutePathName(dst)
        if fsObj:FolderExists(dst) then
            dst = dst .. '\\' .. fsObj:getFileName(src)
        end
        if string.match(src,'^http://') then
            dst = dst .. '.url'
        elseif not string.match(dst,'%.lnk$') then
            dst = dst .. '.lnk'
        end
        print('    '..src)
        print('--> '..dst)

        local shell = assert(nyaos.create_object('WScript.Shell'))
        local shortcut=assert(shell:CreateShortcut(dst))
        shortcut.TargetPath=src
        shortcut:Save()
    end
"
