
var name = "Tesses.CrossLang.ShellPackage-1.0.0.0-dev.crvm";
func main(args)
{
    create_archive();
    if(args.Count > 1 && args[1] == "install")
    {
        install();
    }
}

func install()
{
    var d = Env.CrossLangConfig;
    FS.Local.CreateDirectory(d);
    var sdfs = new SubdirFilesystem(FS.Local, d);
    var strm = FS.Local.OpenFile(name,"rb");
    FS.ExtractArchive(strm, sdfs);
    sdfs.Close();
    strm.Close();
}

func create_archive()
{
    
    var tmpFS = new SubdirFilesystem(FS.Local,FS.MakeFull("crosslang_shell_archive_maker/tmp"));


    func copyFile(src,dest)
    {
        if(!FS.Local.FileExists(src)) return;
        var srcF = FS.Local.OpenFile(src,"rb");
        var destF = tmpFS.OpenFile(dest,"wb");
        srcF.CopyTo(destF);
        srcF.Close();
        destF.Close();
    }
    
    var r = Path.Root();

    var shell = r / "Shell";


    tmpFS.CreateDirectory(shell);

    copyFile("Tesses.CrossLang.Shell/bin/Tesses.CrossLang.Args-1.0.0.0-dev.crvm", shell / "Tesses.CrossLang.Args-1.0.0.0-dev.crvm");
    copyFile("Tesses.CrossLang.Shell/bin/Tesses.CrossLang.BuildEssentials-1.0.0.0-dev.crvm", shell / "Tesses.CrossLang.BuildEssentials-1.0.0.0-dev.crvm");
    copyFile("Tesses.CrossLang.Shell/bin/Tesses.CrossLang.Shell-1.0.0.0-dev.crvm", shell / "Shell.crvm");
    copyFile("Tesses.CrossLang.Reference/bin/Tesses.CrossLang.Reference-1.0.0.0-dev.crvm", "Reference.crvm");
    var devStudio = r / "DevStudio";
    tmpFS.CreateDirectory(devStudio);
    copyFile("vscode-extension/syntaxes/crosslang.tmLanguage.json", devStudio / "grammar.json");
    copyFile("vscode-extension/crosslang-1.0.0.vsix", r / "crosslang-vscode-1.0.0.vsix");
    var webAppsCrossLang = r / "WebApps" / "crosslang";
    tmpFS.CreateDirectory(webAppsCrossLang);
    var pm = new Tesses.CrossLang.PackageManager();
    pm.Offline = false;
    var bt = new Tesses.CrossLang.BuildTool(pm);

    var publisher= new Tesses.CrossLang.Publisher(bt, FS.MakeFull("Tesses.CrossLang.App"));
    publisher.OutputDirectory =  FS.MakeFull("crosslang_shell_archive_maker/tmp/WebApps/crosslang");
    publisher.RuntimeIdentifier = "copy";
    publisher.Publish();
    
    var templates = r / "Templates";
    tmpFS.CreateDirectory(templates);


    

    copyFile("Templates/compiletool/bin/Tesses.CrossLang.Template.CompileTool-1.0.0.0-dev.crvm", templates / "compiletool.crvm");
    copyFile("Templates/console/bin/Tesses.CrossLang.Template.Console-1.0.0.0-dev.crvm", templates / "console.crvm");
    copyFile("Templates/lib/bin/Tesses.CrossLang.Template.Library-1.0.0.0-dev.crvm", templates / "lib.crvm");
    copyFile("Templates/template/bin/Tesses.CrossLang.Template.Template-1.0.0.0-dev.crvm", templates / "template.crvm");
    copyFile("Templates/web/bin/Tesses.CrossLang.Template.Website-1.0.0.0-dev.crvm", templates / "web.crvm");
    copyFile("Templates/emptyweb/bin/Tesses.CrossLang.Template.EmptyWebsite-1.0.0.0-dev.crvm", templates / "emptyweb.crvm");
    copyFile("Templates/tool/bin/Tesses.CrossLang.Template.Tool-1.0.0.0-dev.crvm", templates / "tool.crvm");
    copyFile("Templates/webapp/bin/Tesses.CrossLang.Template.WebApp-1.0.0.0-dev.crvm", templates / "webapp.crvm");
    copyFile("Templates/npmwebapp/bin/Tesses.CrossLang.Template.NPMWebApp-1.0.0.0-dev.crvm", templates / "npmwebapp.crvm");
    copyFile("Templates/npmweb/bin/Tesses.CrossLang.Template.NPMWebsite-1.0.0.0-dev.crvm", templates / "npmweb.crvm");
    
    var packageCache = r / "PackageCache";

    tmpFS.CreateDirectory(packageCache);
    tmpFS.CreateDirectory(packageCache / "Tesses.CrossLang.Args");
    tmpFS.CreateDirectory(packageCache / "Tesses.CrossLang.BuildEssentials");
    tmpFS.CreateDirectory(packageCache / "Tesses.CrossLang.Markup");
    tmpFS.CreateDirectory(packageCache / "Tesses.CrossLang.Std");
    copyFile("Tesses.CrossLang.Args/bin/Tesses.CrossLang.Args-1.0.0.0-dev.crvm", packageCache / "Tesses.CrossLang.Args" / "1.0.0.0-prod");
    copyFile("Tesses.CrossLang.BuildEssentials/bin/Tesses.CrossLang.BuildEssentials-1.0.0.0-dev.crvm", packageCache / "Tesses.CrossLang.BuildEssentials" / "1.0.0.0-prod");
    //copyFile("Tesses.CrossLang.Markup/bin/Tesses.CrossLang.Markup-1.0.0.0-dev.crvm", packageCache / "Tesses.CrossLang.Markup" / "1.0.0.0-prod");
    copyFile("Tesses.CrossLang.Args/bin/Tesses.CrossLang.Args-1.0.0.0-dev.crvm", packageCache / "Tesses.CrossLang.Args" / "1.0.0.0-prod");
    copyFile("Tesses.CrossLang.Std/bin/Tesses.CrossLang.Std-1.0.0.0-dev.crvm", packageCache / "Tesses.CrossLang.Std" / "1.0.0.0-prod");

    var installer = FS.Local.OpenFile(name, "wb");

    var ifo = {
        type = "archive",
        maintainer = "Mike Nolan",
        repo = "https://git.tesses.org/tesses50/crosslangextras",
        homepage = "https://crosslang.tesseslanguage.com/"
    };
    
    FS.CreateArchive(tmpFS,installer, "Tesses.CrossLang.ShellPackage","1.0.0.0-prod", Json.Encode(ifo));

    installer.Close();
    tmpFS.Close();

    FS.Local.DeleteDirectroyRecurse("crosslang_shell_archive_maker/tmp");
}
