package main

// This should build and upload the binary to mirrors

// upload binary to mirrors.wit.com/cloud/control-panel

import "log"
import "fmt"
import "time"
import "strings"
import "os/user"
import "os"
import "runtime"

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

import "git.wit.org/wit/shell"

// import "os/user"
// import "io/ioutil"
// github.com/bramvdbogaerde/go-scp

var BuildMap map[string]*bool

var filename string
var builddir string

func main() {
	BuildMap  = make(map[string]*bool)

	setbuilddir()

	// shell.Quiet(true)
	shell.Quiet(false)

	// shell.Run("git tag --list")
	// os.Exit(-1)

	for {
		shell.Run("git checkout master")
		shell.Run("git pull --tags")
		shell.Run("go get -v ...")
		shell.Run("go get -v -u ...")
		shell.Run("go get -v -u -t")

		findtags()
		time.Sleep(time.Second * 60)
	}
}

func findtags() {
	tags := shell.Run("git tag --list")
	// log.Println(tags)
	for _, tag := range strings.Split(tags, "\n") {
		tag = strings.Replace(tag, "v", "", -1) // remove all 'v' chars
		if (BuildMap[tag] == nil) {
			log.Println("TAG =", tag, "is nil")
			log.Println("\tCHECK IF BUILD WAS ATTEMPTED =", tag)
			BuildMap[tag] = build(tag)
		} else {
			if (*BuildMap[tag] == true) {
				log.Println("TAG =", tag, "is true")
			} else {
				log.Println("TAG =", tag, "is false")
			}
		}
	}

	// os.Exit(0)
}

func build(tag string) *bool {
	var b bool
	url := "https://mirrors.wit.com/cloud/control-panel/linux/" + filename + "-v" + tag + ".md5sum"
	log.Println("\tTRYING URL", url)
	md5sum := shell.Chomp(shell.Wget(url))
	if (md5sum != "") {
		log.Println("\tBUILD ALREADY DONE TAG =", tag, "md5sum =", md5sum)
		b = true
		// for {}
		return &b
	}
	// for {}

	shell.Quiet(false)
	shell.Run("git checkout v" + tag)

	gitref := shell.Run("git rev-list -1 HEAD")
	tagref := shell.Run("cat .git/refs/tags/v" + tag) // what is the right way?

	if (gitref != tagref) {
		log.Println("\tJESUS HOW TO DO THIS CORRECTLY?", gitref, "ne", tagref)
		// os.Exit(-1)
		// return nil
	}

	// setup the files that will end up in the binary
	epoch := fmt.Sprintf("%d", time.Now().Unix())
	shell.Write("./resources/BUILDDATE", epoch)
	shell.Write("./resources/BUILDREF", gitref)

	// rebuild the binary (always remove the old one
	shell.Run("rm " + filename)
	shell.Run("go build")
	md5sum = shell.Run("md5sum " + filename)
	log.Println("\tmd5sum =", md5sum)

	if (md5sum == "") {
		log.Println("\tBUILD FAILED")
		b = false
		return &b
	}
	shell.Write("/tmp/autobuild.md5sum", md5sum)
	upload(tag)

	// This version has been built yet
	log.Println("\tTHIS VERSION HAS BEEN UPLOADED =", tag)
	b = true
	return &b
}

func setbuilddir() {
	user, err := user.Current()
	spew.Dump(user)
	if err != nil {
		os.Exit(-1)
	}

	if runtime.GOOS == "linux" {
		log.Println("loadConfigFile() OS: Linux")
		filename = "cloud-control-panel"
		builddir = user.HomeDir + "/go/src/git.wit.com/wit/cloud-control-panel"
	} else if runtime.GOOS == "windows" {
		log.Println("loadConfigFile() OS: Windows")
		builddir = user.HomeDir + "\\go\\src\\git.wit.com\\wit\\cloud-control-panel"
		filename = "cloud-control-panel.exe"
	} else {
		log.Println("loadConfigFile() OS: " + runtime.GOOS)
		filename = "cloud-control-panel"
		builddir = user.HomeDir + "/go/src/git.wit.com/wit/cloud-control-panel"
	}
}

// upload the files to mirrors
func upload(tag string) {
	mirrors := "root@mirrors.wit.com:/data/mirrors/cloud/control-panel/linux/"

	// don't update the master VERSION here
	// shell.Run("scp resources/VERSION "     + mirrors)
	shell.Run("scp cloud-control-panel "   + mirrors)
	shell.Run("scp resources/BUILDDATE "   + mirrors + filename + "-v" + tag + ".BUILDDATE")
	shell.Run("scp cloud-control-panel "   + mirrors + filename + "-v" + tag)
	shell.Run("scp /tmp/autobuild.md5sum " + mirrors + filename + "-v" + tag + ".md5sum")
}
