package main

import (
	"bytes"
	"fmt"
	"go/format"
	"os"
	"path/filepath"

	"github.com/thlib/go-timezone-local/tzdata"
)

func main() {
	path, err := filepath.Abs("./tzlocal/tzmapping.go")
	if err != nil {
		panic(err)
	}

	var out bytes.Buffer
	out.WriteString("// Code generated by tzlocal/cmd/update_tzmapping.go DO NOT EDIT.\n")
	out.WriteString("//go:build windows\n// +build windows\n\n")
	out.WriteString("package tzlocal\n\n")
	if err := tzdata.UpdateWindowsTZMapping(&out); err != nil {
		panic(err)
	}

	generated, err := format.Source(out.Bytes())
	if err != nil {
		panic(err)
	}
	if err := os.WriteFile(path, generated, 0644); err != nil {
		panic(err)
	}
	fmt.Printf("Successfully updated %s\n", path)
}
