package main

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

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

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

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

func upgrade() {
	mirrorsBUILDDATE := shell.Chomp(shell.Wget("https://mirrors.wit.com/cloud/control-panel/linux/BUILDDATE"))

	// When this binary was built
	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("config.Dirty         =", config.Dirty)
	log.Println("config.Goversion     =", config.Goversion)
	log.Println()
	log.Println("myBUILDVERSION       =", myBUILDVERSION)
	log.Println("myBUILDDATE          =", myBUILDDATE)
	log.Println("mirrorsBUILDDATE     =", mirrorsBUILDDATE)
	log.Println()
	log.Println("Number of CPUs       =", runtime.NumCPU())
	log.Println("Number of GoRoutines =", runtime.NumGoroutine())
	log.Println("runtime.GOARCH       =", runtime.GOARCH)
	log.Println()

	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)
		} 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 {}
	// os.Exit(0)
}

func updateDebug() {
	mirrorsBUILDDATE := shell.Chomp(shell.Wget("https://mirrors.wit.com/cloud/control-panel/linux/BUILDDATE"))
	upstream         := shell.Chomp(shell.Wget("https://mirrors.wit.com/cloud/control-panel/linux/VERSION"))
	upstreamEpoch    := shell.Run("git log -1 --format=%at v" + upstream)
	epoch            := time.Now().Unix()

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

	log.Println()
	log.Println("upstream             =", upstream)
	log.Println("now()                =", epoch)
	log.Println("myBUILDDATE          =", myBUILDDATE)
	log.Println("upstreamEpoch        =", upstreamEpoch)
	log.Println("mirrorsBUILDDATE     =", mirrorsBUILDDATE)
	log.Println()
	log.Println("config.Gitref        =", config.Gitref)
	log.Println("config.Goversion     =", config.Goversion)
	log.Println("config.Dirty         =", config.Dirty)
	log.Println()
	log.Println("runtime.Version      =", runtime.Version())
	log.Println("Number of CPUs       =", runtime.NumCPU())
	log.Println("Number of GoRoutines =", runtime.NumGoroutine())
	log.Println("runtime.GOARCH       =", runtime.GOARCH)
	log.Println()

	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)
		} 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")
		}
	}

	// for {}
	// os.Exit(0)
}
