sourceafFandocViewer::Main.fan

using flux::Main
using util

// afFandocViewer Announced at http://fantom.org/sidewalk/topic/2116

** Launch [Flux]`http://fantom.org/doc/flux/#overview` from the command line, passing in 
** a pod name, a uri, or a Windows file path. 
** 
** pre>
**   C:\> fan afFandocViewer fandoc://sys
**   C:\> fan afFandocViewer C:\Projects\FandocViewer\pod.fandoc
** <pre
class Main : AbstractMain {

    @Arg { help="pod name, file or url. e.g. afFandocViewer, pod.fandoc, http://www.fantomfactory.org/" }
    Str? file
    
    @Opt { help="Install Fandoc Viewer Flux Tools" } 
    Bool install
    
    @Opt { help="Print version"; aliases=["v"] } 
    Bool version
    
    override Int main(Str[] args := Env.cur.args) {
        argsOk := parseArgs(args)
        if (version)
            echo(Pod.of(this).name + "-" + Pod.of(this).version)
        if (install)
            return InstallScript().main
        if (helpOpt || file == null)
            return usage
        flux::Main.main([toUriStr(file)])
        return 0
    }

    override Int run() { -1 }
    
    ** *Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.*
    ** 
    ** > Jamie Zawinski (1997)
    ** 
    ** See `http://regex.info/blog/2006-09-15/247`
    internal static Str toUriStr(Str str) {
        // check if str is a pod name
        if (Pod.find(str, false) != null)
            return "fandoc://${str}"
        
        // check if str is too small to have a scheme
        if (str.size <= 3)
            return str
        
        if (str.chars[0].isAlpha && str.chars[1] == ':' && str.chars[2] == '\\')
            str = str.replace("\\", "/")    
        if (str.chars[0].isAlpha && str.chars[1] == ':' && str.chars[2] == '/')
            str = "file:///$str"
        return str
    }
}