package main

/* 
	Check to see if there is a new version to upgrade
*/

import "log"
import "os"
import "time"
import "runtime"

import "git.wit.com/wit/shell"

// import "github.com/davecgh/go-spew/spew"

func upgrade() {
	filename         := "cloud-control-panel"
	mirrors          := "https://mirrors.wit.com/cloud/control-panel/"
	upstreamVERSION  := shell.Chomp(shell.Wget(mirrors + "VERSION"))
	filenameURL      := mirrors + "linux/cloud-control-panel-v" + upstreamVERSION
	builddateURL     := mirrors + "linux/cloud-control-panel-v" + upstreamVERSION + ".BUILDDATE"
	mirrorsBUILDDATE := shell.Chomp(shell.Wget(builddateURL))
	epoch            := time.Now().Unix()

	myBUILDDATE, _    := packrBox.FindString("BUILDDATE")
	myBUILDDATE        = shell.Chomp(myBUILDDATE)
	myBUILDVERSION, _ := packrBox.FindString("VERSION")
	myBUILDVERSION     = shell.Chomp(myBUILDVERSION)

	config.Version     = myBUILDVERSION
	config.Builddate   = shell.Int64(myBUILDDATE)
	config.Goversion   = runtime.Version()

	log.Println()
	log.Println("upstreamVERSION      =", upstreamVERSION)
	log.Println("builddateURL         =", builddateURL)
	log.Println("now()                =", epoch)
	log.Println("myBUILDVERSION       =", myBUILDVERSION)
	log.Println("myBUILDDATE          =", myBUILDDATE)
	log.Println("mirrorsBUILDDATE     =", mirrorsBUILDDATE)
	log.Println()

	log.Println("config.Dirty         =", config.Dirty)
	log.Println("config.Version       =", config.Version)
	log.Println("config.Goversion     =", config.Goversion)
	log.Println()
	log.Println()
	log.Println("Number of CPUs       =", runtime.NumCPU())
	log.Println("Number of GoRoutines =", runtime.NumGoroutine())
	log.Println("runtime.GOARCH       =", runtime.GOARCH)
	log.Println()

	// https://github.com/go-cmd/cmd/issues/20
	// TODO: look at go-cmd ?
	log.Println("update() compare", mirrorsBUILDDATE, "to", myBUILDDATE)
	if (! config.Dirty) {
		// See if a newer version of this control panel exists
		// if (epoch > time.now().Unix()) // seconds since epoch
		if (shell.Int(mirrorsBUILDDATE) > shell.Int(myBUILDDATE)) {
			log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
			log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
			log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
			time.Sleep(time.Second * 2)
			shell.WgetToFile("/tmp/" + filename, filenameURL)
			shell.Run("chmod +x /tmp/cloud-control-panel")

			// execute the new version here and never return
			// TODO: figure out how to nohup here and properly exit
			shell.Exec("/tmp/cloud-control-panel")
			os.Exit(0)
		} else {
			log.Println("update() UPSTREAM VERSION IS OLDER THAN THIS")
		}
	} else {
		if (shell.Int(mirrorsBUILDDATE) > shell.Int(myBUILDDATE)) {
			log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
			log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
			log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
			time.Sleep(time.Second * 2)
		} else {
			log.Println("update() dirty=true UPSTREAM VERSION IS OLDER THAN THIS")
		}
	}
	time.Sleep(time.Second)

	for {}
}