
is.file <- function(file)
    unix(paste("test -f", file), output = F) == 0
unix.true <- function(command)
    unix(paste("if", command, "; then echo 1; else echo 0; fi"))

xgobi <- function(matrx,
                  collab=dimnames(matrx)[[2]], rowlab=dimnames(matrx)[[1]],
                  std = "mmx", dev = 2.0,
                  vgroups=NULL, colors=NULL, glyphs=NULL, erase=NULL,
                  lines=NULL, resources=NULL,
                  display=NULL, name=NULL, title=NULL, mach=NULL)
{
    if (missing(mach))
    {
        mach <- "sun3"
        if (is.file("/bin/arch"))
            mach <- unix("/bin/arch")
        else if (is.file("/bin/sun4") && unix.true("/bin/sun4") == 0)
            mach <- "sun4"
        else if (is.file("/bin/sun4c") && unix.true("/bin/sun4c") == 0)
            mach <- "sun4"
        else if (is.file("/bin/sparc") && unix.true("/bin/sparc") == 0)
            mach <- "sun4"
        else if (is.file("/bin/mips") && unix.true("/bin/mips") == 0)
        {
          if (is.file("/bin/4d") && unix.true("/bin/4d") == 0)
            mach <- "sgi"
        }
        else if (is.file("/bin/toshiba") && unix.true("/bin/toshiba") == 0)
            mach <- "toshiba"
        else if (is.file("/usr/bin/nm1.31") || is.file("/usr/bin/nm2.0"))
            mach <- "dec3100"
        else if (is.file("/usr/bin/nm2.0") || is.file("/usr/bin/nm2.1"))
            mach <- "dec5000"
        else if (is.file("/usr/ccs/bin/cord"))
            mach <- "alpha"
    }
    
    if (!missing(matrx))
    {
        dfile <- tempfile("unix")
        cat(t(matrx), file = dfile, fill = 80)

        if (length(collab) > 0)
        {
            colfile <- paste(dfile, ".col", sep="")
            cat(collab, file = colfile, sep="\n")
        }
        if (length(rowlab) > 0)
        {
            rowfile <- paste(dfile, ".row", sep="")
            cat(rowlab, file = rowfile, sep="\n")
        }

        if (!missing(vgroups))
        {
            vgroupfile <- paste(dfile, ".vgroups", sep="")
            cat(vgroups, file = vgroupfile, sep="\n")
        }
        if (!missing(colors))
        {
            colorfile <- paste(dfile, ".colors", sep="")
            cat(colors, file = colorfile, sep="\n")
        }
        if (!missing(glyphs))
        {
            glyphfile <- paste(dfile, ".glyphs", sep="")
            cat(glyphs, file = glyphfile, sep="\n")
        }
        if (!missing(erase))
        {
            erasefile <- paste(dfile, ".erase", sep="")
            cat(erase, file = erasefile, sep="\n")
        }

        if (!missing(lines))
        {
            linesfile <- paste(dfile, ".lines", sep="")
            if (nrow(lines) > 0)
            {
                cat(lines[1,], "\n", file = linesfile)
                if (nrow(lines) > 1)
                    for (i in 2:nrow(lines))
                        cat(lines[i,], "\n", file = linesfile, append=T)
            }
        }
        if (!missing(resources))
        {
            resourcefile <- paste(dfile, ".resources", sep="")
            cat(resources, file = resourcefile, sep="\n")
        }

        args <- paste("-s -std", std, "-dev", dev)

        if (!missing(display))
        {
            if (!is.character(display))
                warning("display must be a character string")
            else
                args <- paste("-display", display, args)
        }

        if (!missing(name))
        {
            if (!is.character(name))
            {
                warning("name must be a character string")
                name <- deparse(substitute(matrx))
            }
        }
        else
            name <- deparse(substitute(matrx))
        args <- paste("-name", paste("'", name, "'", sep=""), args)
        
        if (!missing(title))
        {
            if (!is.character(title))
            {
                warning("title must be a character string")
                title <- deparse(substitute(matrx))
            }
        }
        else
            title <- deparse(substitute(matrx))
        args <- paste("-title", paste("'", title, "'", sep=""), args)

#
# Note to installer:
# Here you need to specify the path to the xgobi executable
# on your system.  It it depends on mach, use it here; otherwise
# you don't need the mach variable and you can remove it from
# the function and the documentation if you like.
#
        path <- paste("/usr/local/", mach, "/bin/", sep="")
        command <- paste(path, "xgobi", sep="")
        command <- paste(command, args)

        command <- paste(command, nrow(matrx), ncol(matrx),
            search()[1], dfile, "&")
        cat(command, "\n")

        invisible(.Internal(unix(command, F), "S_system", T, 0))
    }
}
