// alias.src
// @description general alias-cli, create alias for any existed application
// @version 0.2
// TODO list
//      [+] alias list
//      [+] alias <string>=<string>
//      [] alias -g <string>=<string>   
pass = @yield
globals.sh = get_shell
globals.pc = get_shell.host_computer
globals.NAME = "alias"
globals.paths = ["/bin",home_dir]
// meta functions
SHOW_USAGES = function
    u_1 = "\n<b>Name</b>\n" + char(9) + "alias\n\n"
    u_2 = "<b>Gramma</b>\n" + char(9) + "alias [opt:-g] alias_name=command_name\n" + char(9) + "alias list\n\n"
    u_3 = "<b>Examples</b>\n" + char(9) + "alias -g py=python\n" + char(9) + "alias p=ping\n\n"
    u_4 = "<b>Explain</b>\n" + char(9) + "alias used to define a short name for terminal commands.\n\n"
    u_5 = char(9) + char(9) + "<b>-g</b> : set global alias. It will be set for current user if ignore it.\n\n"
    u_6 = char(9) + char(9) + "<b>list</b> : show alias settings\n\n"
    print u_1+u_2+u_3+u_4+u_5+u_6
end function
// from pythonic.src
map = function(proc, iterable_obj)
    res = []
    for obj in iterable_obj
        res.push(proc(obj))
    end for
    return res
end function
// temporary functions
get_file_name = function(file)
    return file.name
end function
cfg = {}
cfg.name = ".aliasrc"
cfg.path = home_dir
cfg.text = ""
cfg._load = function
    f = pc.File(self.path+"/"+self.name)
    if not f then 
        ok = pc.touch(self.path,self.name)
        if ok != 1 then
            print ok
            return -1
        end if
        f = pc.File(self.path+"/"+self.name)
    end if
    self.file = f
    self.text = f.get_content
    if self.text != "" then
        self.lines = self.text.split("\n")
    else
        self.lines = []
    end if
    return 0
end function
cfg.print = function
    if self.text == "" then 
        print "\n<b>no alias existed</b>\n"
    else
        print self.text
    end if
end function
cfg._varify = function
    l0 = map(@get_file_name, pc.File(paths[0]).get_files)
    l1 = map(@get_file_name, pc.File(paths[1]).get_files)
    cmds = l0+l1
    command_exists_1 = false
    command_exists_2 = false
    lines = self.text.split("\n")
    if lines.len != 0 then
        for l in lines
            kv = l.split("=")
            if kv.len != 2 then continue
            for i in cmds
                if kv[0] == i then command_exists_1 = true
                if kv[1] == i then command_exists_2 = true
            end for
            if not command_exists_1 then return -1
            if not command_exists_2 then return -1
        end for
    end if
    return 0
end function
// save alias to .aliasrc
cfg.save = function(name,cmd)
    record = name + "=" + cmd
    record_existed = false
    for i in self.lines
        if i == record then record_existed = true
    end for
    if not record_existed then
        self.lines.push(record)
        self.file.set_content(self.lines.join(char(10)))
    end if
end function
cfg.setup = function
    if self._load then exit "can't get file .aliasrc"
    if self._varify then exit "command not found"
end function
builder = {}
builder.generate_src = function(alias_path,alias_name,cmd_path,cmd_name)
    self.fpath = alias_path
    src = "get_shell.launch(""" + cmd_path + """,params.join("" ""))" 
    pc.touch(self.fpath,alias_name+".src")
    self.file = pc.File(self.fpath+"/"+alias_name+".src")
    self.file.set_content(src)
end function
builder.build = function(alias_path,alias_name,cmd_path,cmd_name)
    self.generate_src(alias_path,alias_name,cmd_path,cmd_name)
    sh.build(self.file.path, alias_path)
    self.file.delete
end function
parser = {}     // TODO: -g 
parser.is_global = false
parser.init = function
    if params.len == 0 or params.len > 2 then 
        SHOW_USAGES;exit 
    end if
    arg0 = params[0]
    if params.len == 2 then
        if arg0 == "-g" then
            self.is_global = true
            arg0 = params[1]
        else 
            exit "Unknown Argument " + arg0
        end if
    end if
    if arg0 == "-h" or arg0 == "--help" or arg0 == "?" then
        SHOW_USAGES;exit
    else if arg0 == "list" then
        cfg.print;exit
    end if
    kv = arg0.split("=")
    if kv.len != 2 then 
        exit "error alias format.\n example: alias p=ping"
    end if
    alias = kv[0]
    cmd = kv[1]
    l1 = pc.File(paths[0]).get_files
    l2 = pc.File(paths[1]).get_files
    clis = l1 + l2
    for i in clis
        if i.name == alias then exit alias + " has existed."
    end for
    for i in clis
        n = i.name
        p = i.path
        if n == cmd then 
            cfg.save(alias,cmd)
            builder.build(paths[0],alias,p,cmd)
            return
        end if
    end for
    exit  cmd + " not found"
end function
main = function
    cfg.setup
    parser.init
end function
main