package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"lst.net/pkg"
)

// ---- Handle Build Counter ----
func bumpBuild() (int, error) {
	data, err := os.ReadFile("../.build")
	buildNum := 0
	if err == nil { // if file exists, parse current number
		num, err := strconv.Atoi(strings.TrimSpace(string(data)))
		if err == nil {
			buildNum = num
		}
	}
	buildNum++
	err = os.WriteFile("../.build", []byte(fmt.Sprintf("%d", buildNum)), 0644)
	if err != nil {
		return 0, err
	}

	// update the db so we have the build number in here going forward.

	pkg.UpdateServerStats(int64(buildNum))

	return buildNum, nil
}
