package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"aytechnology.us/gomatlab/mod/aroz"
	"aytechnology.us/gomatlab/mod/server"
)

//MCServer should not be exported
var MCServer *server.Handler

var (
	handler *aroz.ArozHandler
)

//Kill signal handler. Do something before the system the core terminate.
func SetupCloseHandler() {
	c := make(chan os.Signal, 2)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-c
		log.Println("\r- Shutting down demo module.")
		//Do other things like close database or opened files

		os.Exit(0)
	}()
}

func main() {
	//AROZOS
	//If you have other flags, please add them here

	//Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
	handler = aroz.HandleFlagParse(aroz.ServiceInfo{
		Name:     "Octave",
		Desc:     "GO Octave",
		Group:    "Development",
		IconPath: "gooctave/icon.png",
		Version:  "0.0.1",
		//You can define any path before the actualy html file. This directory (in this case demo/ ) will be the reverse proxy endpoint for this module
		StartDir:     "gooctave/index.html",
		SupportFW:    true,
		LaunchFWDir:  "gooctave/index.html",
		SupportEmb:   true,
		LaunchEmb:    "gooctave/404.html",
		InitFWSize:   []int{720, 480},
		InitEmbSize:  []int{720, 480},
		SupportedExt: []string{},
	})

	MCServer = server.NewHandler()
	MCServer.StartService("./tmp/")

	//init the image Handler
	MCServer.SendCommand("figure(\"visible\", \"off\")")
	MCServer.SendCommand("plot(0,0)")
	//MCServer.SendCommand("figure(\"visible\", \"on\")")

	SetupCloseHandler()
	//Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
	log.Println("gooctave module started. Listening on " + handler.Port)
	webServer("./webroot/", "./tmp/", handler.Port)
	MCServer.Wait()

}
